1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Reflection; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\TokenParser; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use const T_CLASS; |
8
|
|
|
use const T_DOC_COMMENT; |
9
|
|
|
use const T_EXTENDS; |
10
|
|
|
use const T_FUNCTION; |
11
|
|
|
use const T_PAAMAYIM_NEKUDOTAYIM; |
12
|
|
|
use const T_PRIVATE; |
13
|
|
|
use const T_PROTECTED; |
14
|
|
|
use const T_PUBLIC; |
15
|
|
|
use const T_STRING; |
16
|
|
|
use const T_USE; |
17
|
|
|
use const T_VAR; |
18
|
|
|
use const T_VARIABLE; |
19
|
|
|
use function array_merge; |
20
|
|
|
use function file_get_contents; |
21
|
|
|
use function ltrim; |
22
|
|
|
use function preg_match; |
23
|
|
|
use function sprintf; |
24
|
|
|
use function strpos; |
25
|
|
|
use function strrpos; |
26
|
|
|
use function strtolower; |
27
|
|
|
use function substr; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Parses a file for namespaces/use/class declarations. |
31
|
|
|
*/ |
32
|
|
|
class StaticReflectionParser implements ReflectionProviderInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The fully qualified class name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $className; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The short class name. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $shortClassName; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Whether the caller only wants class annotations. |
50
|
|
|
* |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
protected $classAnnotationOptimize; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* A ClassFinder object which finds the class. |
57
|
|
|
* |
58
|
|
|
* @var ClassFinderInterface |
59
|
|
|
*/ |
60
|
|
|
protected $finder; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Whether the parser has run. |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
protected $parsed = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The namespace of the class. |
71
|
|
|
* |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected $namespace = ''; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The use statements of the class. |
78
|
|
|
* |
79
|
|
|
* @var string[] |
80
|
|
|
*/ |
81
|
|
|
protected $useStatements = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The docComment of the class. |
85
|
|
|
* |
86
|
|
|
* @var mixed[] |
87
|
|
|
*/ |
88
|
|
|
protected $docComment = [ |
89
|
|
|
'class' => '', |
90
|
|
|
'property' => [], |
91
|
|
|
'method' => [], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The name of the class this class extends, if any. |
96
|
|
|
* |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
protected $parentClassName = ''; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The parent PSR-0 Parser. |
103
|
|
|
* |
104
|
|
|
* @var \Doctrine\Common\Reflection\StaticReflectionParser |
105
|
|
|
*/ |
106
|
|
|
protected $parentStaticReflectionParser; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Parses a class residing in a PSR-0 hierarchy. |
110
|
|
|
* |
111
|
|
|
* @param string $className The full, namespaced class name. |
112
|
|
|
* @param ClassFinderInterface $finder A ClassFinder object which finds the class. |
113
|
|
|
* @param bool $classAnnotationOptimize Only retrieve the class docComment. |
114
|
|
|
* Presumes there is only one statement per line. |
115
|
|
|
*/ |
116
|
14 |
|
public function __construct($className, $finder, $classAnnotationOptimize = false) |
117
|
|
|
{ |
118
|
14 |
|
$this->className = ltrim($className, '\\'); |
119
|
14 |
|
$lastNsPos = strrpos($this->className, '\\'); |
120
|
|
|
|
121
|
14 |
|
if ($lastNsPos !== false) { |
122
|
14 |
|
$this->namespace = substr($this->className, 0, $lastNsPos); |
123
|
14 |
|
$this->shortClassName = substr($this->className, $lastNsPos + 1); |
124
|
|
|
} else { |
125
|
|
|
$this->shortClassName = $this->className; |
126
|
|
|
} |
127
|
|
|
|
128
|
14 |
|
$this->finder = $finder; |
129
|
14 |
|
$this->classAnnotationOptimize = $classAnnotationOptimize; |
130
|
14 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
14 |
|
protected function parse() |
136
|
|
|
{ |
137
|
14 |
|
$fileName = $this->finder->findFile($this->className); |
138
|
|
|
|
139
|
14 |
|
if ($this->parsed || ! $fileName) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
14 |
|
$this->parsed = true; |
143
|
14 |
|
$contents = file_get_contents($fileName); |
144
|
14 |
|
if ($this->classAnnotationOptimize) { |
145
|
7 |
|
$regex = sprintf('/\A.*^\s*((abstract|final)\s+)?class\s+%s\s+/sm', $this->shortClassName); |
146
|
|
|
|
147
|
7 |
|
if (preg_match($regex, $contents, $matches)) { |
148
|
7 |
|
$contents = $matches[0]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
14 |
|
$tokenParser = new TokenParser($contents); |
152
|
14 |
|
$docComment = ''; |
153
|
14 |
|
$last_token = false; |
154
|
|
|
|
155
|
14 |
|
while ($token = $tokenParser->next(false)) { |
156
|
14 |
|
switch ($token[0]) { |
157
|
14 |
|
case T_USE: |
158
|
6 |
|
$this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement()); |
159
|
6 |
|
break; |
160
|
14 |
|
case T_DOC_COMMENT: |
161
|
9 |
|
$docComment = $token[1]; |
162
|
9 |
|
break; |
163
|
14 |
|
case T_CLASS: |
164
|
14 |
|
if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM) { |
165
|
14 |
|
$this->docComment['class'] = $docComment; |
166
|
14 |
|
$docComment = ''; |
167
|
|
|
} |
168
|
14 |
|
break; |
169
|
14 |
|
case T_VAR: |
170
|
14 |
|
case T_PRIVATE: |
171
|
14 |
|
case T_PROTECTED: |
172
|
14 |
|
case T_PUBLIC: |
173
|
6 |
|
$token = $tokenParser->next(); |
174
|
6 |
|
if ($token[0] === T_VARIABLE) { |
175
|
5 |
|
$propertyName = substr($token[1], 1); |
176
|
5 |
|
$this->docComment['property'][$propertyName] = $docComment; |
177
|
5 |
|
continue 2; |
178
|
|
|
} |
179
|
1 |
|
if ($token[0] !== T_FUNCTION) { |
180
|
|
|
// For example, it can be T_FINAL. |
181
|
1 |
|
continue 2; |
182
|
|
|
} |
183
|
|
|
// No break. |
184
|
14 |
|
case T_FUNCTION: |
185
|
|
|
// The next string after function is the name, but |
186
|
|
|
// there can be & before the function name so find the |
187
|
|
|
// string. |
188
|
|
|
while (($token = $tokenParser->next()) && $token[0] !== T_STRING) { |
189
|
|
|
continue; |
190
|
|
|
} |
191
|
|
|
$methodName = $token[1]; |
192
|
|
|
$this->docComment['method'][$methodName] = $docComment; |
193
|
|
|
$docComment = ''; |
194
|
|
|
break; |
195
|
14 |
|
case T_EXTENDS: |
196
|
4 |
|
$this->parentClassName = $tokenParser->parseClass(); |
197
|
4 |
|
$nsPos = strpos($this->parentClassName, '\\'); |
198
|
4 |
|
$fullySpecified = false; |
199
|
4 |
|
if ($nsPos === 0) { |
200
|
|
|
$fullySpecified = true; |
201
|
|
|
} else { |
202
|
4 |
|
if ($nsPos) { |
203
|
1 |
|
$prefix = strtolower(substr($this->parentClassName, 0, $nsPos)); |
204
|
1 |
|
$postfix = substr($this->parentClassName, $nsPos); |
205
|
|
|
} else { |
206
|
3 |
|
$prefix = strtolower($this->parentClassName); |
207
|
3 |
|
$postfix = ''; |
208
|
|
|
} |
209
|
4 |
|
foreach ($this->useStatements as $alias => $use) { |
210
|
1 |
|
if ($alias !== $prefix) { |
211
|
|
|
continue; |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
$this->parentClassName = '\\' . $use . $postfix; |
215
|
1 |
|
$fullySpecified = true; |
216
|
|
|
} |
217
|
|
|
} |
218
|
4 |
|
if (! $fullySpecified) { |
219
|
3 |
|
$this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName; |
220
|
|
|
} |
221
|
4 |
|
break; |
222
|
|
|
} |
223
|
|
|
|
224
|
14 |
|
$last_token = $token[0]; |
225
|
|
|
} |
226
|
14 |
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return StaticReflectionParser |
230
|
|
|
*/ |
231
|
4 |
|
protected function getParentStaticReflectionParser() |
232
|
|
|
{ |
233
|
4 |
|
if (empty($this->parentStaticReflectionParser)) { |
234
|
4 |
|
$this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder); |
235
|
|
|
} |
236
|
|
|
|
237
|
4 |
|
return $this->parentStaticReflectionParser; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
5 |
|
public function getClassName() |
244
|
|
|
{ |
245
|
5 |
|
return $this->className; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function getNamespaceName() |
252
|
|
|
{ |
253
|
|
|
return $this->namespace; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritDoc} |
258
|
|
|
*/ |
259
|
|
|
public function getReflectionClass() |
260
|
|
|
{ |
261
|
|
|
return new StaticReflectionClass($this); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritDoc} |
266
|
|
|
*/ |
267
|
|
|
public function getReflectionMethod($methodName) |
268
|
|
|
{ |
269
|
|
|
return new StaticReflectionMethod($this, $methodName); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritDoc} |
274
|
|
|
*/ |
275
|
|
|
public function getReflectionProperty($propertyName) |
276
|
|
|
{ |
277
|
|
|
return new StaticReflectionProperty($this, $propertyName); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Gets the use statements from this file. |
282
|
|
|
* |
283
|
|
|
* @return string[] |
284
|
|
|
*/ |
285
|
|
|
public function getUseStatements() |
286
|
|
|
{ |
287
|
|
|
$this->parse(); |
288
|
|
|
|
289
|
|
|
return $this->useStatements; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Gets the doc comment. |
294
|
|
|
* |
295
|
|
|
* @param string $type The type: 'class', 'property' or 'method'. |
296
|
|
|
* @param string $name The name of the property or method, not needed for 'class'. |
297
|
|
|
* |
298
|
|
|
* @return string The doc comment, empty string if none. |
299
|
|
|
*/ |
300
|
4 |
|
public function getDocComment($type = 'class', $name = '') |
301
|
|
|
{ |
302
|
4 |
|
$this->parse(); |
303
|
|
|
|
304
|
4 |
|
return $name ? $this->docComment[$type][$name] : $this->docComment[$type]; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Gets the PSR-0 parser for the declaring class. |
309
|
|
|
* |
310
|
|
|
* @param string $type The type: 'property' or 'method'. |
311
|
|
|
* @param string $name The name of the property or method. |
312
|
|
|
* |
313
|
|
|
* @return StaticReflectionParser A static reflection parser for the declaring class. |
314
|
|
|
* |
315
|
|
|
* @throws ReflectionException |
316
|
|
|
*/ |
317
|
10 |
|
public function getStaticReflectionParserForDeclaringClass($type, $name) |
318
|
|
|
{ |
319
|
10 |
|
$this->parse(); |
320
|
10 |
|
if (isset($this->docComment[$type][$name])) { |
321
|
5 |
|
return $this; |
322
|
|
|
} |
323
|
9 |
|
if (! empty($this->parentClassName)) { |
324
|
4 |
|
return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name); |
325
|
|
|
} |
326
|
5 |
|
throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"'); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|