[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Reflection / Docblock.php
blob0706d6e858225521b60d1858e70845540930f86a
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Reflection
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
22 /**
23 * @see Zend_Reflection_Docblock_Tag
25 require_once 'Zend/Reflection/Docblock/Tag.php';
27 /**
28 * @category Zend
29 * @package Zend_Reflection
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Reflection_Docblock implements Reflector
35 /**
36 * @var Reflector
38 protected $_reflector = null;
40 /**#@+
41 * @var int
43 protected $_startLine = null;
44 protected $_endLine = null;
45 /**#@-*/
47 /**
48 * @var string
50 protected $_docComment = null;
52 /**
53 * @var string
55 protected $_cleanDocComment = null;
57 /**
58 * @var string
60 protected $_longDescription = null;
62 /**
63 * @var string
65 protected $_shortDescription = null;
67 /**
68 * @var array
70 protected $_tags = array();
72 /**
73 * Export reflection
75 * Reqired by the Reflector interface.
77 * @todo What should this do?
78 * @return void
80 public static function export()
85 /**
86 * Serialize to string
88 * Required by the Reflector interface
90 * @todo What should this return?
91 * @return string
93 public function __toString()
95 $str = "Docblock [ /* Docblock */ ] {".PHP_EOL.PHP_EOL;
96 $str .= " - Tags [".count($this->_tags)."] {".PHP_EOL;
98 foreach($this->_tags AS $tag) {
99 $str .= " ".$tag;
102 $str .= " }".PHP_EOL;
103 $str .= "}".PHP_EOL;
105 return $str;
109 * Constructor
111 * @param Reflector|string $commentOrReflector
113 public function __construct($commentOrReflector)
115 if ($commentOrReflector instanceof Reflector) {
116 $this->_reflector = $commentOrReflector;
117 if (!method_exists($commentOrReflector, 'getDocComment')) {
118 require_once 'Zend/Reflection/Exception.php';
119 throw new Zend_Reflection_Exception('Reflector must contain method "getDocComment"');
121 $docComment = $commentOrReflector->getDocComment();
123 $lineCount = substr_count($docComment, "\n");
125 $this->_startLine = $this->_reflector->getStartLine() - $lineCount - 1;
126 $this->_endLine = $this->_reflector->getStartLine() - 1;
128 } elseif (is_string($commentOrReflector)) {
129 $docComment = $commentOrReflector;
130 } else {
131 require_once 'Zend/Reflection/Exception.php';
132 throw new Zend_Reflection_Exception(get_class($this) . ' must have a (string) DocComment or a Reflector in the constructor');
135 if ($docComment == '') {
136 require_once 'Zend/Reflection/Exception.php';
137 throw new Zend_Reflection_Exception('DocComment cannot be empty');
140 $this->_docComment = $docComment;
141 $this->_parse();
145 * Retrieve contents of docblock
147 * @return string
149 public function getContents()
151 return $this->_cleanDocComment;
155 * Get start line (position) of docblock
157 * @return int
159 public function getStartLine()
161 return $this->_startLine;
165 * Get last line (position) of docblock
167 * @return int
169 public function getEndLine()
171 return $this->_endLine;
175 * Get docblock short description
177 * @return string
179 public function getShortDescription()
181 return $this->_shortDescription;
185 * Get docblock long description
187 * @return string
189 public function getLongDescription()
191 return $this->_longDescription;
195 * Does the docblock contain the given annotation tag?
197 * @param string $name
198 * @return bool
200 public function hasTag($name)
202 foreach ($this->_tags as $tag) {
203 if ($tag->getName() == $name) {
204 return true;
207 return false;
211 * Retrieve the given docblock tag
213 * @param string $name
214 * @return Zend_Reflection_Docblock_Tag|false
216 public function getTag($name)
218 foreach ($this->_tags as $tag) {
219 if ($tag->getName() == $name) {
220 return $tag;
224 return false;
228 * Get all docblock annotation tags
230 * @param string $filter
231 * @return array Array of Zend_Reflection_Docblock_Tag
233 public function getTags($filter = null)
235 if ($filter === null || !is_string($filter)) {
236 return $this->_tags;
239 $returnTags = array();
240 foreach ($this->_tags as $tag) {
241 if ($tag->getName() == $filter) {
242 $returnTags[] = $tag;
245 return $returnTags;
249 * Parse the docblock
251 * @return void
253 protected function _parse()
255 $docComment = $this->_docComment;
257 // First remove doc block line starters
258 $docComment = preg_replace('#[ \t]*(?:\/\*\*|\*\/|\*)?[ ]{0,1}(.*)?#', '$1', $docComment);
259 $docComment = ltrim($docComment, "\r\n"); // @todo should be changed to remove first and last empty line
261 $this->_cleanDocComment = $docComment;
263 // Next parse out the tags and descriptions
264 $parsedDocComment = $docComment;
265 $lineNumber = $firstBlandLineEncountered = 0;
266 while (($newlinePos = strpos($parsedDocComment, "\n")) !== false) {
267 $lineNumber++;
268 $line = substr($parsedDocComment, 0, $newlinePos);
270 $matches = array();
272 if ((strpos($line, '@') === 0) && (preg_match('#^(@\w+.*?)(\n)(?:@|\r?\n|$)#s', $parsedDocComment, $matches))) {
273 $this->_tags[] = Zend_Reflection_Docblock_Tag::factory($matches[1]);
274 $parsedDocComment = str_replace($matches[1] . $matches[2], '', $parsedDocComment);
275 } else {
276 if ($lineNumber < 3 && !$firstBlandLineEncountered) {
277 $this->_shortDescription .= $line . "\n";
278 } else {
279 $this->_longDescription .= $line . "\n";
282 if ($line == '') {
283 $firstBlandLineEncountered = true;
286 $parsedDocComment = substr($parsedDocComment, $newlinePos + 1);
291 $this->_shortDescription = rtrim($this->_shortDescription);
292 $this->_longDescription = rtrim($this->_longDescription);