1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Doppelgaenger\Parser\AbstractStructureParser |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Bernhard Wick <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
18
|
|
|
* @link http://www.appserver.io/ |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Doppelgaenger\Parser; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\StructureDefinitionList; |
24
|
|
|
use AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface; |
25
|
|
|
use AppserverIo\Doppelgaenger\Interfaces\StructureParserInterface; |
26
|
|
|
use AppserverIo\Doppelgaenger\Entities\Definitions\FileDefinition; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The abstract class AbstractStructureParser which provides a basic implementation other structure parsers |
30
|
|
|
* can inherit from |
31
|
|
|
* |
32
|
|
|
* @author Bernhard Wick <[email protected]> |
33
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
36
|
|
|
* @link http://www.appserver.io/ |
37
|
|
|
*/ |
38
|
|
|
abstract class AbstractStructureParser extends AbstractParser implements StructureParserInterface |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Will return the constants within the main token array |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getConstants() |
47
|
|
|
{ |
48
|
|
|
// Check the tokens |
49
|
|
|
$constants = array(); |
50
|
|
|
for ($i = 0; $i < $this->tokenCount; $i++) { |
51
|
|
|
// If we got the class name |
52
|
|
|
if ($this->tokens[$i][0] === T_CONST) { |
53
|
|
|
for ($j = $i + 1; $j < $this->tokenCount; $j++) { |
54
|
|
|
if ($this->tokens[$j] === ';') { |
55
|
|
|
break; |
56
|
|
|
|
|
|
|
|
57
|
|
|
} elseif ($this->tokens[$j][0] === T_STRING) { |
58
|
|
|
$constants[$this->tokens[$j][1]] = ''; |
59
|
|
|
|
60
|
|
|
for ($k = $j + 1; $k < count($this->tokens); $k++) { |
|
|
|
|
61
|
|
|
if ($this->tokens[$k] === ';') { |
62
|
|
|
break; |
63
|
|
|
|
|
|
|
|
64
|
|
|
} elseif (is_array($this->tokens[$k]) && $this->tokens[$k][0] !== '=') { |
65
|
|
|
$constants[$this->tokens[$j][1]] .= $this->tokens[$k][1]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Now trim what we got |
70
|
|
|
$constants[$this->tokens[$j][1]] = trim($constants[$this->tokens[$j][1]]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Return what we did or did not found |
77
|
|
|
return $constants; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Will return the definition of a specified structure |
82
|
|
|
* |
83
|
|
|
* @param null|string $name The name of the class we are searching for |
84
|
|
|
* @param boolean $getRecursive Do we have to get the ancestral conditions as well? |
85
|
|
|
* |
86
|
|
|
* @return boolean|\AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface |
87
|
|
|
*/ |
88
|
|
|
public function getDefinition($name = null, $getRecursive = true) |
89
|
|
|
{ |
90
|
|
|
// Maybe we already got this structure? |
91
|
|
|
if ($this->structureDefinitionHierarchy->entryExists($name)) { |
92
|
|
|
return $this->structureDefinitionHierarchy->getEntry($name); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// First of all we need to get the structure tokens |
96
|
|
|
$tokens = $this->getStructureTokens($this->getToken()); |
97
|
|
|
|
98
|
|
|
// Did we get something valuable? |
99
|
|
|
if ($tokens === false) { |
100
|
|
|
return false; |
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
} elseif ($name === null && count($tokens) > 1) { |
103
|
|
|
// If we did not get a structure name and we got more than one class we can fail right here |
104
|
|
|
return false; |
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
} elseif (count($tokens) === 1) { |
107
|
|
|
// We got what we came for |
108
|
|
|
return $this->getDefinitionFromTokens($tokens[0], $getRecursive); |
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
} elseif (is_string($name) && count($tokens) > 1) { |
111
|
|
|
// We are still here, but got a structure name to look for |
112
|
|
|
|
113
|
|
View Code Duplication |
foreach ($tokens as $key => $token) { |
114
|
|
|
// Now iterate over the array and search for the class we want |
115
|
|
|
for ($i = 0; $i < count($token); $i++) { |
|
|
|
|
116
|
|
|
if (is_array($token[$i]) && $token[$i] === $this->getToken() && $token[$i + 2] === $name) { |
117
|
|
|
return $this->getDefinitionFromTokens($tokens[$key], $getRecursive); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Still here? Must be an error. |
124
|
|
|
return false; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Will return a list of found structures or false on error |
129
|
|
|
* |
130
|
|
|
* @param string $file Path of the file we are searching in |
131
|
|
|
* @param FileDefinition $fileDefinition Definition of the file the class is in |
132
|
|
|
* @param boolean $getRecursive Do we have to get the ancestral conditions as well? |
133
|
|
|
* |
134
|
|
|
* @return boolean|StructureDefinitionList |
135
|
|
|
*/ |
136
|
|
|
public function getDefinitionListFromFile($file, FileDefinition $fileDefinition, $getRecursive = true) |
137
|
|
|
{ |
138
|
|
|
// Get all the token arrays for the different classes |
139
|
|
|
$tokens = $this->getStructureTokens($file, $this->getToken()); |
|
|
|
|
140
|
|
|
|
141
|
|
|
// Did we get the right thing? |
142
|
|
|
if (!is_array($tokens)) { |
143
|
|
|
return false; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$structureDefinitionList = new StructureDefinitionList(); |
147
|
|
|
foreach ($tokens as $token) { |
148
|
|
|
try { |
149
|
|
|
$structureDefinitionList->add($this->getDefinitionFromTokens($token, $fileDefinition, $getRecursive)); |
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
} catch (\UnexpectedValueException $e) { |
152
|
|
|
// Just try the next one |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $structureDefinitionList; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the name of the structure |
162
|
|
|
* |
163
|
|
|
* @param array $tokens The token array |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function getName($tokens) |
168
|
|
|
{ |
169
|
|
|
// Check the tokens |
170
|
|
|
$name = ''; |
171
|
|
|
$targetToken = $this->getToken(); |
172
|
|
|
for ($i = 0; $i < count($tokens); $i++) { |
|
|
|
|
173
|
|
|
// If we got the structure name |
174
|
|
|
if ($tokens[$i][0] === $targetToken && $tokens[$i - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM) { |
175
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
176
|
|
|
if ($tokens[$j] === '{') { |
177
|
|
|
$name = $tokens[$i + 2][1]; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// Return what we did or did not found |
184
|
|
|
return $name; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Will return the structure's namespace if found |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getNamespace() |
193
|
|
|
{ |
194
|
|
|
// Check the tokens |
195
|
|
|
$namespace = ''; |
196
|
|
|
for ($i = 0; $i < $this->tokenCount; $i++) { |
197
|
|
|
// If we got the namespace |
198
|
|
|
if ($this->tokens[$i][0] === T_NAMESPACE) { |
199
|
|
View Code Duplication |
for ($j = $i + 1; $j < count($this->tokens); $j++) { |
|
|
|
|
200
|
|
|
if ($this->tokens[$j][0] === T_STRING) { |
201
|
|
|
$namespace .= '\\' . $this->tokens[$j][1]; |
202
|
|
|
|
|
|
|
|
203
|
|
|
} elseif ($this->tokens[$j] === '{' || |
204
|
|
|
$this->tokens[$j] === ';' || |
205
|
|
|
$this->tokens[$j][0] === T_CURLY_OPEN |
206
|
|
|
) { |
207
|
|
|
break; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Return what we did or did not found |
214
|
|
|
return substr($namespace, 1); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Will check the main token array for the occurrence of a certain on (class, interface or trait) |
219
|
|
|
* |
220
|
|
|
* @return string|boolean |
221
|
|
|
*/ |
222
|
|
|
protected function getStructureToken() |
223
|
|
|
{ |
224
|
|
|
for ($i = 0; $i < $this->tokenCount; $i++) { |
225
|
|
|
switch ($this->tokens[$i][0]) { |
226
|
|
|
case T_CLASS: |
227
|
|
|
return 'class'; |
228
|
|
|
break; |
|
|
|
|
229
|
|
|
|
230
|
|
|
case T_INTERFACE: |
231
|
|
|
return 'interface'; |
232
|
|
|
break; |
|
|
|
|
233
|
|
|
|
234
|
|
|
case T_TRAIT: |
235
|
|
|
return 'trait'; |
236
|
|
|
break; |
|
|
|
|
237
|
|
|
|
238
|
|
|
default: |
239
|
|
|
continue; |
240
|
|
|
break; |
|
|
|
|
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// We are still here? That should not be. |
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Will check if a certain token describes a structure |
250
|
|
|
* |
251
|
|
|
* @param mixed $token The token to test |
252
|
|
|
* |
253
|
|
|
* @return boolean |
254
|
|
|
*/ |
255
|
|
|
protected function isStructureToken($token) |
256
|
|
|
{ |
257
|
|
|
if (is_array($token) && ($token[0] === T_CLASS || $token[0] === T_INTERFACE || $token[0] === T_TRAIT)) { |
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
// We are still here? That should not be. |
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Will return a subset of our main token array. This subset includes all tokens belonging to a certain structure. |
266
|
|
|
* Might return false on failure |
267
|
|
|
* |
268
|
|
|
* @param integer $structureToken The structure we are after e.g. T_CLASS, use PHP tokens here |
269
|
|
|
* |
270
|
|
|
* @return array|boolean |
271
|
|
|
*/ |
272
|
|
|
protected function getStructureTokens($structureToken) |
273
|
|
|
{ |
274
|
|
|
// Now iterate over the array and filter different classes from it |
275
|
|
|
$result = array(); |
276
|
|
|
for ($i = 0; $i < $this->tokenCount; $i++) { |
277
|
|
|
// If we got a class keyword, we have to check how far the class extends, |
278
|
|
|
// then copy the array withing that bounds |
279
|
|
|
|
280
|
|
|
if (is_array($this->tokens[$i]) && $this->tokens[$i][0] === $structureToken && $this->tokens[$i - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM) { |
281
|
|
|
// The lower bound should be the last semicolon|closing curly bracket|PHP tag before the structure |
282
|
|
|
$lowerBound = 0; |
283
|
|
|
for ($j = $i - 1; $j >= 0; $j--) { |
284
|
|
|
if ($this->tokens[$j] === ';' || $this->tokens[$j] === '}' || |
285
|
|
|
is_array($this->tokens[$j]) && $this->tokens[$j][0] === T_OPEN_TAG |
286
|
|
|
) { |
287
|
|
|
$lowerBound = $j; |
288
|
|
|
break; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// The upper bound should be the first time the curly brackets are even again |
293
|
|
|
$upperBound = $this->tokenCount - 1; |
294
|
|
|
$bracketCounter = null; |
295
|
|
|
for ($j = $i + 1; $j < count($this->tokens); $j++) { |
|
|
|
|
296
|
|
|
if ($this->tokens[$j] === '{' || $this->tokens[$j][0] === T_CURLY_OPEN) { |
297
|
|
|
// If we still got null set to 0 |
298
|
|
|
if ($bracketCounter === null) { |
299
|
|
|
$bracketCounter = 0; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$bracketCounter++; |
303
|
|
|
|
|
|
|
|
304
|
|
View Code Duplication |
} elseif ($this->tokens[$j] === '}') { |
305
|
|
|
// If we still got null set to 0 |
306
|
|
|
if ($bracketCounter === null) { |
307
|
|
|
$bracketCounter = 0; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$bracketCounter--; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// Do we have an even amount of brackets yet? |
314
|
|
|
if ($bracketCounter === 0) { |
315
|
|
|
$upperBound = $j; |
316
|
|
|
break; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$result[] = array_slice($this->tokens, $lowerBound, $upperBound - $lowerBound); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
// Last line of defence; did we get something? |
325
|
|
|
if (empty($result)) { |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $result; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Will return an array of structures which this structure references by use statements |
334
|
|
|
* |
335
|
|
|
* @return array |
336
|
|
|
*/ |
337
|
|
|
public function getUsedStructures() |
338
|
|
|
{ |
339
|
|
|
// Check the tokens |
340
|
|
|
$structures = array(); |
341
|
|
|
for ($i = 0; $i < $this->tokenCount; $i++) { |
342
|
|
|
// break as soon as we pass a structure keyword as "use" imports must be globally |
343
|
|
|
if ($this->isStructureToken($this->tokens[$i])) { |
344
|
|
|
break; |
345
|
|
|
} |
346
|
|
|
// if we got a use statement |
347
|
|
|
if ($this->tokens[$i][0] === T_USE) { |
348
|
|
|
$structure = ''; |
349
|
|
View Code Duplication |
for ($j = $i + 1; $j < count($this->tokens); $j++) { |
|
|
|
|
350
|
|
|
if ($this->tokens[$j][0] === T_STRING) { |
351
|
|
|
$structure .= '\\' . $this->tokens[$j][1]; |
352
|
|
|
|
|
|
|
|
353
|
|
|
} elseif ($this->tokens[$j] === '{' || |
354
|
|
|
$this->tokens[$j] === ';' || |
355
|
|
|
$this->tokens[$j][0] === T_CURLY_OPEN |
356
|
|
|
) { |
357
|
|
|
$structures[] = $structure; |
358
|
|
|
break; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Return what we did or did not found |
365
|
|
|
return $structures; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Will check if a certain structure was mentioned in one(!) use statement. |
370
|
|
|
* |
371
|
|
|
* @param StructureDefinitionInterface $structureDefinition The structure $structureName is compared against |
372
|
|
|
* @param string $structureName The name of the structure we have to check against the |
373
|
|
|
* use statements of the definition |
374
|
|
|
* |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
|
|
protected function resolveUsedNamespace(StructureDefinitionInterface & $structureDefinition, $structureName) |
378
|
|
|
{ |
379
|
|
|
// If there was no useful name passed we can fail right here |
380
|
|
|
if (empty($structureName)) { |
381
|
|
|
return ''; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
// Walk over all namespaces and if we find something we will act accordingly. |
385
|
|
|
$result = $structureDefinition->getQualifiedName(); |
386
|
|
|
foreach ($structureDefinition->getUsedStructures() as $key => $usedStructures) { |
387
|
|
|
// Check if the last part of the use statement is our structure |
388
|
|
|
$tmp = explode('\\', $usedStructures); |
389
|
|
|
if (array_pop($tmp) === $structureName) { |
390
|
|
|
// Tell them we succeeded |
391
|
|
|
return trim(implode('\\', $tmp) . '\\' . $structureName, '\\'); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
// We did not seem to have found anything. Might it be that we are in our own namespace? |
396
|
|
|
if ($structureDefinition->getNamespace() !== null && strpos($structureName, '\\') !== 0) { |
397
|
|
|
return $structureDefinition->getNamespace() . '\\' . $structureName; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// Still here? Return what we got. |
401
|
|
|
return $result; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|