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