PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/common/lib/Zend/Reflection/Docblock.php

https://bitbucket.org/haichau59/manga
PHP | 294 lines | 129 code | 40 blank | 125 comment | 22 complexity | 49ad2786d3920e0316ef86963750d430 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_Reflection
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Docblock.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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. $str = "Docblock [ /* Docblock */ ] {".PHP_EOL.PHP_EOL;
  85. $str .= " - Tags [".count($this->_tags)."] {".PHP_EOL;
  86. foreach($this->_tags AS $tag) {
  87. $str .= " ".$tag;
  88. }
  89. $str .= " }".PHP_EOL;
  90. $str .= "}".PHP_EOL;
  91. return $str;
  92. }
  93. /**
  94. * Constructor
  95. *
  96. * @param Reflector|string $commentOrReflector
  97. */
  98. public function __construct($commentOrReflector)
  99. {
  100. if ($commentOrReflector instanceof Reflector) {
  101. $this->_reflector = $commentOrReflector;
  102. if (!method_exists($commentOrReflector, 'getDocComment')) {
  103. //require_once 'Zend/Reflection/Exception.php';
  104. throw new Zend_Reflection_Exception('Reflector must contain method "getDocComment"');
  105. }
  106. $docComment = $commentOrReflector->getDocComment();
  107. $lineCount = substr_count($docComment, "\n");
  108. $this->_startLine = $this->_reflector->getStartLine() - $lineCount - 1;
  109. $this->_endLine = $this->_reflector->getStartLine() - 1;
  110. } elseif (is_string($commentOrReflector)) {
  111. $docComment = $commentOrReflector;
  112. } else {
  113. //require_once 'Zend/Reflection/Exception.php';
  114. throw new Zend_Reflection_Exception(get_class($this) . ' must have a (string) DocComment or a Reflector in the constructor');
  115. }
  116. if ($docComment == '') {
  117. //require_once 'Zend/Reflection/Exception.php';
  118. throw new Zend_Reflection_Exception('DocComment cannot be empty');
  119. }
  120. $this->_docComment = $docComment;
  121. $this->_parse();
  122. }
  123. /**
  124. * Retrieve contents of docblock
  125. *
  126. * @return string
  127. */
  128. public function getContents()
  129. {
  130. return $this->_cleanDocComment;
  131. }
  132. /**
  133. * Get start line (position) of docblock
  134. *
  135. * @return int
  136. */
  137. public function getStartLine()
  138. {
  139. return $this->_startLine;
  140. }
  141. /**
  142. * Get last line (position) of docblock
  143. *
  144. * @return int
  145. */
  146. public function getEndLine()
  147. {
  148. return $this->_endLine;
  149. }
  150. /**
  151. * Get docblock short description
  152. *
  153. * @return string
  154. */
  155. public function getShortDescription()
  156. {
  157. return $this->_shortDescription;
  158. }
  159. /**
  160. * Get docblock long description
  161. *
  162. * @return string
  163. */
  164. public function getLongDescription()
  165. {
  166. return $this->_longDescription;
  167. }
  168. /**
  169. * Does the docblock contain the given annotation tag?
  170. *
  171. * @param string $name
  172. * @return bool
  173. */
  174. public function hasTag($name)
  175. {
  176. foreach ($this->_tags as $tag) {
  177. if ($tag->getName() == $name) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. * Retrieve the given docblock tag
  185. *
  186. * @param string $name
  187. * @return Zend_Reflection_Docblock_Tag|false
  188. */
  189. public function getTag($name)
  190. {
  191. foreach ($this->_tags as $tag) {
  192. if ($tag->getName() == $name) {
  193. return $tag;
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * Get all docblock annotation tags
  200. *
  201. * @param string $filter
  202. * @return array Array of Zend_Reflection_Docblock_Tag
  203. */
  204. public function getTags($filter = null)
  205. {
  206. if ($filter === null || !is_string($filter)) {
  207. return $this->_tags;
  208. }
  209. $returnTags = array();
  210. foreach ($this->_tags as $tag) {
  211. if ($tag->getName() == $filter) {
  212. $returnTags[] = $tag;
  213. }
  214. }
  215. return $returnTags;
  216. }
  217. /**
  218. * Parse the docblock
  219. *
  220. * @return void
  221. */
  222. protected function _parse()
  223. {
  224. $docComment = $this->_docComment;
  225. // First remove doc block line starters
  226. $docComment = preg_replace('#[ \t]*(?:\/\*\*|\*\/|\*)?[ ]{0,1}(.*)?#', '$1', $docComment);
  227. $docComment = ltrim($docComment, "\r\n"); // @todo should be changed to remove first and last empty line
  228. $this->_cleanDocComment = $docComment;
  229. // Next parse out the tags and descriptions
  230. $parsedDocComment = $docComment;
  231. $lineNumber = $firstBlandLineEncountered = 0;
  232. while (($newlinePos = strpos($parsedDocComment, "\n")) !== false) {
  233. $lineNumber++;
  234. $line = substr($parsedDocComment, 0, $newlinePos);
  235. $matches = array();
  236. if ((strpos($line, '@') === 0) && (preg_match('#^(@\w+.*?)(\n)(?:@|\r?\n|$)#s', $parsedDocComment, $matches))) {
  237. $this->_tags[] = Zend_Reflection_Docblock_Tag::factory($matches[1]);
  238. $parsedDocComment = str_replace($matches[1] . $matches[2], '', $parsedDocComment);
  239. } else {
  240. if ($lineNumber < 3 && !$firstBlandLineEncountered) {
  241. $this->_shortDescription .= $line . "\n";
  242. } else {
  243. $this->_longDescription .= $line . "\n";
  244. }
  245. if ($line == '') {
  246. $firstBlandLineEncountered = true;
  247. }
  248. $parsedDocComment = substr($parsedDocComment, $newlinePos + 1);
  249. }
  250. }
  251. $this->_shortDescription = rtrim($this->_shortDescription);
  252. $this->_longDescription = rtrim($this->_longDescription);
  253. }
  254. }