1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Container; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Gacela\Container\Exception\DependencyInvalidArgumentException; |
9
|
|
|
use Gacela\Container\Exception\DependencyNotFoundException; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use ReflectionFunction; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionNamedType; |
14
|
|
|
use ReflectionParameter; |
15
|
|
|
|
16
|
|
|
use function is_callable; |
17
|
|
|
use function is_object; |
18
|
|
|
use function is_string; |
19
|
|
|
|
20
|
|
|
final class DependencyResolver |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param array<class-string,class-string|callable|object> $bindings |
24
|
|
|
*/ |
25
|
26 |
|
public function __construct( |
26
|
|
|
private array $bindings = [], |
27
|
|
|
) { |
28
|
26 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param class-string|Closure $toResolve |
|
|
|
|
32
|
|
|
* |
33
|
|
|
* @return list<mixed> |
34
|
|
|
*/ |
35
|
26 |
|
public function resolveDependencies(string|Closure $toResolve): array |
36
|
|
|
{ |
37
|
|
|
/** @var list<mixed> $dependencies */ |
38
|
26 |
|
$dependencies = []; |
39
|
|
|
|
40
|
26 |
|
$parameters = $this->getParametersToResolve($toResolve); |
41
|
|
|
|
42
|
26 |
|
foreach ($parameters as $parameter) { |
43
|
|
|
/** @psalm-suppress MixedAssignment */ |
44
|
22 |
|
$dependencies[] = $this->resolveDependenciesRecursively($parameter); |
45
|
|
|
} |
46
|
|
|
|
47
|
20 |
|
return $dependencies; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param class-string|Closure $toResolve |
|
|
|
|
52
|
|
|
* |
53
|
|
|
* @return list<ReflectionParameter> |
54
|
|
|
*/ |
55
|
26 |
|
private function getParametersToResolve(Closure|string $toResolve): array |
56
|
|
|
{ |
57
|
26 |
|
if (is_string($toResolve)) { |
58
|
19 |
|
$reflection = new ReflectionClass($toResolve); |
59
|
19 |
|
$constructor = $reflection->getConstructor(); |
60
|
19 |
|
if (!$constructor) { |
61
|
3 |
|
return []; |
62
|
|
|
} |
63
|
16 |
|
return $constructor->getParameters(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
$reflection = new ReflectionFunction($toResolve); |
67
|
7 |
|
return $reflection->getParameters(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
22 |
|
private function resolveDependenciesRecursively(ReflectionParameter $parameter): mixed |
71
|
|
|
{ |
72
|
22 |
|
$this->checkInvalidArgumentParam($parameter); |
73
|
|
|
|
74
|
|
|
/** @var ReflectionNamedType $paramType */ |
75
|
18 |
|
$paramType = $parameter->getType(); |
76
|
|
|
|
77
|
|
|
/** @var class-string $paramTypeName */ |
78
|
18 |
|
$paramTypeName = $paramType->getName(); |
79
|
18 |
|
if ($parameter->isDefaultValueAvailable()) { |
80
|
12 |
|
return $parameter->getDefaultValue(); |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
return $this->resolveClass($paramTypeName); |
84
|
|
|
} |
85
|
|
|
|
86
|
22 |
|
private function checkInvalidArgumentParam(ReflectionParameter $parameter): void |
87
|
|
|
{ |
88
|
22 |
|
if (!$parameter->hasType()) { |
89
|
2 |
|
throw DependencyInvalidArgumentException::noParameterTypeFor($parameter->getName()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @var ReflectionNamedType $paramType */ |
93
|
20 |
|
$paramType = $parameter->getType(); |
94
|
20 |
|
$paramTypeName = $paramType->getName(); |
95
|
|
|
|
96
|
20 |
|
if ($this->isScalar($paramTypeName) && !$parameter->isDefaultValueAvailable()) { |
97
|
|
|
/** @var ReflectionClass $reflectionClass */ |
98
|
2 |
|
$reflectionClass = $parameter->getDeclaringClass(); |
99
|
2 |
|
throw DependencyInvalidArgumentException::unableToResolve($paramTypeName, $reflectionClass->getName()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
20 |
|
private function isScalar(string $paramTypeName): bool |
104
|
|
|
{ |
105
|
20 |
|
return !class_exists($paramTypeName) |
106
|
20 |
|
&& !interface_exists($paramTypeName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param class-string $paramTypeName |
111
|
|
|
*/ |
112
|
15 |
|
private function resolveClass(string $paramTypeName): mixed |
113
|
|
|
{ |
114
|
|
|
/** @var mixed $bindClass */ |
115
|
15 |
|
$bindClass = $this->bindings[$paramTypeName] ?? null; |
116
|
15 |
|
if (is_callable($bindClass)) { |
117
|
|
|
return $bindClass(); |
118
|
|
|
} |
119
|
|
|
|
120
|
15 |
|
if (is_object($bindClass)) { |
121
|
3 |
|
return $bindClass; |
122
|
|
|
} |
123
|
|
|
|
124
|
12 |
|
$reflection = $this->resolveReflectionClass($paramTypeName); |
125
|
10 |
|
$constructor = $reflection->getConstructor(); |
126
|
10 |
|
if ($constructor === null) { |
127
|
1 |
|
return $reflection->newInstance(); |
128
|
|
|
} |
129
|
|
|
|
130
|
9 |
|
return $this->resolveInnerDependencies($constructor, $reflection); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param class-string $paramTypeName |
135
|
|
|
*/ |
136
|
12 |
|
private function resolveReflectionClass(string $paramTypeName): ReflectionClass |
137
|
|
|
{ |
138
|
12 |
|
$reflection = new ReflectionClass($paramTypeName); |
139
|
|
|
|
140
|
12 |
|
if ($reflection->isInstantiable()) { |
141
|
8 |
|
return $reflection; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** @var mixed $concreteClass */ |
145
|
4 |
|
$concreteClass = $this->bindings[$reflection->getName()] ?? null; |
146
|
|
|
|
147
|
4 |
|
if ($concreteClass !== null) { |
148
|
|
|
/** @var class-string $concreteClass */ |
149
|
2 |
|
return new ReflectionClass($concreteClass); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
throw DependencyNotFoundException::mapNotFoundForClassName($reflection->getName()); |
153
|
|
|
} |
154
|
|
|
|
155
|
9 |
|
private function resolveInnerDependencies(ReflectionMethod $constructor, ReflectionClass $reflection): object |
156
|
|
|
{ |
157
|
|
|
/** @var list<mixed> $innerDependencies */ |
158
|
9 |
|
$innerDependencies = []; |
159
|
|
|
|
160
|
9 |
|
foreach ($constructor->getParameters() as $constructorParameter) { |
161
|
9 |
|
$paramType = $constructorParameter->getType(); |
162
|
9 |
|
if ($paramType) { |
163
|
|
|
/** @psalm-suppress MixedAssignment */ |
164
|
9 |
|
$innerDependencies[] = $this->resolveDependenciesRecursively($constructorParameter); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
9 |
|
return $reflection->newInstanceArgs($innerDependencies); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|