|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\ReflectionFactory; |
|
6
|
|
|
|
|
7
|
|
|
use Jasny\ReflectionFactory\ReflectionFactoryInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Factory to use in dependency injection when using PHP Reflection. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ReflectionFactory implements ReflectionFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Create a ReflectionClass. |
|
16
|
|
|
* @see \ReflectionClass |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|object $class Either a string containing the name of the class to reflect, or an object. |
|
19
|
|
|
* @return \ReflectionClass |
|
20
|
|
|
* @throws \ReflectionException |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function reflectClass($class): \ReflectionClass |
|
23
|
|
|
{ |
|
24
|
2 |
|
return new \ReflectionClass($class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a ReflectionClassConstant. |
|
29
|
|
|
* @see \ReflectionClassConstant |
|
30
|
|
|
* |
|
31
|
|
|
* @param string|object $class Either a string containing the name of the class to reflect, or an object. |
|
32
|
|
|
* @param string $name The name of the class constant. |
|
33
|
|
|
* @return \ReflectionClassConstant |
|
34
|
|
|
* @throws \ReflectionException |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function reflectClassConstant($class, string $name): \ReflectionClassConstant |
|
37
|
|
|
{ |
|
38
|
2 |
|
return new \ReflectionClassConstant($class, $name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a ReflectionZendExtension. |
|
43
|
|
|
* @see \ReflectionZendExtension |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $name Name of the extension. |
|
46
|
|
|
* @return \ReflectionZendExtension |
|
47
|
|
|
* @throws \ReflectionException |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function reflectZendExtension(string $name): \ReflectionZendExtension |
|
50
|
|
|
{ |
|
51
|
1 |
|
return new \ReflectionZendExtension($name); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a ReflectionExtension. |
|
56
|
|
|
* @see \ReflectionExtension |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name Name of the extension. |
|
59
|
|
|
* @return \ReflectionExtension |
|
60
|
|
|
* @throws \ReflectionException |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function reflectExtension(string $name): \ReflectionExtension |
|
63
|
|
|
{ |
|
64
|
1 |
|
return new \ReflectionExtension($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create a ReflectionFunction. |
|
69
|
|
|
* @see \ReflectionFunction |
|
70
|
|
|
* |
|
71
|
|
|
* @param string|\Closure $name The name of the function to reflect or a closure. |
|
72
|
|
|
* @return \ReflectionFunction |
|
73
|
|
|
* @throws \ReflectionException |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function reflectFunction($name): \ReflectionFunction |
|
76
|
|
|
{ |
|
77
|
2 |
|
return new \ReflectionFunction($name); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create a ReflectionMethod. |
|
82
|
|
|
* @see \ReflectionMethod |
|
83
|
|
|
* |
|
84
|
|
|
* @param string|object $class Classname, class method or object (instance of the class) that contains the method. |
|
85
|
|
|
* @param string $name Name of the method. |
|
86
|
|
|
* @return \ReflectionMethod |
|
87
|
|
|
* @throws \ReflectionException |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function reflectMethod($class, string $name = null): \ReflectionMethod |
|
90
|
|
|
{ |
|
91
|
3 |
|
return isset($name) ? new \ReflectionMethod($class, $name) : new \ReflectionMethod($class); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Create a ReflectionObject. |
|
96
|
|
|
* @see \ReflectionObject |
|
97
|
|
|
* |
|
98
|
|
|
* @param object $object An object instance. |
|
99
|
|
|
* @return \ReflectionObject |
|
100
|
|
|
* @throws \ReflectionException |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function reflectObject($object): \ReflectionObject |
|
103
|
|
|
{ |
|
104
|
1 |
|
return new \ReflectionObject($object); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Create a ReflectionParameter. |
|
109
|
|
|
* @see \ReflectionParameter |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|array $function The function (string) or method (array) to reflect parameters from. |
|
112
|
|
|
* @param string $parameter The parameter name. |
|
113
|
|
|
* @return \ReflectionParameter |
|
114
|
|
|
* @throws \ReflectionException |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function reflectParameter($function, string $parameter): \ReflectionParameter |
|
117
|
|
|
{ |
|
118
|
3 |
|
return new \ReflectionParameter($function, $parameter); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Create a ReflectionProperty. |
|
123
|
|
|
* @see \ReflectionProperty |
|
124
|
|
|
* |
|
125
|
|
|
* @param string|object $class The class name, that contains the property. |
|
126
|
|
|
* @param string $name The name of the property being reflected. |
|
127
|
|
|
* @return \ReflectionProperty |
|
128
|
|
|
* @throws \ReflectionException |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function reflectProperty($class, string $name): \ReflectionProperty |
|
131
|
|
|
{ |
|
132
|
3 |
|
return new \ReflectionProperty($class, $name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Create a ReflectionGenerator. |
|
137
|
|
|
* @see \ReflectionGenerator |
|
138
|
|
|
* |
|
139
|
|
|
* @param \Generator $generator A generator object. |
|
140
|
|
|
* @return \ReflectionGenerator |
|
141
|
|
|
* @throws \ReflectionException |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function reflectGenerator(\Generator $generator): \ReflectionGenerator |
|
144
|
|
|
{ |
|
145
|
1 |
|
return new \ReflectionGenerator($generator); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Return true if the given function has been defined. |
|
151
|
|
|
* @see function_exists() |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $name The function name, as a string. |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function functionExists(string $name): bool |
|
157
|
|
|
{ |
|
158
|
1 |
|
return function_exists($name); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Checks if the class has been defined. |
|
163
|
|
|
* @see class_exists() |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $class The class name. The name is matched in a case-insensitive manner. |
|
166
|
|
|
* @param bool $autoload Whether or not to call autoload by default. |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function classExists(string $class, bool $autoload = true): bool |
|
170
|
|
|
{ |
|
171
|
1 |
|
return class_exists($class, $autoload); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Checks if the class method exists. |
|
176
|
|
|
* @see method_exists() |
|
177
|
|
|
* |
|
178
|
|
|
* @param string|object $class The class name or an object of the class to test for. |
|
179
|
|
|
* @param string $method The method name. |
|
180
|
|
|
* @return bool |
|
181
|
|
|
*/ |
|
182
|
2 |
|
public function methodExists($class, string $method): bool |
|
183
|
|
|
{ |
|
184
|
2 |
|
return method_exists($class, $method); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Checks if the object or class has a property. |
|
189
|
|
|
* @see property_exists() |
|
190
|
|
|
* |
|
191
|
|
|
* @param string|object $class The class name or an object of the class to test for. |
|
192
|
|
|
* @param string $property The name of the property |
|
193
|
|
|
* @return bool|null |
|
194
|
|
|
*/ |
|
195
|
3 |
|
public function propertyExists($class, string $property): ?bool |
|
196
|
|
|
{ |
|
197
|
3 |
|
return property_exists($class, $property); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Find out whether an extension is loaded. |
|
202
|
|
|
* @see extension_loaded() |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $name The extension name. |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function extensionLoaded(string $name): bool |
|
208
|
|
|
{ |
|
209
|
1 |
|
return extension_loaded($name); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Checks if the object is of this class or has this class as one of its parents. |
|
214
|
|
|
* @see is_a() |
|
215
|
|
|
* |
|
216
|
|
|
* @param object|string $object Object instance or class name. |
|
217
|
|
|
* @param string $class The class or interface name. |
|
218
|
|
|
* @param bool $allow_string Allow class name as `object` |
|
219
|
|
|
* @return bool |
|
220
|
|
|
*/ |
|
221
|
1 |
|
public function isA($object, string $class, bool $allow_string = false): bool |
|
222
|
|
|
{ |
|
223
|
1 |
|
return is_a($object, $class, $allow_string); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Verify that the contents of a variable can be called as a function. |
|
228
|
|
|
* @see is_callable() |
|
229
|
|
|
* |
|
230
|
|
|
* @param mixed $var The value to check. |
|
231
|
|
|
* @param bool $syntaxOnly If set to TRUE the function only verifies that name might be a function or method. |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
2 |
|
public function isCallable($var, bool $syntaxOnly = false): bool |
|
235
|
|
|
{ |
|
236
|
2 |
|
return is_callable($var, $syntaxOnly); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|