|
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
|
|
|
* @param string $name |
|
15
|
|
|
* @return string[] |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
15 |
|
public function getDependencies(string $name): array |
|
19
|
|
|
{ |
|
20
|
|
|
try { |
|
21
|
15 |
|
$class = new ReflectionClass($name); |
|
22
|
12 |
|
return self::getMethodDependencies($class->getConstructor()); |
|
23
|
3 |
|
} catch (ReflectionException $exception) { |
|
24
|
3 |
|
return []; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @return bool |
|
31
|
|
|
* @internal |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function isVariadic(string $name): bool |
|
34
|
|
|
{ |
|
35
|
|
|
try { |
|
36
|
9 |
|
$class = new ReflectionClass($name); |
|
37
|
6 |
|
if ($class->getConstructor() !== null) { |
|
38
|
6 |
|
$parameters = $class->getConstructor()->getParameters(); |
|
39
|
6 |
|
$count = count($parameters); |
|
40
|
6 |
|
return $count > 0 ? $parameters[$count - 1]->isVariadic() : false; |
|
41
|
|
|
} |
|
42
|
3 |
|
return false; |
|
43
|
3 |
|
} catch (ReflectionException $exception) { |
|
44
|
3 |
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @return bool |
|
51
|
|
|
* @internal |
|
52
|
|
|
*/ |
|
53
|
6 |
|
public function isInstantiable(string $name): bool |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
6 |
|
$class = new ReflectionClass($name); |
|
57
|
3 |
|
return $class->isInstantiable(); |
|
58
|
3 |
|
} catch (ReflectionException $exception) { |
|
59
|
3 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ReflectionMethod|null $method |
|
65
|
|
|
* @return array<string> |
|
66
|
|
|
*/ |
|
67
|
12 |
|
private static function getMethodDependencies(?ReflectionMethod $method): array |
|
68
|
|
|
{ |
|
69
|
12 |
|
$parameters = []; |
|
70
|
12 |
|
if ($method !== null) { |
|
71
|
9 |
|
$annotations = self::getAnnotationParameters((string) $method->getDocComment()); |
|
72
|
9 |
|
foreach ($method->getParameters() as $parameter) { |
|
73
|
9 |
|
$class = $parameter->getClass(); |
|
74
|
9 |
|
$name = $class && $parameter->isVariadic() !== true ? $class->name : $parameter->name; |
|
75
|
9 |
|
$parameters[$name] = array_key_exists($name, $annotations) ? $annotations[$name] : $name; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
12 |
|
return array_values($parameters); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $comment |
|
83
|
|
|
* @return array<string> |
|
84
|
|
|
*/ |
|
85
|
9 |
|
private static function getAnnotationParameters(string $comment): array |
|
86
|
|
|
{ |
|
87
|
9 |
|
$result = []; |
|
88
|
9 |
|
$matches = []; |
|
89
|
9 |
|
preg_match_all("/@inject \\\\?([\w\d\\\\]*) \\$([\w\d]*)/", $comment, $matches, PREG_SET_ORDER); |
|
90
|
9 |
|
foreach ($matches as $match) { |
|
91
|
3 |
|
$result[$match[2]] = $match[1]; |
|
92
|
|
|
} |
|
93
|
9 |
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|