Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
397 lines (363 loc) · 10.3 KB

File metadata and controls

executable file
·
397 lines (363 loc) · 10.3 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
declare(strict_types=1);
namespace MaplePHP\Container;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use MaplePHP\Container\Exceptions\NotFoundException;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionUnionType;
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;
/**
* 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 the controller,
* it will then wirer it to the expected instance
*
* @param callable $call
* @return void
*/
public static function interfaceFactory(callable $call): void
{
self::$interfaceFactory[] = function ($interfaceClass, $resolvedClass, $reflect) use ($call) {
//self::$interfaceProtocol[$short] = $call($class, $short, $reflect);
return $call($interfaceClass, $resolvedClass, $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 ($constructor !== null) {
$params = $constructor->getParameters();
$this->injectRecursion($params, $this->reflect->getName());
foreach ($params as $param) {
if (!$this->isBuiltin($param)) {
$classKey = $this->getClassNameFromParam($param);
if (!$classKey) {
continue;
}
if (isset(self::$class[$classKey])) {
$args[] = self::$class[$classKey];
}
}
}
}
if ($this->dependMethod !== null) {
$this->dependMethod = null;
return $constructor->invokeArgs($class, $args);
}
return $this->reflect->newInstanceArgs($args);
}
/**
* Set a dependent method to autowire
*
* 1. If the method is null, then it will return the constructor
* 2. If the method is not null, then it will return the 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 ($this->dependMethod === null) {
return $inst->getConstructor();
}
return $inst->getMethod($this->dependMethod);
}
/**
* Check if a parameter type is built-in
*
* @param ReflectionParameter $param Parameter to check
* @return bool Returns true if a parameter type is not built-in, false otherwise
*/
private function isBuiltin(ReflectionParameter $param): bool
{
$type = $param->getType();
return ($type instanceof ReflectionNamedType && $type->isBuiltin());
}
/**
* 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 (!$this->isBuiltin($param)) {
$classNameA = $this->getClassNameFromParam($param);
if (!$classNameA) {
continue; // skip if class name couldn't be resolved
}
$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);
}
}
if (isset(self::$class[$classNameA])) {
$_args[] = self::$class[$classNameA];
}
}
}
}
return $_args;
}
/**
* Extracts the class name from a ReflectionParameter object
* Handles named types, union types, and intersection types (PHP 8.1+)
* Returns the first non-built-in type name found or null if none exists
*
* @param ReflectionParameter $param The reflection parameter to analyze
* @return string|null The extracted class name or null if no class name is found
*/
private function getClassNameFromParam(ReflectionParameter $param): ?string
{
$type = $param->getType();
if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) {
return $type->getName();
}
if ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $innerType) {
if ($innerType instanceof ReflectionNamedType && !$innerType->isBuiltin()) {
return $innerType->getName();
}
}
}
// Optionally handle ReflectionIntersectionType for PHP 8.1+
if (PHP_VERSION_ID >= 80100 && $type instanceof ReflectionIntersectionType) {
foreach ($type->getTypes() as $innerType) {
if ($innerType instanceof ReflectionNamedType && !$innerType->isBuiltin()) {
return $innerType->getName(); // or return all names if needed
}
}
}
return null;
}
/**
* 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 (self::$interfaceFactory !== null) {
foreach (self::$interfaceFactory as $call) {
$getter = $call($inst->getName(), $classNameA, $inst);
if ($getter !== null) {
self::$class[$classNameA] = $getter;
}
}
}
} 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 (!$this->isBuiltin($reflectInstance)) {
$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 the constructor or method (depending on how $data in
* new Reflection($data) is defined). IF the 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;
}
/**
* Check if args has been initiated
*
* @return bool
*/
public function hasArgs(): bool
{
return (is_array($this->args) && count($this->args) > 0);
}
/**
* 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 ($this->method !== null) {
$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 ($this->args !== null) {
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 ($this->args !== null) {
$inst = $this->reflect->newInstanceArgs($this->args);
} else {
$inst = $this->dependencyInjector();
}
return $inst;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.