PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/CodeGenerator/Php/Class.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 513 lines | 250 code | 68 blank | 195 comment | 32 complexity | 6c5c8acec11c3e92c8f844581d3899b5 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Class.php 24457 2011-09-11 13:34:44Z padraic $
  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-2011 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($reflectionClass->getInterfaces(), $parentClass->getInterfaces());
  97. } else {
  98. $interfaces = $reflectionClass->getInterfaces();
  99. }
  100. $interfaceNames = array();
  101. foreach($interfaces AS $interface) {
  102. $interfaceNames[] = $interface->getName();
  103. }
  104. $class->setImplementedInterfaces($interfaceNames);
  105. $properties = array();
  106. foreach ($reflectionClass->getProperties() as $reflectionProperty) {
  107. if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) {
  108. $properties[] = Zend_CodeGenerator_Php_Property::fromReflection($reflectionProperty);
  109. }
  110. }
  111. $class->setProperties($properties);
  112. $methods = array();
  113. foreach ($reflectionClass->getMethods() as $reflectionMethod) {
  114. if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) {
  115. $methods[] = Zend_CodeGenerator_Php_Method::fromReflection($reflectionMethod);
  116. }
  117. }
  118. $class->setMethods($methods);
  119. return $class;
  120. }
  121. /**
  122. * setDocblock() Set the docblock
  123. *
  124. * @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
  125. * @return Zend_CodeGenerator_Php_File
  126. */
  127. public function setDocblock($docblock)
  128. {
  129. if (is_string($docblock)) {
  130. $docblock = array('shortDescription' => $docblock);
  131. }
  132. if (is_array($docblock)) {
  133. $docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
  134. } elseif ((!is_null($docblock)) && (!$docblock instanceof Zend_CodeGenerator_Php_Docblock)) {
  135. require_once 'Zend/CodeGenerator/Php/Exception.php';
  136. throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
  137. }
  138. $this->_docblock = $docblock;
  139. return $this;
  140. }
  141. /**
  142. * getDocblock()
  143. *
  144. * @return Zend_CodeGenerator_Php_Docblock
  145. */
  146. public function getDocblock()
  147. {
  148. return $this->_docblock;
  149. }
  150. /**
  151. * setName()
  152. *
  153. * @param string $name
  154. * @return Zend_CodeGenerator_Php_Class
  155. */
  156. public function setName($name)
  157. {
  158. $this->_name = $name;
  159. return $this;
  160. }
  161. /**
  162. * getName()
  163. *
  164. * @return string
  165. */
  166. public function getName()
  167. {
  168. return $this->_name;
  169. }
  170. /**
  171. * setAbstract()
  172. *
  173. * @param bool $isAbstract
  174. * @return Zend_CodeGenerator_Php_Class
  175. */
  176. public function setAbstract($isAbstract)
  177. {
  178. $this->_isAbstract = ($isAbstract) ? true : false;
  179. return $this;
  180. }
  181. /**
  182. * isAbstract()
  183. *
  184. * @return bool
  185. */
  186. public function isAbstract()
  187. {
  188. return $this->_isAbstract;
  189. }
  190. /**
  191. * setExtendedClass()
  192. *
  193. * @param string $extendedClass
  194. * @return Zend_CodeGenerator_Php_Class
  195. */
  196. public function setExtendedClass($extendedClass)
  197. {
  198. $this->_extendedClass = $extendedClass;
  199. return $this;
  200. }
  201. /**
  202. * getExtendedClass()
  203. *
  204. * @return string
  205. */
  206. public function getExtendedClass()
  207. {
  208. return $this->_extendedClass;
  209. }
  210. /**
  211. * setImplementedInterfaces()
  212. *
  213. * @param array $implementedInterfaces
  214. * @return Zend_CodeGenerator_Php_Class
  215. */
  216. public function setImplementedInterfaces(Array $implementedInterfaces)
  217. {
  218. $this->_implementedInterfaces = $implementedInterfaces;
  219. return $this;
  220. }
  221. /**
  222. * getImplementedInterfaces
  223. *
  224. * @return array
  225. */
  226. public function getImplementedInterfaces()
  227. {
  228. return $this->_implementedInterfaces;
  229. }
  230. /**
  231. * setProperties()
  232. *
  233. * @param array $properties
  234. * @return Zend_CodeGenerator_Php_Class
  235. */
  236. public function setProperties(Array $properties)
  237. {
  238. foreach ($properties as $property) {
  239. $this->setProperty($property);
  240. }
  241. return $this;
  242. }
  243. /**
  244. * setProperty()
  245. *
  246. * @param array|Zend_CodeGenerator_Php_Property $property
  247. * @return Zend_CodeGenerator_Php_Class
  248. */
  249. public function setProperty($property)
  250. {
  251. if (is_array($property)) {
  252. $property = new Zend_CodeGenerator_Php_Property($property);
  253. $propertyName = $property->getName();
  254. } elseif ($property instanceof Zend_CodeGenerator_Php_Property) {
  255. $propertyName = $property->getName();
  256. } else {
  257. require_once 'Zend/CodeGenerator/Php/Exception.php';
  258. throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
  259. }
  260. if (isset($this->_properties[$propertyName])) {
  261. require_once 'Zend/CodeGenerator/Php/Exception.php';
  262. throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
  263. }
  264. $this->_properties[$propertyName] = $property;
  265. return $this;
  266. }
  267. /**
  268. * getProperties()
  269. *
  270. * @return array
  271. */
  272. public function getProperties()
  273. {
  274. return $this->_properties;
  275. }
  276. /**
  277. * getProperty()
  278. *
  279. * @param string $propertyName
  280. * @return Zend_CodeGenerator_Php_Property
  281. */
  282. public function getProperty($propertyName)
  283. {
  284. foreach ($this->_properties as $property) {
  285. if ($property->getName() == $propertyName) {
  286. return $property;
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * hasProperty()
  293. *
  294. * @param string $propertyName
  295. * @return bool
  296. */
  297. public function hasProperty($propertyName)
  298. {
  299. return isset($this->_properties[$propertyName]);
  300. }
  301. /**
  302. * setMethods()
  303. *
  304. * @param array $methods
  305. * @return Zend_CodeGenerator_Php_Class
  306. */
  307. public function setMethods(Array $methods)
  308. {
  309. foreach ($methods as $method) {
  310. $this->setMethod($method);
  311. }
  312. return $this;
  313. }
  314. /**
  315. * setMethod()
  316. *
  317. * @param array|Zend_CodeGenerator_Php_Method $method
  318. * @return Zend_CodeGenerator_Php_Class
  319. */
  320. public function setMethod($method)
  321. {
  322. if (is_array($method)) {
  323. $method = new Zend_CodeGenerator_Php_Method($method);
  324. $methodName = $method->getName();
  325. } elseif ($method instanceof Zend_CodeGenerator_Php_Method) {
  326. $methodName = $method->getName();
  327. } else {
  328. require_once 'Zend/CodeGenerator/Php/Exception.php';
  329. throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method');
  330. }
  331. if (isset($this->_methods[$methodName])) {
  332. require_once 'Zend/CodeGenerator/Php/Exception.php';
  333. throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
  334. }
  335. $this->_methods[$methodName] = $method;
  336. return $this;
  337. }
  338. /**
  339. * getMethods()
  340. *
  341. * @return array
  342. */
  343. public function getMethods()
  344. {
  345. return $this->_methods;
  346. }
  347. /**
  348. * getMethod()
  349. *
  350. * @param string $methodName
  351. * @return Zend_CodeGenerator_Php_Method
  352. */
  353. public function getMethod($methodName)
  354. {
  355. foreach ($this->_methods as $method) {
  356. if ($method->getName() == $methodName) {
  357. return $method;
  358. }
  359. }
  360. return false;
  361. }
  362. /**
  363. * hasMethod()
  364. *
  365. * @param string $methodName
  366. * @return bool
  367. */
  368. public function hasMethod($methodName)
  369. {
  370. return isset($this->_methods[$methodName]);
  371. }
  372. /**
  373. * isSourceDirty()
  374. *
  375. * @return bool
  376. */
  377. public function isSourceDirty()
  378. {
  379. if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
  380. return true;
  381. }
  382. foreach ($this->_properties as $property) {
  383. if ($property->isSourceDirty()) {
  384. return true;
  385. }
  386. }
  387. foreach ($this->_methods as $method) {
  388. if ($method->isSourceDirty()) {
  389. return true;
  390. }
  391. }
  392. return parent::isSourceDirty();
  393. }
  394. /**
  395. * generate()
  396. *
  397. * @return string
  398. */
  399. public function generate()
  400. {
  401. if (!$this->isSourceDirty()) {
  402. return $this->getSourceContent();
  403. }
  404. $output = '';
  405. if (null !== ($docblock = $this->getDocblock())) {
  406. $docblock->setIndentation('');
  407. $output .= $docblock->generate();
  408. }
  409. if ($this->isAbstract()) {
  410. $output .= 'abstract ';
  411. }
  412. $output .= 'class ' . $this->getName();
  413. if ( !empty( $this->_extendedClass) ) {
  414. $output .= ' extends ' . $this->_extendedClass;
  415. }
  416. $implemented = $this->getImplementedInterfaces();
  417. if (!empty($implemented)) {
  418. $output .= ' implements ' . implode(', ', $implemented);
  419. }
  420. $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
  421. $properties = $this->getProperties();
  422. if (!empty($properties)) {
  423. foreach ($properties as $property) {
  424. $output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
  425. }
  426. }
  427. $methods = $this->getMethods();
  428. if (!empty($methods)) {
  429. foreach ($methods as $method) {
  430. $output .= $method->generate() . self::LINE_FEED;
  431. }
  432. }
  433. $output .= self::LINE_FEED . '}' . self::LINE_FEED;
  434. return $output;
  435. }
  436. /**
  437. * _init() - is called at construction time
  438. *
  439. */
  440. protected function _init()
  441. {
  442. $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
  443. $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
  444. }
  445. }