1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Reflection of a PHP file. |
7
|
|
|
*/ |
8
|
|
|
class ReflectionFile |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The file that is reflected. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $reflectedFilePath; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The reflected file content. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $reflectedFileContent; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The namespaces in the file. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $namespaces = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The use statements in the file. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $uses = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Array of objects found in the file. |
41
|
|
|
* |
42
|
|
|
* @var \Reflector[] |
43
|
|
|
*/ |
44
|
|
|
protected $objects = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param string $reflectedFilePath The file path to reflect. |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct($reflectedFilePath) |
52
|
|
|
{ |
53
|
12 |
|
$realPath = realpath($reflectedFilePath); |
54
|
12 |
|
if ($realPath === false) { |
55
|
|
|
throw new \RuntimeException('File "' . $realPath . '" does not exist or is not valid.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
12 |
|
$this->reflectedFilePath = $realPath; |
59
|
12 |
|
$this->reflectedFileContent = file_get_contents($this->reflectedFilePath); |
60
|
|
|
|
61
|
12 |
|
$this->reflect(); |
62
|
12 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Do the reflection to gather data. |
66
|
|
|
*/ |
67
|
12 |
|
private function reflect() |
68
|
|
|
{ |
69
|
12 |
|
$tokens = token_get_all($this->reflectedFileContent); |
70
|
12 |
|
$namespace = null; |
71
|
12 |
|
$classLevel = 0; |
72
|
12 |
|
$level = 0; |
73
|
12 |
|
$res = $uses = array(); |
74
|
12 |
|
while (list(, $token) = each($tokens)) { |
75
|
12 |
|
switch (is_array($token) ? $token[0] : $token) { |
76
|
12 |
|
case T_CLASS: |
77
|
12 |
|
case T_INTERFACE: |
78
|
12 |
|
case T_TRAIT: |
79
|
6 |
|
if ($name = $this->fetch($tokens, array(T_STRING))) { |
80
|
6 |
|
$classLevel = $level + 1; |
81
|
6 |
|
if ($namespace !== null) { |
82
|
6 |
|
$objectFQN = $namespace . '\\' . $name; |
83
|
2 |
|
} else { |
84
|
|
|
$objectFQN = $name; |
85
|
|
|
} |
86
|
6 |
|
$this->objects[] = new ReflectionClass($objectFQN); |
87
|
2 |
|
} |
88
|
6 |
|
break; |
89
|
12 |
|
case T_NAMESPACE: |
90
|
12 |
|
$namespace = '\\' . ltrim($this->fetch($tokens, array(T_STRING, T_NS_SEPARATOR)), '\\'); |
91
|
12 |
|
$res[$namespace] = array(); |
92
|
12 |
|
$uses = array(); |
93
|
12 |
|
break; |
94
|
12 |
|
case T_USE: |
95
|
9 |
|
if ($classLevel === 0) { |
96
|
6 |
|
while ($name = $this->fetch($tokens, array(T_STRING, T_NS_SEPARATOR))) { |
97
|
6 |
|
$name = '\\' . ltrim($name, '\\'); |
98
|
6 |
|
if ($this->fetch($tokens, array(T_AS))) { |
99
|
3 |
|
$uses[$this->fetch($tokens, array(T_STRING))] = $name; |
100
|
1 |
|
} else { |
101
|
6 |
|
$uses[$name] = $name; |
102
|
|
|
} |
103
|
6 |
|
if (!$this->fetch($tokens, array(','))) { |
104
|
6 |
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
6 |
|
$res[$namespace] = $uses; |
108
|
2 |
|
} |
109
|
9 |
|
break; |
110
|
12 |
|
case T_CURLY_OPEN: |
111
|
12 |
|
case T_DOLLAR_OPEN_CURLY_BRACES: |
112
|
12 |
|
case '{': |
113
|
9 |
|
$level++; |
114
|
9 |
|
break; |
115
|
12 |
|
case '}': |
116
|
9 |
|
if ($level === $classLevel) { |
117
|
6 |
|
$classLevel = 0; |
118
|
2 |
|
} |
119
|
9 |
|
$level--; |
120
|
9 |
|
break; |
121
|
4 |
|
} |
122
|
4 |
|
} |
123
|
|
|
|
124
|
12 |
|
$this->namespaces = array_keys($res); |
125
|
12 |
|
$this->uses = $res; |
126
|
12 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the token value for the next token of type. |
130
|
|
|
* |
131
|
|
|
* @param array $tokens The tokens array. |
132
|
|
|
* @param array $take The tokens to look for. |
133
|
|
|
* @return null|string |
134
|
|
|
*/ |
135
|
12 |
|
private function fetch(& $tokens, array $take) |
136
|
|
|
{ |
137
|
12 |
|
$res = null; |
138
|
12 |
|
while ($token = current($tokens)) { |
139
|
12 |
|
list($token, $s) = is_array($token) ? $token : array($token, $token); |
140
|
12 |
|
if (in_array($token, $take, true)) { |
141
|
12 |
|
$res .= $s; |
142
|
12 |
|
} elseif (!in_array($token, array(T_DOC_COMMENT, T_WHITESPACE, T_COMMENT), true)) { |
143
|
12 |
|
break; |
144
|
|
|
} |
145
|
12 |
|
next($tokens); |
146
|
4 |
|
} |
147
|
|
|
|
148
|
12 |
|
return $res; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the file path of the reflected file. |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getFilePath() |
157
|
|
|
{ |
158
|
|
|
return $this->reflectedFilePath; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the namespaces defined in the file. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
9 |
|
public function getNamespaces() |
167
|
|
|
{ |
168
|
9 |
|
return $this->namespaces; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the use statements in the file. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
6 |
|
public function getUses() |
177
|
|
|
{ |
178
|
6 |
|
return $this->uses; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the objects found in the file. |
183
|
|
|
* |
184
|
|
|
* @return \Reflector[] |
185
|
|
|
*/ |
186
|
3 |
|
public function getObjects() |
187
|
|
|
{ |
188
|
3 |
|
return $this->objects; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Resolve an alias for an FQN. |
193
|
|
|
* |
194
|
|
|
* @param string $fqn The fully qualified name to get the alias for. |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
3 |
|
public function resolveFqnToAlias($fqn) |
198
|
|
|
{ |
199
|
3 |
|
foreach ($this->getUses() as $namespace => $uses) { |
200
|
3 |
|
$alias = array_search($fqn, $uses, true); |
201
|
3 |
|
if ($alias) { |
202
|
3 |
|
$parts = explode('\\', $alias); |
203
|
|
|
|
204
|
3 |
|
return end($parts); |
205
|
|
|
} |
206
|
1 |
|
} |
207
|
|
|
|
208
|
3 |
|
return $fqn; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|