argument registration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / DocCommentParser.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * @package FLOW3
25 * @subpackage Reflection
26 * @version $Id: DocCommentParser.php 1966 2009-03-03 13:46:17Z k-fish $
27 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
28 */
29
30 /**
31 * A little parser which creates tag objects from doc comments
32 *
33 * @package FLOW3
34 * @subpackage Reflection
35 * @version $Id: DocCommentParser.php 1966 2009-03-03 13:46:17Z k-fish $
36 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
37 */
38 class Tx_ExtBase_Reflection_DocCommentParser {
39
40 /**
41 * @var string The description as found in the doc comment
42 */
43 protected $description = '';
44
45 /**
46 * @var array An array of tag names and their values (multiple values are possible)
47 */
48 protected $tags = array();
49
50 /**
51 * Parses the given doc comment and saves the result (description and
52 * tags) in the parser's object. They can be retrieved by the
53 * getTags() getTagValues() and getDescription() methods.
54 *
55 * @param string $docComment A doc comment as returned by the reflection getDocComment() method
56 * @return void
57 * @author Robert Lemke <robert@typo3.org>
58 */
59 public function parseDocComment($docComment) {
60 $this->description = '';
61 $this->tags = array();
62
63 $lines = explode(chr(10), $docComment);
64 foreach ($lines as $line) {
65 if (strlen($line) > 0 && strpos($line, '@') !== FALSE) {
66 $this->parseTag(substr($line, strpos($line, '@')));
67 } else if (count($this->tags) === 0) {
68 $this->description .= preg_replace('/\s*\\/?[\\\\*]*(.*)$/', '$1', $line) . chr(10);
69 }
70 }
71 $this->description = trim($this->description);
72 }
73
74 /**
75 * Returns the tags which have been previously parsed
76 *
77 * @return array Array of tag names and their (multiple) values
78 * @author Robert Lemke <robert@typo3.org>
79 */
80 public function getTagsValues() {
81 return $this->tags;
82 }
83
84 /**
85 * Returns the values of the specified tag. The doc comment
86 * must be parsed with parseDocComment() before tags are
87 * available.
88 *
89 * @param string $tagName The tag name to retrieve the values for
90 * @return array The tag's values
91 * @author Robert Lemke <robert@typo3.org>
92 */
93 public function getTagValues($tagName) {
94 if (!$this->isTaggedWith($tagName)) throw new RuntimeException('Tag "' . $tagName . '" does not exist.', 1169128255);
95 return $this->tags[$tagName];
96 }
97
98 /**
99 * Checks if a tag with the given name exists
100 *
101 * @param string $tagName The tag name to check for
102 * @return boolean TRUE the tag exists, otherwise FALSE
103 * @author Robert Lemke <robert@typo3.org>
104 */
105 public function isTaggedWith($tagName) {
106 return (isset($this->tags[$tagName]));
107 }
108
109 /**
110 * Returns the description which has been previously parsed
111 *
112 * @return string The description which has been parsed
113 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
114 */
115 public function getDescription() {
116 return $this->description;
117 }
118
119 /**
120 * Parses a line of a doc comment for a tag and its value.
121 * The result is stored in the interal tags array.
122 *
123 * @param string $line A line of a doc comment which starts with an @-sign
124 * @return void
125 * @author Robert Lemke <robert@typo3.org>
126 */
127 protected function parseTag($line) {
128 $tagAndValue = preg_split('/\s/', $line, 2);
129 $tag = substr($tagAndValue[0], 1);
130 if (count($tagAndValue) > 1) {
131 $this->tags[$tag][] = trim($tagAndValue[1]);
132 } else {
133 $this->tags[$tag] = array();
134 }
135 }
136 }
137
138 ?>