|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_CodeGenerator |
|
17 * @subpackage PHP |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Method.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_CodeGenerator_Php_Member_Abstract |
|
25 */ |
|
26 require_once 'Zend/CodeGenerator/Php/Member/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_CodeGenerator_Php_Docblock |
|
30 */ |
|
31 require_once 'Zend/CodeGenerator/Php/Docblock.php'; |
|
32 |
|
33 /** |
|
34 * @see Zend_CodeGenerator_Php_Parameter |
|
35 */ |
|
36 require_once 'Zend/CodeGenerator/Php/Parameter.php'; |
|
37 |
|
38 /** |
|
39 * @category Zend |
|
40 * @package Zend_CodeGenerator |
|
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
42 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
43 */ |
|
44 class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract |
|
45 { |
|
46 /** |
|
47 * @var Zend_CodeGenerator_Php_Docblock |
|
48 */ |
|
49 protected $_docblock = null; |
|
50 |
|
51 /** |
|
52 * @var bool |
|
53 */ |
|
54 protected $_isFinal = false; |
|
55 |
|
56 /** |
|
57 * @var array |
|
58 */ |
|
59 protected $_parameters = array(); |
|
60 |
|
61 /** |
|
62 * @var string |
|
63 */ |
|
64 protected $_body = null; |
|
65 |
|
66 /** |
|
67 * fromReflection() |
|
68 * |
|
69 * @param Zend_Reflection_Method $reflectionMethod |
|
70 * @return Zend_CodeGenerator_Php_Method |
|
71 */ |
|
72 public static function fromReflection(Zend_Reflection_Method $reflectionMethod) |
|
73 { |
|
74 $method = new self(); |
|
75 |
|
76 $method->setSourceContent($reflectionMethod->getContents(false)); |
|
77 $method->setSourceDirty(false); |
|
78 |
|
79 if ($reflectionMethod->getDocComment() != '') { |
|
80 $method->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionMethod->getDocblock())); |
|
81 } |
|
82 |
|
83 $method->setFinal($reflectionMethod->isFinal()); |
|
84 |
|
85 if ($reflectionMethod->isPrivate()) { |
|
86 $method->setVisibility(self::VISIBILITY_PRIVATE); |
|
87 } elseif ($reflectionMethod->isProtected()) { |
|
88 $method->setVisibility(self::VISIBILITY_PROTECTED); |
|
89 } else { |
|
90 $method->setVisibility(self::VISIBILITY_PUBLIC); |
|
91 } |
|
92 |
|
93 $method->setStatic($reflectionMethod->isStatic()); |
|
94 |
|
95 $method->setName($reflectionMethod->getName()); |
|
96 |
|
97 foreach ($reflectionMethod->getParameters() as $reflectionParameter) { |
|
98 $method->setParameter(Zend_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter)); |
|
99 } |
|
100 |
|
101 $method->setBody($reflectionMethod->getBody()); |
|
102 |
|
103 return $method; |
|
104 } |
|
105 |
|
106 /** |
|
107 * setFinal() |
|
108 * |
|
109 * @param bool $isFinal |
|
110 */ |
|
111 public function setFinal($isFinal) |
|
112 { |
|
113 $this->_isFinal = ($isFinal) ? true : false; |
|
114 } |
|
115 |
|
116 /** |
|
117 * setParameters() |
|
118 * |
|
119 * @param array $parameters |
|
120 * @return Zend_CodeGenerator_Php_Method |
|
121 */ |
|
122 public function setParameters(Array $parameters) |
|
123 { |
|
124 foreach ($parameters as $parameter) { |
|
125 $this->setParameter($parameter); |
|
126 } |
|
127 return $this; |
|
128 } |
|
129 |
|
130 /** |
|
131 * setParameter() |
|
132 * |
|
133 * @param Zend_CodeGenerator_Php_Parameter|array $parameter |
|
134 * @return Zend_CodeGenerator_Php_Method |
|
135 */ |
|
136 public function setParameter($parameter) |
|
137 { |
|
138 if (is_array($parameter)) { |
|
139 $parameter = new Zend_CodeGenerator_Php_Parameter($parameter); |
|
140 $parameterName = $parameter->getName(); |
|
141 } elseif ($parameter instanceof Zend_CodeGenerator_Php_Parameter) { |
|
142 $parameterName = $parameter->getName(); |
|
143 } else { |
|
144 require_once 'Zend/CodeGenerator/Php/Exception.php'; |
|
145 throw new Zend_CodeGenerator_Php_Exception('setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter'); |
|
146 } |
|
147 |
|
148 $this->_parameters[$parameterName] = $parameter; |
|
149 return $this; |
|
150 } |
|
151 |
|
152 /** |
|
153 * getParameters() |
|
154 * |
|
155 * @return array Array of Zend_CodeGenerator_Php_Parameter |
|
156 */ |
|
157 public function getParameters() |
|
158 { |
|
159 return $this->_parameters; |
|
160 } |
|
161 |
|
162 /** |
|
163 * setBody() |
|
164 * |
|
165 * @param string $body |
|
166 * @return Zend_CodeGenerator_Php_Method |
|
167 */ |
|
168 public function setBody($body) |
|
169 { |
|
170 $this->_body = $body; |
|
171 return $this; |
|
172 } |
|
173 |
|
174 /** |
|
175 * getBody() |
|
176 * |
|
177 * @return string |
|
178 */ |
|
179 public function getBody() |
|
180 { |
|
181 return $this->_body; |
|
182 } |
|
183 |
|
184 /** |
|
185 * generate() |
|
186 * |
|
187 * @return string |
|
188 */ |
|
189 public function generate() |
|
190 { |
|
191 $output = ''; |
|
192 |
|
193 $indent = $this->getIndentation(); |
|
194 |
|
195 if (($docblock = $this->getDocblock()) !== null) { |
|
196 $docblock->setIndentation($indent); |
|
197 $output .= $docblock->generate(); |
|
198 } |
|
199 |
|
200 $output .= $indent; |
|
201 |
|
202 if ($this->isAbstract()) { |
|
203 $output .= 'abstract '; |
|
204 } else { |
|
205 $output .= (($this->isFinal()) ? 'final ' : ''); |
|
206 } |
|
207 |
|
208 $output .= $this->getVisibility() |
|
209 . (($this->isStatic()) ? ' static' : '') |
|
210 . ' function ' . $this->getName() . '('; |
|
211 |
|
212 $parameters = $this->getParameters(); |
|
213 if (!empty($parameters)) { |
|
214 foreach ($parameters as $parameter) { |
|
215 $parameterOuput[] = $parameter->generate(); |
|
216 } |
|
217 |
|
218 $output .= implode(', ', $parameterOuput); |
|
219 } |
|
220 |
|
221 $output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED; |
|
222 |
|
223 if ($this->_body) { |
|
224 $output .= ' ' |
|
225 . str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body)) |
|
226 . self::LINE_FEED; |
|
227 } |
|
228 |
|
229 $output .= $indent . '}' . self::LINE_FEED; |
|
230 |
|
231 return $output; |
|
232 } |
|
233 |
|
234 } |