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