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