Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Reflection;
|
Chris@0
|
21
|
Chris@0
|
22 use Doctrine\Common\Annotations\TokenParser;
|
Chris@0
|
23 use ReflectionException;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Parses a file for namespaces/use/class declarations.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @author Karoly Negyesi <karoly@negyesi.net>
|
Chris@0
|
29 */
|
Chris@0
|
30 class StaticReflectionParser implements ReflectionProviderInterface
|
Chris@0
|
31 {
|
Chris@0
|
32 /**
|
Chris@0
|
33 * The fully qualified class name.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var string
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $className;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The short class name.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $shortClassName;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Whether the caller only wants class annotations.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @var boolean.
|
Chris@0
|
50 */
|
Chris@0
|
51 protected $classAnnotationOptimize;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Whether the parser has run.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @var boolean
|
Chris@0
|
57 */
|
Chris@0
|
58 protected $parsed = false;
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * The namespace of the class.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @var string
|
Chris@0
|
64 */
|
Chris@0
|
65 protected $namespace = '';
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * The use statements of the class.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @var array
|
Chris@0
|
71 */
|
Chris@0
|
72 protected $useStatements = [];
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * The docComment of the class.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @var string
|
Chris@0
|
78 */
|
Chris@0
|
79 protected $docComment = [
|
Chris@0
|
80 'class' => '',
|
Chris@0
|
81 'property' => [],
|
Chris@0
|
82 'method' => []
|
Chris@0
|
83 ];
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * The name of the class this class extends, if any.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @var string
|
Chris@0
|
89 */
|
Chris@0
|
90 protected $parentClassName = '';
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * The parent PSR-0 Parser.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @var \Doctrine\Common\Reflection\StaticReflectionParser
|
Chris@0
|
96 */
|
Chris@0
|
97 protected $parentStaticReflectionParser;
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Parses a class residing in a PSR-0 hierarchy.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @param string $className The full, namespaced class name.
|
Chris@0
|
103 * @param ClassFinderInterface $finder A ClassFinder object which finds the class.
|
Chris@0
|
104 * @param boolean $classAnnotationOptimize Only retrieve the class docComment.
|
Chris@0
|
105 * Presumes there is only one statement per line.
|
Chris@0
|
106 */
|
Chris@0
|
107 public function __construct($className, $finder, $classAnnotationOptimize = false)
|
Chris@0
|
108 {
|
Chris@0
|
109 $this->className = ltrim($className, '\\');
|
Chris@0
|
110 $lastNsPos = strrpos($this->className, '\\');
|
Chris@0
|
111
|
Chris@0
|
112 if ($lastNsPos !== false) {
|
Chris@0
|
113 $this->namespace = substr($this->className, 0, $lastNsPos);
|
Chris@0
|
114 $this->shortClassName = substr($this->className, $lastNsPos + 1);
|
Chris@0
|
115 } else {
|
Chris@0
|
116 $this->shortClassName = $this->className;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 $this->finder = $finder;
|
Chris@0
|
120 $this->classAnnotationOptimize = $classAnnotationOptimize;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * @return void
|
Chris@0
|
125 */
|
Chris@0
|
126 protected function parse()
|
Chris@0
|
127 {
|
Chris@0
|
128 if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
|
Chris@0
|
129 return;
|
Chris@0
|
130 }
|
Chris@0
|
131 $this->parsed = true;
|
Chris@0
|
132 $contents = file_get_contents($fileName);
|
Chris@0
|
133 if ($this->classAnnotationOptimize) {
|
Chris@0
|
134 if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
|
Chris@0
|
135 $contents = $matches[0];
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138 $tokenParser = new TokenParser($contents);
|
Chris@0
|
139 $docComment = '';
|
Chris@12
|
140 $last_token = false;
|
Chris@12
|
141
|
Chris@0
|
142 while ($token = $tokenParser->next(false)) {
|
Chris@12
|
143 if (is_array($token)) {switch ($token[0]) {
|
Chris@12
|
144 case T_USE:
|
Chris@12
|
145 $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
|
Chris@12
|
146 break;
|
Chris@12
|
147 case T_DOC_COMMENT:
|
Chris@12
|
148 $docComment = $token[1];
|
Chris@12
|
149 break;
|
Chris@12
|
150 case T_CLASS:
|
Chris@12
|
151 if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM) {$this->docComment['class'] = $docComment;
|
Chris@12
|
152 $docComment = '';}
|
Chris@12
|
153 break;
|
Chris@12
|
154 case T_VAR:
|
Chris@12
|
155 case T_PRIVATE:
|
Chris@12
|
156 case T_PROTECTED:
|
Chris@12
|
157 case T_PUBLIC:
|
Chris@12
|
158 $token = $tokenParser->next();
|
Chris@12
|
159 if ($token[0] === T_VARIABLE) {
|
Chris@12
|
160 $propertyName = substr($token[1], 1);
|
Chris@12
|
161 $this->docComment['property'][$propertyName] = $docComment;
|
Chris@12
|
162 continue 2;
|
Chris@12
|
163 }
|
Chris@12
|
164 if ($token[0] !== T_FUNCTION) {
|
Chris@12
|
165 // For example, it can be T_FINAL.
|
Chris@12
|
166 continue 2;
|
Chris@12
|
167 }
|
Chris@12
|
168 // No break.
|
Chris@12
|
169 case T_FUNCTION:
|
Chris@12
|
170 // The next string after function is the name, but
|
Chris@12
|
171 // there can be & before the function name so find the
|
Chris@12
|
172 // string.
|
Chris@12
|
173 while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
|
Chris@12
|
174 $methodName = $token[1];
|
Chris@12
|
175 $this->docComment['method'][$methodName] = $docComment;
|
Chris@12
|
176 $docComment = '';
|
Chris@12
|
177 break;
|
Chris@12
|
178 case T_EXTENDS:
|
Chris@12
|
179 $this->parentClassName = $tokenParser->parseClass();
|
Chris@12
|
180 $nsPos = strpos($this->parentClassName, '\\');
|
Chris@12
|
181 $fullySpecified = false;
|
Chris@12
|
182 if ($nsPos === 0) {
|
Chris@12
|
183 $fullySpecified = true;
|
Chris@12
|
184 } else {
|
Chris@12
|
185 if ($nsPos) {
|
Chris@12
|
186 $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
|
Chris@12
|
187 $postfix = substr($this->parentClassName, $nsPos);
|
Chris@12
|
188 } else {
|
Chris@12
|
189 $prefix = strtolower($this->parentClassName);
|
Chris@12
|
190 $postfix = '';
|
Chris@0
|
191 }
|
Chris@12
|
192 foreach ($this->useStatements as $alias => $use) {
|
Chris@12
|
193 if ($alias == $prefix) {
|
Chris@12
|
194 $this->parentClassName = '\\' . $use . $postfix;
|
Chris@12
|
195 $fullySpecified = true;
|
Chris@12
|
196 }
|
Chris@0
|
197 }
|
Chris@12
|
198 }
|
Chris@12
|
199 if (!$fullySpecified) {
|
Chris@12
|
200 $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
|
Chris@12
|
201 }
|
Chris@12
|
202 break;}
|
Chris@0
|
203 }
|
Chris@12
|
204
|
Chris@12
|
205 $last_token = $token[0];
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 /**
|
Chris@0
|
210 * @return StaticReflectionParser
|
Chris@0
|
211 */
|
Chris@0
|
212 protected function getParentStaticReflectionParser()
|
Chris@0
|
213 {
|
Chris@0
|
214 if (empty($this->parentStaticReflectionParser)) {
|
Chris@0
|
215 $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 return $this->parentStaticReflectionParser;
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * @return string
|
Chris@0
|
223 */
|
Chris@0
|
224 public function getClassName()
|
Chris@0
|
225 {
|
Chris@0
|
226 return $this->className;
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * @return string
|
Chris@0
|
231 */
|
Chris@0
|
232 public function getNamespaceName()
|
Chris@0
|
233 {
|
Chris@0
|
234 return $this->namespace;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * {@inheritDoc}
|
Chris@0
|
239 */
|
Chris@0
|
240 public function getReflectionClass()
|
Chris@0
|
241 {
|
Chris@0
|
242 return new StaticReflectionClass($this);
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * {@inheritDoc}
|
Chris@0
|
247 */
|
Chris@0
|
248 public function getReflectionMethod($methodName)
|
Chris@0
|
249 {
|
Chris@0
|
250 return new StaticReflectionMethod($this, $methodName);
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * {@inheritDoc}
|
Chris@0
|
255 */
|
Chris@0
|
256 public function getReflectionProperty($propertyName)
|
Chris@0
|
257 {
|
Chris@0
|
258 return new StaticReflectionProperty($this, $propertyName);
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 /**
|
Chris@0
|
262 * Gets the use statements from this file.
|
Chris@0
|
263 *
|
Chris@0
|
264 * @return array
|
Chris@0
|
265 */
|
Chris@0
|
266 public function getUseStatements()
|
Chris@0
|
267 {
|
Chris@0
|
268 $this->parse();
|
Chris@0
|
269
|
Chris@0
|
270 return $this->useStatements;
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 /**
|
Chris@0
|
274 * Gets the doc comment.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @param string $type The type: 'class', 'property' or 'method'.
|
Chris@0
|
277 * @param string $name The name of the property or method, not needed for 'class'.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @return string The doc comment, empty string if none.
|
Chris@0
|
280 */
|
Chris@0
|
281 public function getDocComment($type = 'class', $name = '')
|
Chris@0
|
282 {
|
Chris@0
|
283 $this->parse();
|
Chris@0
|
284
|
Chris@0
|
285 return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 /**
|
Chris@0
|
289 * Gets the PSR-0 parser for the declaring class.
|
Chris@0
|
290 *
|
Chris@0
|
291 * @param string $type The type: 'property' or 'method'.
|
Chris@0
|
292 * @param string $name The name of the property or method.
|
Chris@0
|
293 *
|
Chris@0
|
294 * @return StaticReflectionParser A static reflection parser for the declaring class.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @throws ReflectionException
|
Chris@0
|
297 */
|
Chris@0
|
298 public function getStaticReflectionParserForDeclaringClass($type, $name)
|
Chris@0
|
299 {
|
Chris@0
|
300 $this->parse();
|
Chris@0
|
301 if (isset($this->docComment[$type][$name])) {
|
Chris@0
|
302 return $this;
|
Chris@0
|
303 }
|
Chris@0
|
304 if (!empty($this->parentClassName)) {
|
Chris@0
|
305 return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
|
Chris@0
|
306 }
|
Chris@0
|
307 throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
|
Chris@0
|
308 }
|
Chris@0
|
309 }
|