1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cekta\DI; |
6
|
|
|
|
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
|
11
|
|
|
class Reflection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ReflectionTransformer[] |
15
|
|
|
*/ |
16
|
|
|
private $transformers; |
17
|
|
|
|
18
|
|
|
public function __construct(ReflectionTransformer ...$tranfromers) |
19
|
|
|
{ |
20
|
|
|
$this->transformers = $tranfromers; |
21
|
21 |
|
} |
22
|
|
|
|
23
|
21 |
|
/** |
24
|
21 |
|
* @param string $name |
25
|
|
|
* @return string[] |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
public function getDependencies(string $name): array |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
15 |
|
$class = new ReflectionClass($name); |
32
|
|
|
$dependencies = self::getMethodDependencies($class->getConstructor()); |
33
|
15 |
|
return $this->tranform($class->getName(), $dependencies); |
34
|
12 |
|
} catch (ReflectionException $exception) { |
35
|
|
|
return []; |
36
|
15 |
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return bool |
42
|
|
|
* @internal |
43
|
|
|
*/ |
44
|
6 |
|
public function isVariadic(string $name): bool |
45
|
|
|
{ |
46
|
6 |
|
try { |
47
|
3 |
|
$class = new ReflectionClass($name); |
48
|
|
|
if ($class->getConstructor() !== null) { |
49
|
6 |
|
$parameters = $class->getConstructor()->getParameters(); |
50
|
|
|
$count = count($parameters); |
51
|
|
|
return $count > 0 ? $parameters[$count - 1]->isVariadic() : false; |
52
|
|
|
} |
53
|
|
|
return false; |
54
|
|
|
} catch (ReflectionException $exception) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
6 |
|
} |
58
|
|
|
|
59
|
6 |
|
/** |
60
|
6 |
|
* @param string $name |
61
|
|
|
* @return bool |
62
|
6 |
|
* @internal |
63
|
|
|
*/ |
64
|
|
|
public function isInstantiable(string $name): bool |
65
|
21 |
|
{ |
66
|
|
|
try { |
67
|
|
|
$class = new ReflectionClass($name); |
68
|
21 |
|
return $class->isInstantiable(); |
69
|
18 |
|
} catch (ReflectionException $exception) { |
70
|
18 |
|
return false; |
71
|
18 |
|
} |
72
|
18 |
|
} |
73
|
3 |
|
|
74
|
3 |
|
private static function getMethodDependencies(?ReflectionMethod $method): array |
75
|
3 |
|
{ |
76
|
3 |
|
$parameters = []; |
77
|
|
|
if ($method !== null) { |
78
|
21 |
|
$annotations = self::getAnnotationParameters((string) $method->getDocComment()); |
79
|
|
|
foreach ($method->getParameters() as $parameter) { |
80
|
18 |
|
$class = $parameter->getClass(); |
81
|
|
|
$name = $class && $parameter->isVariadic() !== true ? $class->name : $parameter->name; |
82
|
18 |
|
$parameters[$name] = array_key_exists($name, $annotations) ? $annotations[$name] : $name; |
83
|
18 |
|
} |
84
|
18 |
|
} |
85
|
15 |
|
return array_values($parameters); |
86
|
15 |
|
} |
87
|
15 |
|
|
88
|
15 |
|
private function tranform(string $name, array $params) |
89
|
|
|
{ |
90
|
|
|
foreach ($this->transformers as $tranfromer) { |
91
|
18 |
|
$params = $tranfromer->transform($name, $params); |
92
|
|
|
} |
93
|
|
|
return $params; |
94
|
18 |
|
} |
95
|
|
|
|
96
|
18 |
|
private static function getAnnotationParameters(string $comment) |
97
|
3 |
|
{ |
98
|
|
|
$result = []; |
99
|
18 |
|
$matches = []; |
100
|
|
|
preg_match_all("/@inject \\\\?([\w\d\\\\]*) \\$([\w\d]*)/", $comment, $matches, PREG_SET_ORDER); |
101
|
|
|
foreach ($matches as $match) { |
102
|
|
|
$result[$match[2]] = $match[1]; |
103
|
|
|
} |
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|