|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\ClassResolver\DependencyResolver; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use ReflectionNamedType; |
|
10
|
|
|
use ReflectionParameter; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
use function is_callable; |
|
13
|
|
|
|
|
14
|
|
|
final class DependencyResolver |
|
15
|
|
|
{ |
|
16
|
|
|
private GacelaConfigFile $gacelaConfigFile; |
|
17
|
|
|
|
|
18
|
12 |
|
public function __construct(GacelaConfigFile $gacelaConfigFile) |
|
19
|
|
|
{ |
|
20
|
12 |
|
$this->gacelaConfigFile = $gacelaConfigFile; |
|
21
|
12 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param class-string $resolvedClassName |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* @return list<mixed> |
|
27
|
|
|
*/ |
|
28
|
12 |
|
public function resolveDependencies(string $resolvedClassName): array |
|
29
|
|
|
{ |
|
30
|
12 |
|
$reflection = new ReflectionClass($resolvedClassName); |
|
31
|
12 |
|
$constructor = $reflection->getConstructor(); |
|
32
|
12 |
|
if (!$constructor) { |
|
33
|
11 |
|
return []; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @var list<mixed> $dependencies */ |
|
37
|
1 |
|
$dependencies = []; |
|
38
|
1 |
|
foreach ($constructor->getParameters() as $parameter) { |
|
39
|
1 |
|
$paramType = $parameter->getType(); |
|
40
|
1 |
|
if ($paramType) { |
|
41
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
42
|
1 |
|
$dependencies[] = $this->resolveDependenciesRecursively($parameter); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return $dependencies; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
1 |
|
private function resolveDependenciesRecursively(ReflectionParameter $parameter) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (!$parameter->hasType()) { |
|
55
|
|
|
throw new RuntimeException("No parameter type for '{$parameter->getName()}'"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var ReflectionNamedType $paramType */ |
|
59
|
1 |
|
$paramType = $parameter->getType(); |
|
60
|
1 |
|
$paramTypeName = $paramType->getName(); |
|
61
|
1 |
|
if (!class_exists($paramTypeName) && !interface_exists($paramTypeName)) { |
|
62
|
|
|
if ($parameter->isDefaultValueAvailable()) { |
|
63
|
|
|
return $parameter->getDefaultValue(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var ReflectionClass $reflectionClass */ |
|
67
|
|
|
$reflectionClass = $parameter->getDeclaringClass(); |
|
68
|
|
|
throw new RuntimeException("Unable to resolve [$parameter] from {$reflectionClass->getName()}"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @var mixed $mappedClass */ |
|
72
|
1 |
|
$mappedClass = $this->gacelaConfigFile->getMappingInterface($paramTypeName); |
|
73
|
1 |
|
if (is_callable($mappedClass)) { |
|
74
|
1 |
|
return $mappedClass(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if (is_object($mappedClass)) { |
|
78
|
1 |
|
return $mappedClass; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$reflection = $this->resolveReflectionClass($paramTypeName); |
|
82
|
1 |
|
$constructor = $reflection->getConstructor(); |
|
83
|
1 |
|
if (!$constructor) { |
|
84
|
1 |
|
return $reflection->newInstance(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @var list<mixed> $innerDependencies */ |
|
88
|
1 |
|
$innerDependencies = []; |
|
89
|
|
|
|
|
90
|
1 |
|
foreach ($constructor->getParameters() as $constructorParameter) { |
|
91
|
1 |
|
$paramType = $constructorParameter->getType(); |
|
92
|
1 |
|
if ($paramType) { |
|
93
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
94
|
1 |
|
$innerDependencies[] = $this->resolveDependenciesRecursively($constructorParameter); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $reflection->newInstanceArgs($innerDependencies); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param class-string $paramTypeName |
|
|
|
|
|
|
103
|
|
|
*/ |
|
104
|
1 |
|
private function resolveReflectionClass(string $paramTypeName): ReflectionClass |
|
105
|
|
|
{ |
|
106
|
1 |
|
$reflection = new ReflectionClass($paramTypeName); |
|
107
|
|
|
|
|
108
|
1 |
|
if ($reflection->isInstantiable()) { |
|
109
|
1 |
|
return $reflection; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** @var mixed $concreteClass */ |
|
113
|
1 |
|
$concreteClass = $this->gacelaConfigFile->getMappingInterface($reflection->getName()); |
|
114
|
|
|
|
|
115
|
1 |
|
if (null !== $concreteClass) { |
|
116
|
|
|
/** @var class-string $concreteClass */ |
|
117
|
1 |
|
return new ReflectionClass($concreteClass); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
throw DependencyResolverNotFoundException::forClassName($reflection->getName()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|