Method.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_CodeGenerator_Php_Member_Abstract
  24. */
  25. require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
  26. /**
  27. * @see Zend_CodeGenerator_Php_Docblock
  28. */
  29. require_once 'Zend/CodeGenerator/Php/Docblock.php';
  30. /**
  31. * @see Zend_CodeGenerator_Php_Parameter
  32. */
  33. require_once 'Zend/CodeGenerator/Php/Parameter.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_CodeGenerator
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract
  41. {
  42. /**
  43. * @var Zend_CodeGenerator_Php_Docblock
  44. */
  45. protected $_docblock = null;
  46. /**
  47. * @var bool
  48. */
  49. protected $_isFinal = false;
  50. /**
  51. * @var array
  52. */
  53. protected $_parameters = array();
  54. /**
  55. * @var string
  56. */
  57. protected $_body = null;
  58. /**
  59. * fromReflection()
  60. *
  61. * @param Zend_Reflection_Method $reflectionMethod
  62. * @return Zend_CodeGenerator_Php_Method
  63. */
  64. public static function fromReflection(Zend_Reflection_Method $reflectionMethod)
  65. {
  66. $method = new self();
  67. $method->setSourceContent($reflectionMethod->getContents(false));
  68. $method->setSourceDirty(false);
  69. if ($reflectionMethod->getDocComment() != '') {
  70. $method->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionMethod->getDocblock()));
  71. }
  72. $method->setFinal($reflectionMethod->isFinal());
  73. if ($reflectionMethod->isPrivate()) {
  74. $method->setVisibility(self::VISIBILITY_PRIVATE);
  75. } elseif ($reflectionMethod->isProtected()) {
  76. $method->setVisibility(self::VISIBILITY_PROTECTED);
  77. } else {
  78. $method->setVisibility(self::VISIBILITY_PUBLIC);
  79. }
  80. $method->setStatic($reflectionMethod->isStatic());
  81. $method->setName($reflectionMethod->getName());
  82. foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
  83. $method->setParameter(Zend_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter));
  84. }
  85. $method->setBody($reflectionMethod->getBody());
  86. return $method;
  87. }
  88. /**
  89. * setFinal()
  90. *
  91. * @param bool $isFinal
  92. */
  93. public function setFinal($isFinal)
  94. {
  95. $this->_isFinal = ($isFinal) ? true : false;
  96. }
  97. /**
  98. * setParameters()
  99. *
  100. * @param array $parameters
  101. * @return Zend_CodeGenerator_Php_Method
  102. */
  103. public function setParameters(Array $parameters)
  104. {
  105. foreach ($parameters as $parameter) {
  106. $this->setParameter($parameter);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * setParameter()
  112. *
  113. * @param Zend_CodeGenerator_Php_Parameter|array $parameter
  114. * @return Zend_CodeGenerator_Php_Method
  115. */
  116. public function setParameter($parameter)
  117. {
  118. if (is_array($parameter)) {
  119. $parameter = new Zend_CodeGenerator_Php_Parameter($parameter);
  120. $parameterName = $parameter->getName();
  121. } elseif ($parameter instanceof Zend_CodeGenerator_Php_Parameter) {
  122. $parameterName = $parameter->getName();
  123. } else {
  124. require_once 'Zend/CodeGenerator/Php/Exception.php';
  125. throw new Zend_CodeGenerator_Php_Exception('setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter');
  126. }
  127. $this->_parameters[$parameterName] = $parameter;
  128. return $this;
  129. }
  130. /**
  131. * getParameters()
  132. *
  133. * @return array Array of Zend_CodeGenerator_Php_Parameter
  134. */
  135. public function getParameters()
  136. {
  137. return $this->_parameters;
  138. }
  139. /**
  140. * setBody()
  141. *
  142. * @param string $body
  143. * @return Zend_CodeGenerator_Php_Method
  144. */
  145. public function setBody($body)
  146. {
  147. $this->_body = $body;
  148. return $this;
  149. }
  150. /**
  151. * getBody()
  152. *
  153. * @return string
  154. */
  155. public function getBody()
  156. {
  157. return $this->_body;
  158. }
  159. /**
  160. * generate()
  161. *
  162. * @return string
  163. */
  164. public function generate()
  165. {
  166. $output = '';
  167. $indent = $this->getIndentation();
  168. if (($docblock = $this->getDocblock()) !== null) {
  169. $docblock->setIndentation($indent);
  170. $output .= $docblock->generate();
  171. }
  172. $output .= $indent;
  173. if ($this->isAbstract()) {
  174. $output .= 'abstract ';
  175. } else {
  176. $output .= (($this->isFinal()) ? 'final ' : '');
  177. }
  178. $output .= $this->getVisibility()
  179. . (($this->isStatic()) ? ' static' : '')
  180. . ' function ' . $this->getName() . '(';
  181. $parameters = $this->getParameters();
  182. if (!empty($parameters)) {
  183. foreach ($parameters as $parameter) {
  184. $parameterOuput[] = $parameter->generate();
  185. }
  186. $output .= implode(', ', $parameterOuput);
  187. }
  188. $output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
  189. if ($this->_body && $this->isSourceDirty()) {
  190. $output .= ' '
  191. . str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body))
  192. . self::LINE_FEED;
  193. } elseif ($this->_body) {
  194. $output .= $this->_body . self::LINE_FEED;
  195. }
  196. $output .= $indent . '}' . self::LINE_FEED;
  197. return $output;
  198. }
  199. }