Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. * hasProperty()
  289. *
  290. * @param string $propertyName
  291. * @return bool
  292. */
  293. public function hasProperty($propertyName)
  294. {
  295. return isset($this->_properties[$propertyName]);
  296. }
  297. /**
  298. * setMethods()
  299. *
  300. * @param array $methods
  301. * @return Zend_CodeGenerator_Php_Class
  302. */
  303. public function setMethods(Array $methods)
  304. {
  305. foreach ($methods as $method) {
  306. $this->setMethod($method);
  307. }
  308. return $this;
  309. }
  310. /**
  311. * setMethod()
  312. *
  313. * @param array|Zend_CodeGenerator_Php_Method $method
  314. * @return Zend_CodeGenerator_Php_Class
  315. */
  316. public function setMethod($method)
  317. {
  318. if (is_array($method)) {
  319. $method = new Zend_CodeGenerator_Php_Method($method);
  320. $methodName = $method->getName();
  321. } elseif ($method instanceof Zend_CodeGenerator_Php_Method) {
  322. $methodName = $method->getName();
  323. } else {
  324. require_once 'Zend/CodeGenerator/Php/Exception.php';
  325. throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method');
  326. }
  327. if (isset($this->_methods[$methodName])) {
  328. require_once 'Zend/CodeGenerator/Php/Exception.php';
  329. throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
  330. }
  331. $this->_methods->append($method);
  332. return $this;
  333. }
  334. /**
  335. * getMethods()
  336. *
  337. * @return array
  338. */
  339. public function getMethods()
  340. {
  341. return $this->_methods;
  342. }
  343. /**
  344. * getMethod()
  345. *
  346. * @param string $methodName
  347. * @return Zend_CodeGenerator_Php_Method
  348. */
  349. public function getMethod($methodName)
  350. {
  351. foreach ($this->_methods as $method) {
  352. if ($method->getName() == $methodName) {
  353. return $method;
  354. }
  355. }
  356. return false;
  357. }
  358. /**
  359. * hasMethod()
  360. *
  361. * @param string $methodName
  362. * @return bool
  363. */
  364. public function hasMethod($methodName)
  365. {
  366. return isset($this->_methods[$methodName]);
  367. }
  368. /**
  369. * isSourceDirty()
  370. *
  371. * @return bool
  372. */
  373. public function isSourceDirty()
  374. {
  375. if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
  376. return true;
  377. }
  378. foreach ($this->_properties as $property) {
  379. if ($property->isSourceDirty()) {
  380. return true;
  381. }
  382. }
  383. foreach ($this->_methods as $method) {
  384. if ($method->isSourceDirty()) {
  385. return true;
  386. }
  387. }
  388. return parent::isSourceDirty();
  389. }
  390. /**
  391. * generate()
  392. *
  393. * @return string
  394. */
  395. public function generate()
  396. {
  397. if (!$this->isSourceDirty()) {
  398. return $this->getSourceContent();
  399. }
  400. $output = '';
  401. if (null !== ($docblock = $this->getDocblock())) {
  402. $docblock->setIndentation('');
  403. $output .= $docblock->generate();
  404. }
  405. if ($this->isAbstract()) {
  406. $output .= 'abstract ';
  407. }
  408. $output .= 'class ' . $this->getName();
  409. if (null !== ($ec = $this->_extendedClass)) {
  410. $output .= ' extends ' . $ec;
  411. }
  412. $implemented = $this->getImplementedInterfaces();
  413. if (!empty($implemented)) {
  414. $output .= ' implements ' . implode(', ', $implemented);
  415. }
  416. $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
  417. $properties = $this->getProperties();
  418. if (!empty($properties)) {
  419. foreach ($properties as $property) {
  420. $output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
  421. }
  422. }
  423. $methods = $this->getMethods();
  424. if (!empty($methods)) {
  425. foreach ($methods as $method) {
  426. $output .= $method->generate() . self::LINE_FEED;
  427. }
  428. }
  429. $output .= self::LINE_FEED . '}' . self::LINE_FEED;
  430. return $output;
  431. }
  432. /**
  433. * _init() - is called at construction time
  434. *
  435. */
  436. protected function _init()
  437. {
  438. $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
  439. $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
  440. }
  441. }