|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
use Wingu\OctopusCore\Reflection\Exceptions\RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Reflection about a class. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ReflectionClass extends \ReflectionClass |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
use ReflectionDocCommentTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get the body of the class. |
|
17
|
|
|
* |
|
18
|
|
|
* @return string |
|
19
|
|
|
* @throws \Wingu\OctopusCore\Reflection\Exceptions\RuntimeException If the class is internal. |
|
20
|
|
|
*/ |
|
21
|
51 |
|
public function getBody() |
|
22
|
|
|
{ |
|
23
|
51 |
|
$fileName = $this->getFileName(); |
|
24
|
51 |
|
if ($fileName === false) { |
|
25
|
3 |
|
throw new RuntimeException('Can not get body of a class that is internal.'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
48 |
|
$lines = file($fileName, FILE_IGNORE_NEW_LINES); |
|
29
|
48 |
|
$lines = array_slice( |
|
30
|
16 |
|
$lines, |
|
31
|
48 |
|
$this->getStartLine() - 1, |
|
32
|
48 |
|
($this->getEndLine() - $this->getStartLine() + 1), |
|
33
|
32 |
|
true |
|
34
|
16 |
|
); |
|
35
|
48 |
|
$lines = implode("\n", $lines); |
|
36
|
|
|
|
|
37
|
48 |
|
$firstBracketPos = strpos($lines, '{'); |
|
38
|
48 |
|
$lastBracketPost = strrpos($lines, '}'); |
|
39
|
48 |
|
$body = substr($lines, $firstBracketPos + 1, $lastBracketPost - $firstBracketPos - 1); |
|
40
|
|
|
|
|
41
|
48 |
|
return trim(rtrim($body), "\n\r"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Gets the interfaces. |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
|
48
|
|
|
*/ |
|
49
|
12 |
|
public function getInterfaces() |
|
50
|
|
|
{ |
|
51
|
12 |
|
$return = array(); |
|
52
|
|
|
|
|
53
|
12 |
|
foreach ($this->getInterfaceNames() as $interface) { |
|
54
|
6 |
|
$return[$interface] = new static($interface); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
12 |
|
return $return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get interfaces that are implemented directly by the reflected class. |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
|
64
|
|
|
*/ |
|
65
|
6 |
|
public function getOwnInterfaces() |
|
66
|
|
|
{ |
|
67
|
6 |
|
$parent = $this->getParentClass(); |
|
68
|
6 |
|
if ($parent !== false) { |
|
69
|
3 |
|
return array_diff_key($this->getInterfaces(), $parent->getInterfaces()); |
|
70
|
|
|
} else { |
|
71
|
3 |
|
return $this->getInterfaces(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets a reflection on a method. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name The method name. |
|
79
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionMethod |
|
80
|
|
|
*/ |
|
81
|
12 |
|
public function getMethod($name) |
|
82
|
|
|
{ |
|
83
|
12 |
|
return new ReflectionMethod($this->getName(), $name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets a list of methods. |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer $filter Filter for method types. This is an OR filter only. |
|
90
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionMethod[] |
|
91
|
|
|
*/ |
|
92
|
24 |
|
public function getMethods($filter = -1) |
|
93
|
|
|
{ |
|
94
|
24 |
|
$return = parent::getMethods($filter); |
|
95
|
24 |
|
foreach ($return as $key => $val) { |
|
96
|
18 |
|
$return[$key] = new ReflectionMethod($this->getName(), $val->getName()); |
|
97
|
8 |
|
} |
|
98
|
|
|
|
|
99
|
24 |
|
return $return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets a list of own methods (not inherited). |
|
104
|
|
|
* |
|
105
|
|
|
* @param integer $filter Filter for method types. |
|
106
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionMethod[] |
|
107
|
|
|
*/ |
|
108
|
15 |
|
public function getOwnMethods($filter = -1) |
|
109
|
|
|
{ |
|
110
|
15 |
|
$return = $this->getMethods($filter); |
|
111
|
|
|
|
|
112
|
15 |
|
$traitMethods = $traitAliases = array(); |
|
113
|
15 |
|
foreach ($this->getTraits() as $trait) { |
|
114
|
6 |
|
$traitAliases = array_merge($traitAliases, $this->getTraitAliases()); |
|
115
|
6 |
|
foreach ($trait->getMethods($filter) as $method) { |
|
116
|
6 |
|
$traitMethods[] = $method->getName(); |
|
117
|
2 |
|
} |
|
118
|
5 |
|
} |
|
119
|
|
|
|
|
120
|
15 |
|
$traitMethods = array_merge($traitMethods, array_keys($traitAliases)); |
|
121
|
|
|
|
|
122
|
15 |
|
foreach ($return as $key => $val) { |
|
123
|
9 |
|
if ($val->class === $this->getName() && in_array($val->getName(), $traitMethods) === false) { |
|
124
|
9 |
|
$return[$key] = new ReflectionMethod($this->getName(), $val->getName()); |
|
125
|
3 |
|
} else { |
|
126
|
9 |
|
unset($return[$key]); |
|
127
|
|
|
} |
|
128
|
5 |
|
} |
|
129
|
|
|
|
|
130
|
15 |
|
return array_values($return); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the constructor of the class. |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionMethod |
|
137
|
|
|
*/ |
|
138
|
6 |
|
public function getConstructor() |
|
139
|
|
|
{ |
|
140
|
6 |
|
if ($this->hasMethod('__construct') === true) { |
|
141
|
3 |
|
return $this->getMethod('__construct'); |
|
142
|
|
|
} else { |
|
143
|
3 |
|
return null; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Checks if this method belongs to the current class (not inherited). |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $name Name of the method being checked for. |
|
151
|
|
|
* @return boolean |
|
152
|
|
|
*/ |
|
153
|
9 |
|
public function hasOwnMethod($name) |
|
154
|
|
|
{ |
|
155
|
9 |
|
if ($this->hasMethod($name) === true) { |
|
156
|
6 |
|
return $this->getMethod($name)->class === $this->getName(); |
|
157
|
|
|
} else { |
|
158
|
3 |
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the parent class. |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClass |
|
166
|
|
|
*/ |
|
167
|
18 |
|
public function getParentClass() |
|
168
|
|
|
{ |
|
169
|
18 |
|
$parent = parent::getParentClass(); |
|
170
|
|
|
|
|
171
|
18 |
|
return ($parent !== false) ? new static($parent->getName()) : false; |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get a property reflection. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $name Name of the property. |
|
178
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionProperty |
|
179
|
|
|
*/ |
|
180
|
3 |
|
public function getProperty($name) |
|
181
|
|
|
{ |
|
182
|
3 |
|
return new ReflectionProperty($this->getName(), $name); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Gets properties. |
|
187
|
|
|
* |
|
188
|
|
|
* @param integer $filter Filter for the properties. |
|
189
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionProperty[] |
|
190
|
|
|
*/ |
|
191
|
21 |
|
public function getProperties($filter = -1) |
|
192
|
|
|
{ |
|
193
|
21 |
|
$properties = parent::getProperties($filter); |
|
194
|
21 |
|
foreach ($properties as $key => $val) { |
|
195
|
15 |
|
$properties[$key] = new ReflectionProperty($this->getName(), $val->getName()); |
|
196
|
7 |
|
} |
|
197
|
|
|
|
|
198
|
21 |
|
return $properties; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get properties that are defined in the reflected class. |
|
203
|
|
|
* |
|
204
|
|
|
* @param integer $filter Filter for the properties. |
|
205
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionProperty[] |
|
206
|
|
|
*/ |
|
207
|
12 |
|
public function getOwnProperties($filter = -1) |
|
208
|
|
|
{ |
|
209
|
12 |
|
$return = $this->getProperties($filter); |
|
210
|
|
|
|
|
211
|
12 |
|
$traitProperties = array(); |
|
212
|
12 |
|
foreach ($this->getTraits() as $trait) { |
|
213
|
3 |
|
foreach ($trait->getProperties($filter) as $property) { |
|
214
|
3 |
|
$traitProperties[] = $property->getName(); |
|
215
|
1 |
|
} |
|
216
|
4 |
|
} |
|
217
|
|
|
|
|
218
|
12 |
|
foreach ($return as $key => $val) { |
|
219
|
9 |
|
if ($val->class === $this->getName() && in_array($val->getName(), $traitProperties) === false) { |
|
220
|
9 |
|
$return[$key] = new ReflectionProperty($this->getName(), $val->getName()); |
|
221
|
3 |
|
} else { |
|
222
|
9 |
|
unset($return[$key]); |
|
223
|
|
|
} |
|
224
|
4 |
|
} |
|
225
|
|
|
|
|
226
|
12 |
|
return array_values($return); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get the constants that are defined in the class |
|
231
|
|
|
* |
|
232
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionConstant[] the array of constants |
|
233
|
|
|
*/ |
|
234
|
18 |
|
public function getConstants() |
|
235
|
|
|
{ |
|
236
|
18 |
|
$constants = parent::getConstants(); |
|
237
|
18 |
|
$returnConstants = array(); |
|
238
|
18 |
|
foreach ($constants as $key => $value) { |
|
239
|
12 |
|
$returnConstants[$key] = new ReflectionConstant($this->getName(), $key); |
|
240
|
6 |
|
} |
|
241
|
|
|
|
|
242
|
18 |
|
return $returnConstants; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Get constants that are defined directly by the reflected class. |
|
247
|
|
|
* |
|
248
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionConstant[] |
|
249
|
|
|
*/ |
|
250
|
9 |
|
public function getOwnConstants() |
|
251
|
|
|
{ |
|
252
|
9 |
|
if ($this->getParentClass() === false) { |
|
253
|
3 |
|
return $this->getConstants(); |
|
254
|
|
|
} else { |
|
255
|
6 |
|
return array_diff_key($this->getConstants(), $this->getParentClass()->getConstants()); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Returns an array of traits used by this class. |
|
261
|
|
|
* |
|
262
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
|
263
|
|
|
*/ |
|
264
|
30 |
|
public function getTraits() |
|
265
|
|
|
{ |
|
266
|
30 |
|
$return = parent::getTraits(); |
|
267
|
30 |
|
if ($return !== null) { |
|
268
|
30 |
|
foreach ($return as $key => $val) { |
|
269
|
12 |
|
$return[$key] = new static($val->getName()); |
|
270
|
10 |
|
} |
|
271
|
10 |
|
} |
|
272
|
|
|
|
|
273
|
30 |
|
return $return; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Get the use statements. |
|
278
|
|
|
* |
|
279
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionClassUse[] |
|
280
|
|
|
*/ |
|
281
|
3 |
|
public function getUses() |
|
282
|
|
|
{ |
|
283
|
3 |
|
$return = array(); |
|
284
|
3 |
|
foreach ($this->getTraitNames() as $traitName) { |
|
285
|
3 |
|
$return[] = new ReflectionClassUse($this->name, $traitName); |
|
286
|
1 |
|
} |
|
287
|
|
|
|
|
288
|
3 |
|
return $return; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Gets a ReflectionExtension object for the extension which defined the class. |
|
293
|
|
|
* |
|
294
|
|
|
* @return \Wingu\OctopusCore\Reflection\ReflectionExtension |
|
295
|
|
|
*/ |
|
296
|
3 |
|
public function getExtension() |
|
297
|
|
|
{ |
|
298
|
3 |
|
$extensionName = $this->getExtensionName(); |
|
299
|
3 |
|
if ($extensionName !== false) { |
|
300
|
3 |
|
return new ReflectionExtension($extensionName); |
|
301
|
|
|
} else { |
|
302
|
3 |
|
return null; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|