|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\Parser\InterfaceParser |
|
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\Definitions\InterfaceDefinition; |
|
24
|
|
|
use AppserverIo\Doppelgaenger\Entities\Definitions\FileDefinition; |
|
25
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\StructureDefinitionList; |
|
26
|
|
|
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException; |
|
27
|
|
|
use AppserverIo\Psr\MetaobjectProtocol\Dbc\Annotations\Invariant; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The InterfaceParser class which is used to get an \AppserverIo\Doppelgaenger\Entities\Definitions\InterfaceDefinition |
|
31
|
|
|
* instance (or several) from a fail containing those definition(s) |
|
32
|
|
|
* |
|
33
|
|
|
* @author Bernhard Wick <[email protected]> |
|
34
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
37
|
|
|
* @link http://www.appserver.io/ |
|
38
|
|
|
*/ |
|
39
|
|
|
class InterfaceParser extends AbstractStructureParser |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Token representing the structure this parser is used for |
|
44
|
|
|
* |
|
45
|
|
|
* @var integer TOKEN |
|
46
|
|
|
*/ |
|
47
|
|
|
const TOKEN = T_INTERFACE; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Will return the token representing the structure the parser is used for e.g. T_CLASS |
|
51
|
|
|
* |
|
52
|
|
|
* @return integer |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getToken() |
|
55
|
|
|
{ |
|
56
|
|
|
return self::TOKEN; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Will get all parent interfaces (if any). |
|
61
|
|
|
* Might return false on error |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $tokens The token array |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getParents($tokens) |
|
68
|
|
|
{ |
|
69
|
|
|
// Check the tokens |
|
70
|
|
|
$interfaceString = ''; |
|
71
|
|
|
for ($i = 0; $i < count($tokens); $i++) { |
|
|
|
|
|
|
72
|
|
|
// If we got the interface name |
|
73
|
|
|
if ($tokens[$i][0] === T_EXTENDS) { |
|
74
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
|
|
75
|
|
|
if ($tokens[$j] === '{') { |
|
76
|
|
|
// We got everything |
|
77
|
|
|
break; |
|
78
|
|
|
|
|
|
|
|
|
|
79
|
|
|
} elseif ($tokens[$j][0] === T_STRING) { |
|
80
|
|
|
$interfaceString .= $tokens[$j][1]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Normally we will have one or several interface names separated by commas |
|
87
|
|
|
$parents = explode(',', $interfaceString); |
|
88
|
|
|
foreach ($parents as $key => $parent) { |
|
89
|
|
|
$parents[$key] = trim($parent); |
|
90
|
|
|
|
|
91
|
|
|
// We do not want empty stuff |
|
92
|
|
|
if (empty($parents[$key])) { |
|
93
|
|
|
unset($parents[$key]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $parents; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns a ClassDefinition from a token array. |
|
102
|
|
|
* |
|
103
|
|
|
* This method will use a set of other methods to parse a token array and retrieve any |
|
104
|
|
|
* possible information from it. This information will be entered into a ClassDefinition object. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $tokens The token array |
|
107
|
|
|
* @param boolean $getRecursive Do we have to load the inherited contracts as well? |
|
108
|
|
|
* |
|
109
|
|
|
* @return \AppserverIo\Doppelgaenger\Entities\Definitions\InterfaceDefinition |
|
110
|
|
|
* |
|
111
|
|
|
* @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getDefinitionFromTokens($tokens, $getRecursive = true) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
// First of all we need a new InterfaceDefinition to fill |
|
116
|
|
View Code Duplication |
if (is_null($this->currentDefinition)) { |
|
117
|
|
|
$this->currentDefinition = new InterfaceDefinition(); |
|
118
|
|
|
|
|
|
|
|
|
|
119
|
|
|
} elseif (!$this->currentDefinition instanceof InterfaceDefinition) { |
|
120
|
|
|
throw new GeneratorException(sprintf( |
|
121
|
|
|
'The structure definition %s does not seem to be a interface definition.', |
|
122
|
|
|
$this->currentDefinition->getQualifiedName() |
|
123
|
|
|
)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Save the path of the original definition for later use |
|
127
|
|
|
$this->currentDefinition->setPath($this->file); |
|
128
|
|
|
|
|
129
|
|
|
// Get the interfaces own namespace and the namespace which are included via use |
|
130
|
|
|
$this->currentDefinition->setNamespace($this->getNamespace()); |
|
131
|
|
|
$this->currentDefinition->setUsedStructures($this->getUsedStructures()); |
|
132
|
|
|
|
|
133
|
|
|
// For our next step we would like to get the doc comment (if any) |
|
134
|
|
|
$this->currentDefinition->setDocBlock($this->getDocBlock($tokens, self::TOKEN)); |
|
135
|
|
|
|
|
136
|
|
|
// Get start and end line |
|
137
|
|
|
$this->currentDefinition->setStartLine($this->getStartLine($tokens)); |
|
138
|
|
|
$this->currentDefinition->setEndLine($this->getEndLine($tokens)); |
|
139
|
|
|
|
|
140
|
|
|
// Get the interface identity |
|
141
|
|
|
$this->currentDefinition->setName($this->getName($tokens)); |
|
142
|
|
|
|
|
143
|
|
|
// So we got our docBlock, now we can parse the invariant annotations from it |
|
144
|
|
|
$annotationParser = new AnnotationParser($this->file, $this->config, $this->tokens); |
|
145
|
|
|
$this->currentDefinition->setInvariantConditions($annotationParser->getConditions( |
|
|
|
|
|
|
146
|
|
|
$this->currentDefinition->getDocBlock(), |
|
147
|
|
|
Invariant::ANNOTATION |
|
148
|
|
|
)); |
|
149
|
|
|
|
|
150
|
|
|
// Lets check if there is any inheritance, or if we implement any interfaces |
|
151
|
|
|
$parentNames = $this->getParents($tokens); |
|
152
|
|
|
if (count($this->currentDefinition->getUsedStructures()) === 0) { |
|
153
|
|
|
foreach ($parentNames as $parentName) { |
|
154
|
|
|
if (strpos($parentName, '\\') !== false) { |
|
155
|
|
|
$this->currentDefinition->getExtends()[] = $parentName; |
|
156
|
|
|
|
|
|
|
|
|
|
157
|
|
|
} else { |
|
158
|
|
|
$this->currentDefinition->getExtends()[] = '\\' . $this->currentDefinition->getNamespace() . '\\' . $parentName; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
|
162
|
|
|
} else { |
|
163
|
|
|
foreach ($this->currentDefinition->getUsedStructures() as $alias) { |
|
164
|
|
|
foreach ($parentNames as $parentName) { |
|
165
|
|
|
if (strpos($alias, $parentName) !== false) { |
|
166
|
|
|
$this->currentDefinition->setExtends('\\' . $alias); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// Clean possible double-\ |
|
173
|
|
|
$this->currentDefinition->setExtends(str_replace('\\\\', '\\', $this->currentDefinition->getExtends())); |
|
174
|
|
|
|
|
175
|
|
|
$this->currentDefinition->setConstants($this->getConstants($tokens)); |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
// Only thing still missing are the methods, so ramp up our FunctionParser |
|
178
|
|
|
$functionParser = new FunctionParser( |
|
179
|
|
|
$this->file, |
|
180
|
|
|
$this->config, |
|
181
|
|
|
$this->structureDefinitionHierarchy, |
|
182
|
|
|
$this->structureMap, |
|
183
|
|
|
$this->currentDefinition, |
|
184
|
|
|
$this->tokens |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
$this->currentDefinition->setFunctionDefinitions($functionParser->getDefinitionListFromTokens($tokens)); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
return $this->currentDefinition; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: