1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\container; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionException; |
8
|
|
|
use ReflectionMethod; |
9
|
|
|
use ReflectionParameter; |
10
|
|
|
use ReflectionProperty; |
11
|
|
|
|
12
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Mirror |
18
|
|
|
* Utility class for obtaining details about classes using PHP Reflection. |
19
|
|
|
* To prevent extra processing, all generated reflectors are cached for reuse. |
20
|
|
|
* |
21
|
|
|
* @package EventEspresso\core\services\container |
22
|
|
|
* @author Brent Christensen |
23
|
|
|
* @since $VID:$ |
24
|
|
|
*/ |
25
|
|
|
class Mirror |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ReflectionClass[] $classes |
30
|
|
|
*/ |
31
|
|
|
private $classes = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ReflectionMethod[] $constructors |
35
|
|
|
*/ |
36
|
|
|
private $constructors = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ReflectionParameter[][] $parameters |
40
|
|
|
*/ |
41
|
|
|
private $parameters = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ReflectionParameter[][] $parameters |
45
|
|
|
*/ |
46
|
|
|
private $parameter_classes = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ReflectionProperty[][] $properties |
50
|
|
|
*/ |
51
|
|
|
private $properties = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ReflectionMethod[][] $methods |
55
|
|
|
*/ |
56
|
|
|
private $methods = array(); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $class_name |
61
|
|
|
* @return ReflectionClass |
62
|
|
|
* @throws ReflectionException |
63
|
|
|
* @throws InvalidDataTypeException |
64
|
|
|
*/ |
65
|
|
|
public function getReflectionClass($class_name) |
66
|
|
|
{ |
67
|
|
|
if (! is_string($class_name)) { |
68
|
|
|
throw new InvalidDataTypeException($class_name, '$class_name', 'string (fully qualified class name)'); |
69
|
|
|
} |
70
|
|
|
if (! isset($this->classes[ $class_name ])) { |
71
|
|
|
$this->classes[ $class_name ] = new ReflectionClass($class_name); |
72
|
|
|
} |
73
|
|
|
return $this->classes[ $class_name ]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $class_name |
79
|
|
|
* @return ReflectionMethod |
80
|
|
|
* @throws InvalidDataTypeException |
81
|
|
|
* @throws ReflectionException |
82
|
|
|
*/ |
83
|
|
|
public function getConstructor($class_name) |
84
|
|
|
{ |
85
|
|
|
if (! is_string($class_name)) { |
86
|
|
|
throw new InvalidDataTypeException($class_name, '$class_name', 'string (fully qualified class name)'); |
87
|
|
|
} |
88
|
|
|
if (! isset($this->constructors[ $class_name ])) { |
89
|
|
|
$reflection_class = $this->getReflectionClass($class_name); |
90
|
|
|
$this->constructors[ $class_name ] = $reflection_class->getConstructor(); |
91
|
|
|
} |
92
|
|
|
return $this->constructors[ $class_name ]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ReflectionClass $reflection_class |
98
|
|
|
* @return ReflectionMethod |
99
|
|
|
* @throws InvalidDataTypeException |
100
|
|
|
* @throws ReflectionException |
101
|
|
|
*/ |
102
|
|
|
public function getConstructorFromReflection(ReflectionClass $reflection_class) |
103
|
|
|
{ |
104
|
|
|
return $this->getConstructor($reflection_class->getName()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $class_name |
110
|
|
|
* @return ReflectionParameter[] |
111
|
|
|
* @throws InvalidDataTypeException |
112
|
|
|
* @throws ReflectionException |
113
|
|
|
*/ |
114
|
|
|
public function getParameters($class_name) |
115
|
|
|
{ |
116
|
|
|
if (! isset($this->parameters[ $class_name ])) { |
117
|
|
|
$constructor = $this->getConstructor($class_name); |
118
|
|
|
$this->parameters[ $class_name ] = $constructor->getParameters(); |
119
|
|
|
} |
120
|
|
|
return $this->parameters[ $class_name ]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ReflectionClass $reflection_class |
126
|
|
|
* @return ReflectionParameter[] |
127
|
|
|
* @throws InvalidDataTypeException |
128
|
|
|
* @throws ReflectionException |
129
|
|
|
*/ |
130
|
|
|
public function getParametersFromReflection(ReflectionClass $reflection_class) |
131
|
|
|
{ |
132
|
|
|
return $this->getParameters($reflection_class->getName()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param ReflectionMethod $constructor |
138
|
|
|
* @return ReflectionParameter[] |
139
|
|
|
* @throws InvalidDataTypeException |
140
|
|
|
* @throws ReflectionException |
141
|
|
|
*/ |
142
|
|
|
public function getParametersFromReflectionConstructor(ReflectionMethod $constructor) |
143
|
|
|
{ |
144
|
|
|
return $this->getParameters($constructor->getDeclaringClass()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param ReflectionParameter $param |
150
|
|
|
* @param string $class_name |
151
|
|
|
* @param string $index |
152
|
|
|
* @return string|null |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function getParameterClassName(ReflectionParameter $param, $class_name, $index) |
155
|
|
|
{ |
156
|
|
|
if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_name'])) { |
157
|
|
|
return $this->parameter_classes[ $class_name ][ $index ]['param_class_name']; |
158
|
|
|
} |
159
|
|
|
if (! isset($this->parameter_classes[ $class_name ])) { |
160
|
|
|
$this->parameter_classes[ $class_name ] = array(); |
161
|
|
|
} |
162
|
|
|
if (! isset($this->parameter_classes[ $class_name ][ $index ])) { |
163
|
|
|
$this->parameter_classes[ $class_name ][ $index ] = array(); |
164
|
|
|
} |
165
|
|
|
$this->parameter_classes[ $class_name ][ $index ]['param_class_name'] = $param->getClass() |
166
|
|
|
? $param->getClass()->name |
167
|
|
|
: null; |
168
|
|
|
return $this->parameter_classes[ $class_name ][ $index ]['param_class_name']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ReflectionParameter $param |
174
|
|
|
* @param string $class_name |
175
|
|
|
* @param string $index |
176
|
|
|
* @return string|null |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function getParameterDefaultValue(ReflectionParameter $param, $class_name, $index) |
179
|
|
|
{ |
180
|
|
|
if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_default'])) { |
181
|
|
|
return $this->parameter_classes[ $class_name ][ $index ]['param_class_default']; |
182
|
|
|
} |
183
|
|
|
if (! isset($this->parameter_classes[ $class_name ])) { |
184
|
|
|
$this->parameter_classes[ $class_name ] = array(); |
185
|
|
|
} |
186
|
|
|
if (! isset($this->parameter_classes[ $class_name ][ $index ])) { |
187
|
|
|
$this->parameter_classes[ $class_name ][ $index ] = array(); |
188
|
|
|
} |
189
|
|
|
$this->parameter_classes[ $class_name ][ $index ]['param_class_default'] = $param->isDefaultValueAvailable() |
190
|
|
|
? $param->getDefaultValue() |
191
|
|
|
: null; |
192
|
|
|
return $this->parameter_classes[ $class_name ][ $index ]['param_class_default']; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $class_name |
198
|
|
|
* @return ReflectionProperty[] |
199
|
|
|
* @throws InvalidDataTypeException |
200
|
|
|
* @throws ReflectionException |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
public function getProperties($class_name) |
203
|
|
|
{ |
204
|
|
|
if (! isset($this->properties[ $class_name ])) { |
205
|
|
|
$reflection_class = $this->getReflectionClass($class_name); |
206
|
|
|
$this->properties[ $class_name ] = $reflection_class->getProperties(); |
207
|
|
|
} |
208
|
|
|
return $this->properties[ $class_name ]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param ReflectionClass $reflection_class |
214
|
|
|
* @return ReflectionProperty[] |
215
|
|
|
* @throws InvalidDataTypeException |
216
|
|
|
* @throws ReflectionException |
217
|
|
|
*/ |
218
|
|
|
public function getPropertiesFromReflection(ReflectionClass $reflection_class) |
219
|
|
|
{ |
220
|
|
|
return $this->getProperties($reflection_class->getName()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $class_name |
226
|
|
|
* @return ReflectionMethod[] |
227
|
|
|
* @throws InvalidDataTypeException |
228
|
|
|
* @throws ReflectionException |
229
|
|
|
*/ |
230
|
|
View Code Duplication |
public function getMethods($class_name) |
231
|
|
|
{ |
232
|
|
|
if (! isset($this->methods[ $class_name ])) { |
233
|
|
|
$reflection_class = $this->getReflectionClass($class_name); |
234
|
|
|
$this->methods[ $class_name ] = $reflection_class->getMethods(); |
235
|
|
|
} |
236
|
|
|
return $this->methods[ $class_name ]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param ReflectionClass $reflection_class ) |
242
|
|
|
* @return ReflectionMethod[] |
243
|
|
|
* @throws InvalidDataTypeException |
244
|
|
|
* @throws ReflectionException |
245
|
|
|
*/ |
246
|
|
|
public function getMethodsFromReflection(ReflectionClass $reflection_class) |
247
|
|
|
{ |
248
|
|
|
return $this->getMethods($reflection_class->getName()); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|