|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Annotations; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Parses a file for namespaces/use/class declarations. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Fabien Potencier <[email protected]> |
|
9
|
|
|
* @author Christian Kaps <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class TokenParser |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The token list. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $tokens; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The number of tokens. |
|
22
|
|
|
* |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
private $numTokens; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The current array pointer. |
|
29
|
|
|
* |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $pointer = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $contents |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($contents) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->tokens = token_get_all($contents); |
|
40
|
|
|
|
|
41
|
|
|
// The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it |
|
42
|
|
|
// saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored |
|
43
|
|
|
// doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a |
|
44
|
|
|
// docblock. If the first thing in the file is a class without a doc block this would cause calls to |
|
45
|
|
|
// getDocBlock() on said class to return our long lost doc_comment. Argh. |
|
46
|
|
|
// To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least |
|
47
|
|
|
// it's harmless to us. |
|
48
|
|
|
token_get_all("<?php\n/**\n *\n */"); |
|
49
|
|
|
|
|
50
|
|
|
$this->numTokens = count($this->tokens); |
|
51
|
|
|
|
|
52
|
|
|
// Compatibility defines for PHP < 8.0. |
|
53
|
|
|
if (!defined('T_NAME_QUALIFIED')) { |
|
54
|
|
|
\define('T_NAME_QUALIFIED', -2); |
|
55
|
|
|
} |
|
56
|
|
|
if (!defined('T_NAME_FULLY_QUALIFIED')) { |
|
57
|
|
|
\define('T_NAME_FULLY_QUALIFIED', -3); |
|
58
|
|
|
} |
|
59
|
|
|
if (!defined('T_NAME_RELATIVE')) { |
|
60
|
|
|
\define('T_NAME_RELATIVE', -4); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets the next non whitespace and non comment token. |
|
66
|
|
|
* |
|
67
|
|
|
* @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped. |
|
68
|
|
|
* If FALSE then only whitespace and normal comments are skipped. |
|
69
|
|
|
* |
|
70
|
|
|
* @return array|null The token if exists, null otherwise. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function next($docCommentIsComment = TRUE) |
|
73
|
|
|
{ |
|
74
|
|
|
for ($i = $this->pointer; $i < $this->numTokens; $i++) { |
|
75
|
|
|
$this->pointer++; |
|
76
|
|
|
if ($this->tokens[$i][0] === T_WHITESPACE || |
|
77
|
|
|
$this->tokens[$i][0] === T_COMMENT || |
|
78
|
|
|
($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) { |
|
79
|
|
|
|
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->tokens[$i]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses a single use statement. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array A list with all found class names for a use statement. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function parseUseStatement() |
|
95
|
|
|
{ |
|
96
|
|
|
return PHP_VERSION_ID >=80000 ? $this->parseUseStatementV8() : $this->parseUseStatementV7(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function parseUseStatementV7(): array |
|
100
|
|
|
{ |
|
101
|
|
|
$groupRoot = ''; |
|
102
|
|
|
$class = ''; |
|
103
|
|
|
$alias = ''; |
|
104
|
|
|
$statements = []; |
|
105
|
|
|
$explicitAlias = false; |
|
106
|
|
|
while (($token = $this->next())) { |
|
107
|
|
|
$isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; |
|
108
|
|
|
if (!$explicitAlias && $isNameToken) { |
|
109
|
|
|
$class .= $token[1]; |
|
110
|
|
|
$alias = $token[1]; |
|
111
|
|
|
} else if ($explicitAlias && $isNameToken) { |
|
112
|
|
|
$alias .= $token[1]; |
|
113
|
|
|
} else if ($token[0] === T_AS) { |
|
114
|
|
|
$explicitAlias = true; |
|
115
|
|
|
$alias = ''; |
|
116
|
|
|
} else if ($token === ',') { |
|
117
|
|
|
$statements[strtolower($alias)] = $groupRoot . $class; |
|
118
|
|
|
$class = ''; |
|
119
|
|
|
$alias = ''; |
|
120
|
|
|
$explicitAlias = false; |
|
121
|
|
|
} else if ($token === ';') { |
|
122
|
|
|
$statements[strtolower($alias)] = $groupRoot . $class; |
|
123
|
|
|
break; |
|
124
|
|
|
} else if ($token === '{' ) { |
|
125
|
|
|
$groupRoot = $class; |
|
126
|
|
|
$class = ''; |
|
127
|
|
|
} else if ($token === '}' ) { |
|
128
|
|
|
continue; |
|
129
|
|
|
} else { |
|
130
|
|
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $statements; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function parseUseStatementV8(): array |
|
138
|
|
|
{ |
|
139
|
|
|
$groupRoot = ''; |
|
140
|
|
|
$class = ''; |
|
141
|
|
|
$alias = ''; |
|
142
|
|
|
$statements = []; |
|
143
|
|
|
$explicitAlias = false; |
|
144
|
|
|
while (($token = $this->next())) { |
|
145
|
|
|
$isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR; |
|
146
|
|
|
|
|
147
|
|
|
if ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED) { |
|
148
|
|
|
$class .= $token[1]; |
|
149
|
|
|
|
|
150
|
|
|
$classSplit = explode('\\', $token[1]); |
|
151
|
|
|
$alias = $classSplit[count($classSplit) - 1]; |
|
152
|
|
|
} else if($token[0] === T_NS_SEPARATOR) { |
|
153
|
|
|
$class .= '\\'; |
|
154
|
|
|
} else if ($token[0] === T_AS) { |
|
155
|
|
|
$explicitAlias = true; |
|
156
|
|
|
$alias = ''; |
|
157
|
|
|
} else if ($isNameToken && !$explicitAlias) { |
|
158
|
|
|
$class = $token[1]; |
|
159
|
|
|
$alias = $token[1]; |
|
160
|
|
|
} else if ($isNameToken && $explicitAlias) { |
|
161
|
|
|
$alias = $token[1]; |
|
162
|
|
|
} else if ($token === ',') { |
|
163
|
|
|
$statements[strtolower($alias)] = $groupRoot . $class; |
|
164
|
|
|
$class = ''; |
|
165
|
|
|
$alias = ''; |
|
166
|
|
|
$explicitAlias = false; |
|
167
|
|
|
} else if ($token === ';') { |
|
168
|
|
|
$statements[strtolower($alias)] = $groupRoot . $class; |
|
169
|
|
|
break; |
|
170
|
|
|
} else if ($token === '{' ) { |
|
171
|
|
|
$groupRoot = $class; |
|
172
|
|
|
$class = ''; |
|
173
|
|
|
} else if ($token === '}' ) { |
|
174
|
|
|
continue; |
|
175
|
|
|
} else { |
|
176
|
|
|
break; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $statements; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Gets all use statements. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $namespaceName The namespace name of the reflected class. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array A list with all found use statements. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function parseUseStatements($namespaceName) |
|
191
|
|
|
{ |
|
192
|
|
|
$statements = []; |
|
193
|
|
|
while (($token = $this->next())) { |
|
194
|
|
|
if ($token[0] === T_USE) { |
|
195
|
|
|
$statements = array_merge($statements, $this->parseUseStatement()); |
|
196
|
|
|
continue; |
|
197
|
|
|
} |
|
198
|
|
|
if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) { |
|
199
|
|
|
continue; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// Get fresh array for new namespace. This is to prevent the parser to collect the use statements |
|
203
|
|
|
// for a previous namespace with the same name. This is the case if a namespace is defined twice |
|
204
|
|
|
// or if a namespace with the same name is commented out. |
|
205
|
|
|
$statements = []; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $statements; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Gets the namespace. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string The found namespace. |
|
215
|
|
|
*/ |
|
216
|
|
|
public function parseNamespace() |
|
217
|
|
|
{ |
|
218
|
|
|
$name = ''; |
|
219
|
|
|
while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR || $token[0] === T_NAME_QUALIFIED)) { |
|
220
|
|
|
$name .= $token[1]; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $name; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Gets the class name. |
|
228
|
|
|
* |
|
229
|
|
|
* @return string The found class name. |
|
230
|
|
|
*/ |
|
231
|
|
|
public function parseClass() |
|
232
|
|
|
{ |
|
233
|
|
|
// Namespaces and class names are tokenized the same: T_STRINGs |
|
234
|
|
|
// separated by T_NS_SEPARATOR so we can use one function to provide |
|
235
|
|
|
// both. |
|
236
|
|
|
return $this->parseNamespace(); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|