Docblock.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2014 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_Docblock_Tag
  28. */
  29. require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_CodeGenerator
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
  37. {
  38. /**
  39. * @var string
  40. */
  41. protected $_shortDescription = null;
  42. /**
  43. * @var string
  44. */
  45. protected $_longDescription = null;
  46. /**
  47. * @var array
  48. */
  49. protected $_tags = array();
  50. /**
  51. * @var string
  52. */
  53. protected $_indentation = '';
  54. /**
  55. * fromReflection() - Build a docblock generator object from a reflection object
  56. *
  57. * @param Zend_Reflection_Docblock $reflectionDocblock
  58. * @return Zend_CodeGenerator_Php_Docblock
  59. */
  60. public static function fromReflection(Zend_Reflection_Docblock $reflectionDocblock)
  61. {
  62. $docblock = new self();
  63. $docblock->setSourceContent($reflectionDocblock->getContents());
  64. $docblock->setSourceDirty(false);
  65. $docblock->setShortDescription($reflectionDocblock->getShortDescription());
  66. $docblock->setLongDescription($reflectionDocblock->getLongDescription());
  67. foreach ($reflectionDocblock->getTags() as $tag) {
  68. $docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag::fromReflection($tag));
  69. }
  70. return $docblock;
  71. }
  72. /**
  73. * setShortDescription()
  74. *
  75. * @param string $shortDescription
  76. * @return Zend_CodeGenerator_Php_Docblock
  77. */
  78. public function setShortDescription($shortDescription)
  79. {
  80. $this->_shortDescription = $shortDescription;
  81. return $this;
  82. }
  83. /**
  84. * getShortDescription()
  85. *
  86. * @return string
  87. */
  88. public function getShortDescription()
  89. {
  90. return $this->_shortDescription;
  91. }
  92. /**
  93. * setLongDescription()
  94. *
  95. * @param string $longDescription
  96. * @return Zend_CodeGenerator_Php_Docblock
  97. */
  98. public function setLongDescription($longDescription)
  99. {
  100. $this->_longDescription = $longDescription;
  101. return $this;
  102. }
  103. /**
  104. * getLongDescription()
  105. *
  106. * @return string
  107. */
  108. public function getLongDescription()
  109. {
  110. return $this->_longDescription;
  111. }
  112. /**
  113. * setTags()
  114. *
  115. * @param array $tags
  116. * @return Zend_CodeGenerator_Php_Docblock
  117. */
  118. public function setTags(Array $tags)
  119. {
  120. foreach ($tags as $tag) {
  121. $this->setTag($tag);
  122. }
  123. return $this;
  124. }
  125. /**
  126. * setTag()
  127. *
  128. * @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
  129. * @return Zend_CodeGenerator_Php_Docblock
  130. */
  131. public function setTag($tag)
  132. {
  133. if (is_array($tag)) {
  134. $tag = new Zend_CodeGenerator_Php_Docblock_Tag($tag);
  135. } elseif (!$tag instanceof Zend_CodeGenerator_Php_Docblock_Tag) {
  136. require_once 'Zend/CodeGenerator/Php/Exception.php';
  137. throw new Zend_CodeGenerator_Php_Exception(
  138. 'setTag() expects either an array of method options or an '
  139. . 'instance of Zend_CodeGenerator_Php_Docblock_Tag'
  140. );
  141. }
  142. $this->_tags[] = $tag;
  143. return $this;
  144. }
  145. /**
  146. * getTags
  147. *
  148. * @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
  149. */
  150. public function getTags()
  151. {
  152. return $this->_tags;
  153. }
  154. /**
  155. * generate()
  156. *
  157. * @return string
  158. */
  159. public function generate()
  160. {
  161. if (!$this->isSourceDirty()) {
  162. return $this->_docCommentize($this->getSourceContent());
  163. }
  164. $output = '';
  165. if (null !== ($sd = $this->getShortDescription())) {
  166. $output .= $sd . self::LINE_FEED . self::LINE_FEED;
  167. }
  168. if (null !== ($ld = $this->getLongDescription())) {
  169. $output .= $ld . self::LINE_FEED . self::LINE_FEED;
  170. }
  171. foreach ($this->getTags() as $tag) {
  172. $output .= $tag->generate() . self::LINE_FEED;
  173. }
  174. return $this->_docCommentize(trim($output));
  175. }
  176. /**
  177. * _docCommentize()
  178. *
  179. * @param string $content
  180. * @return string
  181. */
  182. protected function _docCommentize($content)
  183. {
  184. $indent = $this->getIndentation();
  185. $output = $indent . '/**' . self::LINE_FEED;
  186. $content = wordwrap($content, 80, self::LINE_FEED);
  187. $lines = explode(self::LINE_FEED, $content);
  188. foreach ($lines as $line) {
  189. $output .= $indent . ' *';
  190. if ($line) {
  191. $output .= " $line";
  192. }
  193. $output .= self::LINE_FEED;
  194. }
  195. $output = rtrim($output, ' *' . self::LINE_FEED) . self::LINE_FEED;
  196. $output .= $indent . ' */' . self::LINE_FEED;
  197. return $output;
  198. }
  199. }