Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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-2009 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_Abstract
  24. */
  25. require_once 'Zend/CodeGenerator/Php/Abstract.php';
  26. /**
  27. * @see Zend_CodeGenerator_Php_Member_Container
  28. */
  29. require_once 'Zend/CodeGenerator/Php/Member/Container.php';
  30. /**
  31. * @see Zend_CodeGenerator_Php_Method
  32. */
  33. require_once 'Zend/CodeGenerator/Php/Method.php';
  34. /**
  35. * @see Zend_CodeGenerator_Php_Property
  36. */
  37. require_once 'Zend/CodeGenerator/Php/Property.php';
  38. /**
  39. * @see Zend_CodeGenerator_Php_Docblock
  40. */
  41. require_once 'Zend/CodeGenerator/Php/Docblock.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_CodeGenerator
  45. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
  49. {
  50. /**
  51. * @var Zend_CodeGenerator_Php_Docblock
  52. */
  53. protected $_docblock = null;
  54. /**
  55. * @var string
  56. */
  57. protected $_name = null;
  58. /**
  59. * @var bool
  60. */
  61. protected $_isAbstract = false;
  62. /**
  63. * @var string
  64. */
  65. protected $_extendedClass = null;
  66. /**
  67. * @var array Array of string names
  68. */
  69. protected $_implementedInterfaces = array();
  70. /**
  71. * @var array Array of properties
  72. */
  73. protected $_properties = null;
  74. /**
  75. * @var array Array of methods
  76. */
  77. protected $_methods = null;
  78. /**
  79. * fromReflection() - build a Code Generation PHP Object from a Class Reflection
  80. *
  81. * @param Zend_Reflection_Class $reflectionClass
  82. * @return Zend_CodeGenerator_Php_Class
  83. */
  84. public static function fromReflection(Zend_Reflection_Class $reflectionClass)
  85. {
  86. $class = new self();
  87. $class->setSourceContent($class->getSourceContent());
  88. $class->setSourceDirty(false);
  89. if ($reflectionClass->getDocComment() != '') {
  90. $class->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionClass->getDocblock()));
  91. }
  92. $class->setAbstract($reflectionClass->isAbstract());
  93. $class->setName($reflectionClass->getName());
  94. if ($parentClass = $reflectionClass->getParentClass()) {
  95. $class->setExtendedClass($parentClass->getName());
  96. $interfaces = array_diff($parentClass->getInterfaces(), $reflectionClass->getInterfaces());
  97. } else {
  98. $interfaces = $reflectionClass->getInterfaces();
  99. }
  100. $class->setImplementedInterfaces($interfaces);
  101. $properties = array();
  102. foreach ($reflectionClass->getProperties() as $reflectionProperty) {
  103. if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) {
  104. $properties[] = Zend_CodeGenerator_Php_Property::fromReflection($reflectionProperty);
  105. }
  106. }
  107. $class->setProperties($properties);
  108. $methods = array();
  109. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  110. if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) {
  111. $methods[] = Zend_CodeGenerator_Php_Method::fromReflection($reflectionMethod);
  112. }
  113. }
  114. $class->setMethods($methods);
  115. return $class;
  116. }
  117. /**
  118. * setDocblock() Set the docblock
  119. *
  120. * @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
  121. * @return Zend_CodeGenerator_Php_File
  122. */
  123. public function setDocblock($docblock)
  124. {
  125. if (is_string($docblock)) {
  126. $docblock = array('shortDescription' => $docblock);
  127. }
  128. if (is_array($docblock)) {
  129. $docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
  130. } elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
  131. require_once 'Zend/CodeGenerator/Php/Exception.php';
  132. throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
  133. }
  134. $this->_docblock = $docblock;
  135. return $this;
  136. }
  137. /**
  138. * getDocblock()
  139. *
  140. * @return Zend_CodeGenerator_Php_Docblock
  141. */
  142. public function getDocblock()
  143. {
  144. return $this->_docblock;
  145. }
  146. /**
  147. * setName()
  148. *
  149. * @param string $name
  150. * @return Zend_CodeGenerator_Php_Class
  151. */
  152. public function setName($name)
  153. {
  154. $this->_name = $name;
  155. return $this;
  156. }
  157. /**
  158. * getName()
  159. *
  160. * @return string
  161. */
  162. public function getName()
  163. {
  164. return $this->_name;
  165. }
  166. /**
  167. * setAbstract()
  168. *
  169. * @param bool $isAbstract
  170. * @return Zend_CodeGenerator_Php_Class
  171. */
  172. public function setAbstract($isAbstract)
  173. {
  174. $this->_isAbstract = ($isAbstract) ? true : false;
  175. return $this;
  176. }
  177. /**
  178. * isAbstract()
  179. *
  180. * @return bool
  181. */
  182. public function isAbstract()
  183. {
  184. return $this->_isAbstract;
  185. }
  186. /**
  187. * setExtendedClass()
  188. *
  189. * @param string $extendedClass
  190. * @return Zend_CodeGenerator_Php_Class
  191. */
  192. public function setExtendedClass($extendedClass)
  193. {
  194. $this->_extendedClass = $extendedClass;
  195. return $this;
  196. }
  197. /**
  198. * getExtendedClass()
  199. *
  200. * @return string
  201. */
  202. public function getExtendedClass()
  203. {
  204. return $this->_extendedClass;
  205. }
  206. /**
  207. * setImplementedInterfaces()
  208. *
  209. * @param array $implementedInterfaces
  210. * @return Zend_CodeGenerator_Php_Class
  211. */
  212. public function setImplementedInterfaces(Array $implementedInterfaces)
  213. {
  214. $this->_implementedInterfaces = $implementedInterfaces;
  215. return $this;
  216. }
  217. /**
  218. * getImplementedInterfaces
  219. *
  220. * @return array
  221. */
  222. public function getImplementedInterfaces()
  223. {
  224. return $this->_implementedInterfaces;
  225. }
  226. /**
  227. * setProperties()
  228. *
  229. * @param array $properties
  230. * @return Zend_CodeGenerator_Php_Class
  231. */
  232. public function setProperties(Array $properties)
  233. {
  234. foreach ($properties as $property) {
  235. $this->setProperty($property);
  236. }
  237. return $this;
  238. }
  239. /**
  240. * setProperty()
  241. *
  242. * @param array|Zend_CodeGenerator_Php_Property $property
  243. * @return Zend_CodeGenerator_Php_Class
  244. */
  245. public function setProperty($property)
  246. {
  247. if (is_array($property)) {
  248. $property = new Zend_CodeGenerator_Php_Property($property);
  249. $propertyName = $property->getName();
  250. } elseif ($property instanceof Zend_CodeGenerator_Php_Property) {
  251. $propertyName = $property->getName();
  252. } else {
  253. require_once 'Zend/CodeGenerator/Php/Exception.php';
  254. throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
  255. }
  256. if (isset($this->_properties[$propertyName])) {
  257. require_once 'Zend/CodeGenerator/Php/Exception.php';
  258. throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
  259. }
  260. $this->_properties->append($property);
  261. return $this;
  262. }
  263. /**
  264. * getProperties()
  265. *
  266. * @return array
  267. */
  268. public function getProperties()
  269. {
  270. return $this->_properties;
  271. }
  272. /**
  273. * getProperty()
  274. *
  275. * @param string $propertyName
  276. * @return Zend_CodeGenerator_Php_Property
  277. */
  278. public function getProperty($propertyName)
  279. {
  280. foreach ($this->_properties as $property) {
  281. if ($property->getName() == $propertyName) {
  282. return $property;
  283. }
  284. }
  285. return false;
  286. }
  287. /**
  288. * setMethods()
  289. *
  290. * @param array $methods
  291. * @return Zend_CodeGenerator_Php_Class
  292. */
  293. public function setMethods(Array $methods)
  294. {
  295. foreach ($methods as $method) {
  296. $this->setMethod($method);
  297. }
  298. return $this;
  299. }
  300. /**
  301. * setMethod()
  302. *
  303. * @param array|Zend_CodeGenerator_Php_Method $method
  304. * @return Zend_CodeGenerator_Php_Class
  305. */
  306. public function setMethod($method)
  307. {
  308. if (is_array($method)) {
  309. $method = new Zend_CodeGenerator_Php_Method($method);
  310. $methodName = $method->getName();
  311. } elseif ($method instanceof Zend_CodeGenerator_Php_Method) {
  312. $methodName = $method->getName();
  313. } else {
  314. require_once 'Zend/CodeGenerator/Php/Exception.php';
  315. throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method');
  316. }
  317. if (isset($this->_methods[$methodName])) {
  318. require_once 'Zend/CodeGenerator/Php/Exception.php';
  319. throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
  320. }
  321. $this->_methods->append($method);
  322. return $this;
  323. }
  324. /**
  325. * getMethods()
  326. *
  327. * @return array
  328. */
  329. public function getMethods()
  330. {
  331. return $this->_methods;
  332. }
  333. /**
  334. * getMethod()
  335. *
  336. * @param string $methodName
  337. * @return Zend_CodeGenerator_Php_Method
  338. */
  339. public function getMethod($methodName)
  340. {
  341. foreach ($this->_methods as $method) {
  342. if ($method->getName() == $methodName) {
  343. return $method;
  344. }
  345. }
  346. return false;
  347. }
  348. /**
  349. * hasMethod()
  350. *
  351. * @param string $methodName
  352. * @return bool
  353. */
  354. public function hasMethod($methodName)
  355. {
  356. return isset($this->_methods[$methodName]);
  357. }
  358. /**
  359. * isSourceDirty()
  360. *
  361. * @return bool
  362. */
  363. public function isSourceDirty()
  364. {
  365. if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
  366. return true;
  367. }
  368. foreach ($this->_properties as $property) {
  369. if ($property->isSourceDirty()) {
  370. return true;
  371. }
  372. }
  373. foreach ($this->_methods as $method) {
  374. if ($method->isSourceDirty()) {
  375. return true;
  376. }
  377. }
  378. return parent::isSourceDirty();
  379. }
  380. /**
  381. * generate()
  382. *
  383. * @return string
  384. */
  385. public function generate()
  386. {
  387. if (!$this->isSourceDirty()) {
  388. return $this->getSourceContent();
  389. }
  390. $output = '';
  391. if (null !== ($docblock = $this->getDocblock())) {
  392. $docblock->setIndentation('');
  393. $output .= $docblock->generate();
  394. }
  395. if ($this->isAbstract()) {
  396. $output .= 'abstract ';
  397. }
  398. $output .= 'class ' . $this->getName();
  399. if (null !== ($ec = $this->_extendedClass)) {
  400. $output .= ' extends ' . $ec;
  401. }
  402. $implemented = $this->getImplementedInterfaces();
  403. if (!empty($implemented)) {
  404. $output .= ' implements ' . implode(', ', $implemented);
  405. }
  406. $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
  407. $properties = $this->getProperties();
  408. if (!empty($properties)) {
  409. foreach ($properties as $property) {
  410. $output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
  411. }
  412. }
  413. $methods = $this->getMethods();
  414. if (!empty($methods)) {
  415. foreach ($methods as $method) {
  416. $output .= $method->generate() . self::LINE_FEED;
  417. }
  418. }
  419. $output .= self::LINE_FEED . '}' . self::LINE_FEED;
  420. return $output;
  421. }
  422. /**
  423. * _init() - is called at construction time
  424. *
  425. */
  426. protected function _init()
  427. {
  428. $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
  429. $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
  430. }
  431. }