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