2
0

Docblock.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_Reflection
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Reflection_Docblock_Tag
  23. */
  24. require_once 'Zend/Reflection/Docblock/Tag.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Reflection
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Reflection_Docblock implements Reflector
  32. {
  33. /**
  34. * @var Reflector
  35. */
  36. protected $_reflector = null;
  37. /**#@+
  38. * @var int
  39. */
  40. protected $_startLine = null;
  41. protected $_endLine = null;
  42. /**#@-*/
  43. /**
  44. * @var string
  45. */
  46. protected $_docComment = null;
  47. /**
  48. * @var string
  49. */
  50. protected $_cleanDocComment = null;
  51. /**
  52. * @var string
  53. */
  54. protected $_longDescription = null;
  55. /**
  56. * @var string
  57. */
  58. protected $_shortDescription = null;
  59. /**
  60. * @var array
  61. */
  62. protected $_tags = array();
  63. /**
  64. * Export reflection
  65. *
  66. * Reqired by the Reflector interface.
  67. *
  68. * @todo What should this do?
  69. * @return void
  70. */
  71. public static function export()
  72. {
  73. }
  74. /**
  75. * Serialize to string
  76. *
  77. * Required by the Reflector interface
  78. *
  79. * @todo What should this return?
  80. * @return string
  81. */
  82. public function __toString()
  83. {
  84. }
  85. /**
  86. * Constructor
  87. *
  88. * @param Reflector|string $commentOrReflector
  89. */
  90. public function __construct($commentOrReflector)
  91. {
  92. if ($commentOrReflector instanceof Reflector) {
  93. $this->_reflector = $commentOrReflector;
  94. if (!method_exists($commentOrReflector, 'getDocComment')) {
  95. require_once 'Zend/Reflection/Exception.php';
  96. throw new Zend_Reflection_Exception('Reflector must contain method "getDocComment"');
  97. }
  98. $docComment = $commentOrReflector->getDocComment();
  99. $lineCount = substr_count($docComment, "\n");
  100. $this->_startLine = $this->_reflector->getStartLine() - $lineCount - 1;
  101. $this->_endLine = $this->_reflector->getStartLine() - 1;
  102. } elseif (is_string($commentOrReflector)) {
  103. $docComment = $commentOrReflector;
  104. } else {
  105. require_once 'Zend/Reflection/Exception.php';
  106. throw new Zend_Reflection_Exception(get_class($this) . ' must have a (string) DocComment or a Reflector in the constructor');
  107. }
  108. if ($docComment == '') {
  109. require_once 'Zend/Reflection/Exception.php';
  110. throw new Zend_Reflection_Exception('DocComment cannot be empty');
  111. }
  112. $this->_docComment = $docComment;
  113. $this->_parse();
  114. }
  115. /**
  116. * Retrieve contents of docblock
  117. *
  118. * @return string
  119. */
  120. public function getContents()
  121. {
  122. return $this->_cleanDocComment;
  123. }
  124. /**
  125. * Get start line (position) of docblock
  126. *
  127. * @return int
  128. */
  129. public function getStartLine()
  130. {
  131. return $this->_startLine;
  132. }
  133. /**
  134. * Get last line (position) of docblock
  135. *
  136. * @return int
  137. */
  138. public function getEndLine()
  139. {
  140. return $this->_endLine;
  141. }
  142. /**
  143. * Get docblock short description
  144. *
  145. * @return string
  146. */
  147. public function getShortDescription()
  148. {
  149. return $this->_shortDescription;
  150. }
  151. /**
  152. * Get docblock long description
  153. *
  154. * @return string
  155. */
  156. public function getLongDescription()
  157. {
  158. return $this->_longDescription;
  159. }
  160. /**
  161. * Does the docblock contain the given annotation tag?
  162. *
  163. * @param string $name
  164. * @return bool
  165. */
  166. public function hasTag($name)
  167. {
  168. foreach ($this->_tags as $tag) {
  169. if ($tag->getName() == $name) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. /**
  176. * Retrieve the given docblock tag
  177. *
  178. * @param string $name
  179. * @return Zend_Reflection_Docblock_Tag|false
  180. */
  181. public function getTag($name)
  182. {
  183. foreach ($this->_tags as $tag) {
  184. if ($tag->getName() == $name) {
  185. return $tag;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * Get all docblock annotation tags
  192. *
  193. * @param string $filter
  194. * @return array Array of Zend_Reflection_Docblock_Tag
  195. */
  196. public function getTags($filter = null)
  197. {
  198. if ($filter === null || !is_string($filter)) {
  199. return $this->_tags;
  200. }
  201. $returnTags = array();
  202. foreach ($this->_tags as $tag) {
  203. if ($tag->getName() == $filter) {
  204. $returnTags[] = $tag;
  205. }
  206. }
  207. return $returnTags;
  208. }
  209. /**
  210. * Parse the docblock
  211. *
  212. * @return void
  213. */
  214. protected function _parse()
  215. {
  216. $docComment = $this->_docComment;
  217. // First remove doc block line starters
  218. $docComment = preg_replace('#[ \t]*(?:\/\*\*|\*\/|\*)?[ ]{0,1}(.*)?#', '$1', $docComment);
  219. $docComment = ltrim($docComment, "\r\n"); // @todo should be changed to remove first and last empty line
  220. $this->_cleanDocComment = $docComment;
  221. // Next parse out the tags and descriptions
  222. $parsedDocComment = $docComment;
  223. $lineNumber = $firstBlandLineEncountered = 0;
  224. while (($newlinePos = strpos($parsedDocComment, "\n")) !== false) {
  225. $lineNumber++;
  226. $line = substr($parsedDocComment, 0, $newlinePos);
  227. $matches = array();
  228. if ((strpos($line, '@') === 0) && (preg_match('#^(@\w+.*?)(\n)(?:@|\r?\n|$)#s', $parsedDocComment, $matches))) {
  229. $this->_tags[] = Zend_Reflection_Docblock_Tag::factory($matches[1]);
  230. $parsedDocComment = str_replace($matches[1] . $matches[2], '', $parsedDocComment);
  231. } else {
  232. if ($lineNumber < 3 && !$firstBlandLineEncountered) {
  233. $this->_shortDescription .= $line . "\n";
  234. } else {
  235. $this->_longDescription .= $line . "\n";
  236. }
  237. if ($line == '') {
  238. $firstBlandLineEncountered = true;
  239. }
  240. $parsedDocComment = substr($parsedDocComment, $newlinePos + 1);
  241. }
  242. }
  243. $this->_shortDescription = rtrim($this->_shortDescription);
  244. $this->_longDescription = rtrim($this->_longDescription);
  245. }
  246. }