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