-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflection.php
executable file
·325 lines (299 loc) · 10.3 KB
/
Reflection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
declare(strict_types=1);
namespace MaplePHP\Container;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use MaplePHP\Container\Exceptions\NotFoundException;
class Reflection
{
private ?string $method = null;
private ReflectionClass $reflect;
private ?array $args = null;
private bool $allowInterfaces = true;
private ?string $dependMethod = null;
private static array $class = [];
private static ?array $interfaceFactory = null;
//private static array $attr = [];
//private static $interfaceProtocol;
/**
* Start reflection of a class or method
* @param class-string|object $classData
* @throws ReflectionException
*/
public function __construct(string|object $classData)
{
if (is_string($classData)) {
if (($pos = strpos($classData, "::")) !== false) {
$classData = substr($classData, 0, $pos);
$this->method = substr($classData, $pos + 2);
}
if (!class_exists($classData)) {
throw new NotFoundException("Could not find the class \"$classData\".", 1);
}
}
$this->reflect = new ReflectionClass($classData);
}
/**
* If the dependency injector tries to read an interface in controller it
* will search for the object in interfaceFactory.
* @param callable $call
* @return void
*/
public static function interfaceFactory(callable $call): void
{
self::$interfaceFactory[] = function ($short, $class, $reflect) use ($call) {
//self::$interfaceProtocol[$short] = $call($class, $short, $reflect);
return $call($short, $class, $reflect);
};
}
/**
* Allow interfaces
* @param bool $bool
* @return void
*/
public function allowInterfaces(bool $bool): void
{
$this->allowInterfaces = $bool;
}
/**
* Call dependency injector
* @return object
* @throws ReflectionException|Exception
*/
public function dependencyInjector(?object $class = null, ?string $method = null): mixed
{
$args = [];
$constructor = $this->setDependMethod($method, $this->reflect);
if(!is_null($constructor)) {
$params = $constructor->getParameters();
$this->injectRecursion($params, $this->reflect->getName());
foreach ($params as $param) {
if ($param->getType() && !$param->getType()->isBuiltin()) {
$classKey = $param->getType()->getName();
if (isset(self::$class[$classKey])) {
$args[] = self::$class[$classKey];
}
}
}
}
if(!is_null($this->dependMethod)) {
$this->dependMethod = null;
return $constructor->invokeArgs($class, $args);
}
return $this->reflect->newInstanceArgs($args);
}
/**
* Set dependent method
* @param string|null $method
* @param ReflectionClass $inst
* @return ReflectionMethod|null
* @throws ReflectionException
*/
public function setDependMethod(?string $method, ReflectionClass $inst): ?ReflectionMethod
{
$method = ($method === "constructor") ? null : $method;
$this->dependMethod = $method;
if(is_null($this->dependMethod)) {
return $inst->getConstructor();
}
return $inst->getMethod($this->dependMethod);
}
/**
* This will return reflection if class exist or error pointing to file where error existed,
* @param class-string $className
* @param class-string $fromClass
* @return ReflectionClass
* @throws Exception
*/
private function initReclusiveReflect(string $className, string $fromClass): ReflectionClass
{
try {
return new ReflectionClass($className);
} catch (Exception $e) {
if (!class_exists($className)) {
throw new NotFoundException('Class "' . $className . '" does not exist in the class "' . $fromClass . '".', 1);
} else {
throw new Exception($e->getMessage() . '. You might want to check the file ' . $fromClass . '.', 1);
}
}
}
/**
* Recursion inject dependencies
* @param array $params
* @param class-string $fromClass
* @param array $_args
* @return array
* @throws Exception
*/
private function injectRecursion(array $params, string $fromClass, array $_args = []): array
{
$_args = [];
foreach ($params as $param) {
if ($param->getType() && !$param->getType()->isBuiltin()) {
$classNameA = $param->getType()->getName();
$inst = $this->initReclusiveReflect($classNameA, $fromClass);
$reflectParam = [];
$constructor = $inst->getConstructor();
if (!$inst->isInterface()) {
$reflectParam = ($constructor) ? $constructor->getParameters() : [];
}
if (count($reflectParam) > 0) {
$_args = $this->injectRecursion($reflectParam, $inst->getName(), $_args);
// Will make it possible to set same instance in multiple nested classes
$_args = $this->insertMultipleNestedClasses($inst, $constructor, $classNameA, $reflectParam);
} else {
if ($inst->isInterface()) {
$this->insertInterfaceClasses($inst, $classNameA);
} else {
if (empty(self::$class[$classNameA])) {
self::$class[$classNameA] = $this->newInstance($inst, (bool)$constructor, $_args);
}
}
$_args[] = self::$class[$classNameA];
}
}
}
return $_args;
}
/**
* Will insert interface classes (the default classes)
* @param ReflectionClass $inst
* @param string $classNameA
* @return void
*/
private function insertInterfaceClasses(ReflectionClass $inst, string $classNameA): void
{
if ($this->allowInterfaces) {
if (!is_null(self::$interfaceFactory)) {
foreach (self::$interfaceFactory as $call) {
self::$class[$classNameA] = $call($inst->getShortName(), $classNameA, $inst);
}
}
} else {
self::$class[$classNameA] = null;
}
}
/**
* Will make it possible to set same instance in multiple nested classes
* @param ReflectionClass $inst
* @param ReflectionMethod|null $constructor
* @param string $classNameA
* @param array $reflectParam
* @return array
* @throws ReflectionException
*/
private function insertMultipleNestedClasses(
ReflectionClass $inst,
?ReflectionMethod $constructor,
string $classNameA,
array $reflectParam
): array {
$args = [];
foreach ($reflectParam as $reflectInstance) {
if ($reflectInstance->getType() && !$reflectInstance->getType()->isBuiltin()) {
$classNameB = $reflectInstance->getType()->getName();
if (isset(self::$class[$classNameB])) {
$args[] = self::$class[$classNameB];
}
}
}
if (empty(self::$class[$classNameA])) {
self::$class[$classNameA] = $this->newInstance($inst, (bool)$constructor, $args);
}
return $args;
}
/**
* Create an instance from reflection
* @param ReflectionClass $inst
* @param bool $hasCon
* @param array $args
* @return object
* @throws ReflectionException
*/
public function newInstance(ReflectionClass $inst, bool $hasCon, array $args): object
{
if ($hasCon) {
return $inst->newInstanceArgs($args);
}
return $inst->newInstance();
}
/**
* Set arguments to constructor or method (depending on how $data in new Reflection($data) is defined).
* IF method is set then method arguments will be passed, (the method will be treated as a static method)
* @param array $array [description]
*/
public function setArgs(array $array): self
{
$this->args = $array;
return $this;
}
/**
* Access reflection class
* @return ReflectionClass
*/
public function getReflect(): ReflectionClass
{
return $this->reflect;
}
/**
* Get the loaded container data
* @return mixed
* @throws ReflectionException
* @throws Exception
*/
public function get(): mixed
{
if (!is_null($this->method)) {
$method = $this->reflect->getMethod($this->method);
if ($method->isConstructor()) {
return $this->getClass();
}
if ($method->isDestructor()) {
throw new Exception("You can not set a Destructor as a container", 1);
}
$inst = $this->reflect->newInstanceWithoutConstructor();
if (!is_null($this->args)) {
return $method->invokeArgs($inst, $this->args);
} else {
return $method->invoke($inst);
}
}
return $this->getClass();
}
public static function getClassList(): array
{
return self::$class;
}
/**
* Load dependencyInjector / or just a container
* @return object
* @throws ReflectionException
*/
private function getClass(): object
{
if (!is_null($this->args)) {
$inst = $this->reflect->newInstanceArgs($this->args);
} else {
$inst = $this->dependencyInjector();
}
return $inst;
}
/*
// Possible attribute snippet in working progress
function propagateAttr($reflectionClass, $class) {
foreach ($reflectionClass->getMethods() as $method) {
$attributes = $method->getAttributes(ListensTo::class);
foreach ($attributes as $attribute) {
$listener = $attribute->newInstance();
$name = $method->getName();
self::$attr[$name] = [
$listener,
$name
];
}
}
}
*/
}