-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyResolver.php
More file actions
166 lines (143 loc) · 5.51 KB
/
DependencyResolver.php
File metadata and controls
166 lines (143 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Technically\DependencyResolver;
use ArgumentCountError;
use InvalidArgumentException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Technically\CallableReflection\CallableReflection;
use Technically\CallableReflection\Parameters\ParameterReflection;
use Technically\DependencyResolver\Contracts\DependencyResolver as DependencyResolverInterface;
use Technically\DependencyResolver\Exceptions\CannotAutowireArgument;
use Technically\DependencyResolver\Exceptions\CannotAutowireDependencyArgument;
use Technically\DependencyResolver\Exceptions\ClassCannotBeInstantiated;
use Technically\DependencyResolver\Exceptions\DependencyResolutionException;
use Technically\NullContainer\NullContainer;
final class DependencyResolver implements DependencyResolverInterface
{
private ContainerInterface $container;
public function __construct(?ContainerInterface $container = null)
{
$this->container = $container ?: new NullContainer();
}
/**
* @param class-string $className
* @return mixed
*
* @throws InvalidArgumentException If class does not exist.
* @throws ContainerExceptionInterface If error occurs while retrieving the existing entry from the container.
* @throws ClassCannotBeInstantiated If class cannot be instantiated.
* @throws CannotAutowireDependencyArgument If a dependency (of any nesting level) cannot be resolved.
*/
public function resolve(string $className): mixed
{
if (! class_exists($className) && ! interface_exists($className)) {
throw new InvalidArgumentException("`{$className}` is not a valid class name.");
}
if ($this->container->has($className)) {
return $this->container->get($className);
}
return $this->construct($className);
}
/**
* @param class-string $className
* @param array<string,mixed> $bindings
* @return mixed
*
* @throws ClassCannotBeInstantiated
* @throws CannotAutowireDependencyArgument
*/
public function construct(string $className, array $bindings = []): mixed
{
if (! class_exists($className) && ! interface_exists($className)) {
throw new InvalidArgumentException("`{$className}` is not a valid class name.");
}
try {
$reflection = CallableReflection::fromConstructor($className);
} catch (InvalidArgumentException $exception) {
throw new ClassCannotBeInstantiated($className);
}
try {
$values = $this->resolveParameters($reflection->getParameters(), $bindings);
} catch (CannotAutowireArgument $exception) {
throw new CannotAutowireDependencyArgument($className, $exception->getArgumentName(), $exception);
}
return $reflection->apply($values ?? []);
}
/**
* @template T
* @param callable():T $callable
* @param array<string,mixed> $bindings
* @return T
*
* @throws ArgumentCountError
* @throws CannotAutowireArgument
*/
public function call(callable $callable, array $bindings = []): mixed
{
$reflection = CallableReflection::fromCallable($callable);
$values = $this->resolveParameters($reflection->getParameters(), $bindings);
return $reflection->apply($values);
}
/**
* @param ParameterReflection[] $parameters
* @param array<string,mixed> $bindings
* @return array
*
* @throws CannotAutowireArgument
*/
private function resolveParameters(array $parameters, array $bindings = []): array
{
$values = [];
foreach ($parameters as $i => $parameter) {
if (array_key_exists($i, $bindings)) {
$values[] = $bindings[$i];
continue;
}
if (array_key_exists($parameter->getName(), $bindings)) {
if ($parameter->isVariadic()) {
$values = array_merge($values, $bindings[$parameter->getName()]);
} else {
$values[] = $bindings[$parameter->getName()];
}
continue;
}
$values[] = $this->resolveParameter($parameter);
}
return $values;
}
/**
* @param ParameterReflection $parameter
* @return mixed|null
*
* @throws CannotAutowireArgument
*/
private function resolveParameter(ParameterReflection $parameter): mixed
{
foreach ($parameter->getTypes() as $type) {
if ($type->isClassRequirement() && $this->container->has($class = $type->getClassRequirement())) {
try {
return $this->container->get($class);
} catch (ContainerExceptionInterface $exception) {
throw new CannotAutowireArgument($parameter->getName());
}
}
}
if ($parameter->isOptional()) {
return $parameter->getDefaultValue();
}
if ($parameter->isNullable() && count($parameter->getTypes()) > 0) {
return null;
}
foreach ($parameter->getTypes() as $type) {
if ($type->isClassRequirement()) {
$class = $type->getClassRequirement();
try {
return $this->construct($class);
} catch (DependencyResolutionException $exception) {
// try another one
}
}
}
throw new CannotAutowireArgument($parameter->getName());
}
}