|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Injector; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionException; |
|
13
|
|
|
use ReflectionFunction; |
|
14
|
|
|
use ReflectionFunctionAbstract; |
|
15
|
|
|
use ReflectionIntersectionType; |
|
|
|
|
|
|
16
|
|
|
use ReflectionNamedType; |
|
17
|
|
|
use ReflectionParameter; |
|
18
|
|
|
use ReflectionType; |
|
19
|
|
|
use ReflectionUnionType; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Injector is able to analyze callable dependencies based on type hinting and |
|
23
|
|
|
* inject them from any PSR-11 compatible container. |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Injector |
|
26
|
|
|
{ |
|
27
|
|
|
private ?ContainerInterface $container; |
|
28
|
|
|
private bool $cacheReflections = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ReflectionClass[] |
|
32
|
|
|
* @psalm-var array<class-string,ReflectionClass> |
|
33
|
|
|
*/ |
|
34
|
|
|
private array $reflectionsCache = []; |
|
35
|
|
|
|
|
36
|
60 |
|
public function __construct(?ContainerInterface $container = null) |
|
37
|
|
|
{ |
|
38
|
60 |
|
$this->container = $container; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Enable memoization of class reflections for improved performance when resolving the same objects multiple times. |
|
43
|
|
|
* Note: Enabling this feature may increase memory usage. |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function withCacheReflections(bool $cacheReflections = true): self |
|
46
|
|
|
{ |
|
47
|
2 |
|
$new = clone $this; |
|
48
|
2 |
|
$new->cacheReflections = $cacheReflections; |
|
49
|
2 |
|
return $new; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Invoke a callback with resolving dependencies based on parameter types. |
|
54
|
|
|
* |
|
55
|
|
|
* This methods allows invoking a callback and let type hinted parameter names to be |
|
56
|
|
|
* resolved as objects of the Container. It additionally allow calling function passing named arguments. |
|
57
|
|
|
* |
|
58
|
|
|
* For example, the following callback may be invoked using the Container to resolve the formatter dependency: |
|
59
|
|
|
* |
|
60
|
|
|
* ```php |
|
61
|
|
|
* $formatString = function($string, \Yiisoft\I18n\MessageFormatterInterface $formatter) { |
|
62
|
|
|
* // ... |
|
63
|
|
|
* } |
|
64
|
|
|
* |
|
65
|
|
|
* $injector = new Yiisoft\Injector\Injector($container); |
|
66
|
|
|
* $injector->invoke($formatString, ['string' => 'Hello World!']); |
|
67
|
|
|
* ``` |
|
68
|
|
|
* |
|
69
|
|
|
* This will pass the string `'Hello World!'` as the first argument, and a formatter instance created |
|
70
|
|
|
* by the DI container as the second argument. |
|
71
|
|
|
* |
|
72
|
|
|
* @param callable $callable callable to be invoked. |
|
73
|
|
|
* @param array $arguments The array of the function arguments. |
|
74
|
|
|
* This can be either a list of arguments, or an associative array where keys are argument names. |
|
75
|
|
|
* |
|
76
|
|
|
* @throws MissingRequiredArgumentException if required argument is missing. |
|
77
|
|
|
* @throws ContainerExceptionInterface if a dependency cannot be resolved or if a dependency cannot be fulfilled. |
|
78
|
|
|
* @throws ReflectionException |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed The callable return value. |
|
81
|
|
|
*/ |
|
82
|
41 |
|
public function invoke(callable $callable, array $arguments = []) |
|
83
|
|
|
{ |
|
84
|
41 |
|
$callable = Closure::fromCallable($callable); |
|
85
|
41 |
|
$reflection = new ReflectionFunction($callable); |
|
86
|
41 |
|
return $reflection->invokeArgs($this->resolveDependencies($reflection, $arguments)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Creates an object of a given class with resolving constructor dependencies based on parameter types. |
|
91
|
|
|
* |
|
92
|
|
|
* This methods allows invoking a constructor and let type hinted parameter names to be |
|
93
|
|
|
* resolved as objects of the Container. It additionally allow calling constructor passing named arguments. |
|
94
|
|
|
* |
|
95
|
|
|
* For example, the following constructor may be invoked using the Container to resolve the formatter dependency: |
|
96
|
|
|
* |
|
97
|
|
|
* ```php |
|
98
|
|
|
* class StringFormatter |
|
99
|
|
|
* { |
|
100
|
|
|
* public function __construct($string, \Yiisoft\I18n\MessageFormatterInterface $formatter) |
|
101
|
|
|
* { |
|
102
|
|
|
* // ... |
|
103
|
|
|
* } |
|
104
|
|
|
* } |
|
105
|
|
|
* |
|
106
|
|
|
* $injector = new Yiisoft\Injector\Injector($container); |
|
107
|
|
|
* $stringFormatter = $injector->make(StringFormatter::class, ['string' => 'Hello World!']); |
|
108
|
|
|
* ``` |
|
109
|
|
|
* |
|
110
|
|
|
* This will pass the string `'Hello World!'` as the first argument, and a formatter instance created |
|
111
|
|
|
* by the DI container as the second argument. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $class name of the class to be created. |
|
114
|
|
|
* @param array $arguments The array of the function arguments. |
|
115
|
|
|
* This can be either a list of arguments, or an associative array where keys are argument names. |
|
116
|
|
|
* |
|
117
|
|
|
* @throws ContainerExceptionInterface |
|
118
|
|
|
* @throws InvalidArgumentException|MissingRequiredArgumentException |
|
119
|
|
|
* @throws ReflectionException |
|
120
|
|
|
* |
|
121
|
|
|
* @return object The object of the given class. |
|
122
|
|
|
* |
|
123
|
|
|
* @psalm-suppress MixedMethodCall |
|
124
|
|
|
* |
|
125
|
|
|
* @psalm-template T |
|
126
|
|
|
* @psalm-param class-string<T> $class |
|
127
|
|
|
* @psalm-return T |
|
128
|
|
|
*/ |
|
129
|
18 |
|
public function make(string $class, array $arguments = []): object |
|
130
|
|
|
{ |
|
131
|
18 |
|
$classReflection = $this->getClassReflection($class); |
|
132
|
17 |
|
if (!$classReflection->isInstantiable()) { |
|
133
|
3 |
|
throw new \InvalidArgumentException("Class $class is not instantiable."); |
|
134
|
|
|
} |
|
135
|
14 |
|
$reflection = $classReflection->getConstructor(); |
|
136
|
14 |
|
if ($reflection === null) { |
|
137
|
|
|
// Method __construct() does not exist |
|
138
|
2 |
|
return new $class(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
12 |
|
return new $class(...$this->resolveDependencies($reflection, $arguments)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Resolve dependencies for the given function reflection object and a list of concrete arguments |
|
146
|
|
|
* and return array of arguments to call the function with. |
|
147
|
|
|
* |
|
148
|
|
|
* @param ReflectionFunctionAbstract $reflection function reflection. |
|
149
|
|
|
* @param array $arguments concrete arguments. |
|
150
|
|
|
* |
|
151
|
|
|
* @throws ContainerExceptionInterface |
|
152
|
|
|
* @throws InvalidArgumentException|MissingRequiredArgumentException |
|
153
|
|
|
* @throws ReflectionException |
|
154
|
|
|
* |
|
155
|
|
|
* @return array resolved arguments. |
|
156
|
|
|
*/ |
|
157
|
53 |
|
private function resolveDependencies(ReflectionFunctionAbstract $reflection, array $arguments = []): array |
|
158
|
|
|
{ |
|
159
|
53 |
|
$state = new ResolvingState($reflection, $arguments); |
|
160
|
|
|
|
|
161
|
50 |
|
$isInternalOptional = false; |
|
162
|
50 |
|
$internalParameter = ''; |
|
163
|
50 |
|
foreach ($reflection->getParameters() as $parameter) { |
|
164
|
42 |
|
if ($isInternalOptional) { |
|
165
|
|
|
// Check custom parameter definition for an internal function |
|
166
|
|
|
if ($state->hasNamedArgument($parameter->getName())) { |
|
167
|
|
|
throw new MissingInternalArgumentException($reflection, $internalParameter); |
|
168
|
|
|
} |
|
169
|
|
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
// Resolve parameter |
|
172
|
42 |
|
$resolved = $this->resolveParameter($parameter, $state); |
|
173
|
41 |
|
if ($resolved === true) { |
|
174
|
38 |
|
continue; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
5 |
|
if ($resolved === false) { |
|
178
|
5 |
|
throw new MissingRequiredArgumentException($reflection, $parameter->getName()); |
|
179
|
|
|
} |
|
180
|
|
|
// Internal function. Parameter not resolved |
|
181
|
|
|
$isInternalOptional = true; |
|
182
|
|
|
$internalParameter = $parameter->getName(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
43 |
|
return $state->getResolvedValues(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @throws NotFoundExceptionInterface |
|
190
|
|
|
* @throws ReflectionException |
|
191
|
|
|
* |
|
192
|
|
|
* @return bool|null True if argument resolved; False if not resolved; Null if parameter is optional but |
|
193
|
|
|
* without default value in a Reflection object. This is possible for internal functions. |
|
194
|
|
|
*/ |
|
195
|
42 |
|
private function resolveParameter(ReflectionParameter $parameter, ResolvingState $state): ?bool |
|
196
|
|
|
{ |
|
197
|
42 |
|
$name = $parameter->getName(); |
|
198
|
42 |
|
$isVariadic = $parameter->isVariadic(); |
|
199
|
42 |
|
$hasType = $parameter->hasType(); |
|
200
|
42 |
|
$state->disablePushTrailingArguments($isVariadic && $hasType); |
|
201
|
|
|
|
|
202
|
|
|
// Try to resolve parameter by name |
|
203
|
42 |
|
if ($state->resolveParameterByName($name, $isVariadic)) { |
|
204
|
14 |
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
36 |
|
$error = null; |
|
208
|
|
|
|
|
209
|
36 |
|
if ($hasType) { |
|
210
|
|
|
/** @var ReflectionType $reflectionType */ |
|
211
|
35 |
|
$reflectionType = $parameter->getType(); |
|
212
|
|
|
|
|
213
|
35 |
|
if ($this->resolveParameterType($state, $reflectionType, $isVariadic, $error)) { |
|
214
|
24 |
|
return true; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
18 |
|
if ($parameter->isDefaultValueAvailable()) { |
|
219
|
6 |
|
$argument = $parameter->getDefaultValue(); |
|
220
|
6 |
|
$state->addResolvedValue($argument); |
|
221
|
6 |
|
return true; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
12 |
|
if (!$parameter->isOptional()) { |
|
225
|
11 |
|
if ($hasType && $parameter->allowsNull()) { |
|
226
|
4 |
|
$argument = null; |
|
227
|
4 |
|
$state->addResolvedValue($argument); |
|
228
|
4 |
|
return true; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
7 |
|
if ($error === null) { |
|
232
|
5 |
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// Throw NotFoundExceptionInterface |
|
236
|
2 |
|
throw $error; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
if ($isVariadic) { |
|
240
|
1 |
|
return true; |
|
241
|
|
|
} |
|
242
|
|
|
return null; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Resolve parameter using its type. |
|
247
|
|
|
* |
|
248
|
|
|
* @param NotFoundExceptionInterface|null $error Last caught {@see NotFoundExceptionInterface} exception. |
|
249
|
|
|
* |
|
250
|
|
|
* @throws ContainerExceptionInterface |
|
251
|
|
|
* |
|
252
|
|
|
* @return bool True if argument was resolved |
|
253
|
|
|
* |
|
254
|
|
|
* @psalm-suppress PossiblyUndefinedMethod |
|
255
|
|
|
*/ |
|
256
|
35 |
|
private function resolveParameterType( |
|
257
|
|
|
ResolvingState $state, |
|
258
|
|
|
ReflectionType $type, |
|
259
|
|
|
bool $variadic, |
|
260
|
|
|
?NotFoundExceptionInterface &$error |
|
261
|
|
|
): bool { |
|
262
|
|
|
switch (true) { |
|
263
|
35 |
|
case $type instanceof ReflectionNamedType: |
|
264
|
34 |
|
$types = [$type]; |
|
265
|
|
|
// no break |
|
266
|
1 |
|
case $type instanceof ReflectionUnionType: |
|
267
|
35 |
|
$types ??= $type->getTypes(); |
|
|
|
|
|
|
268
|
|
|
/** @var array<int, ReflectionNamedType> $types */ |
|
269
|
35 |
|
foreach ($types as $namedType) { |
|
270
|
|
|
try { |
|
271
|
35 |
|
if ($this->resolveNamedType($state, $namedType, $variadic)) { |
|
272
|
33 |
|
return true; |
|
273
|
|
|
} |
|
274
|
7 |
|
} catch (NotFoundExceptionInterface $e) { |
|
275
|
7 |
|
$error = $e; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
15 |
|
break; |
|
279
|
|
|
case $type instanceof ReflectionIntersectionType: |
|
280
|
|
|
$classes = []; |
|
281
|
|
|
/** @var ReflectionNamedType $namedType */ |
|
282
|
|
|
foreach ($type->getTypes() as $namedType) { |
|
|
|
|
|
|
283
|
|
|
$classes[] = $namedType->getName(); |
|
284
|
|
|
} |
|
285
|
|
|
/** @var array<int, class-string> $classes */ |
|
286
|
|
|
if ($state->resolveParameterByClasses($classes, $variadic)) { |
|
287
|
|
|
return true; |
|
288
|
|
|
} |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
15 |
|
return false; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @throws ContainerExceptionInterface |
|
296
|
|
|
* @throws NotFoundExceptionInterface |
|
297
|
|
|
* |
|
298
|
|
|
* @return bool True if argument was resolved |
|
299
|
|
|
*/ |
|
300
|
35 |
|
private function resolveNamedType(ResolvingState $state, ReflectionNamedType $parameter, bool $isVariadic): bool |
|
301
|
|
|
{ |
|
302
|
35 |
|
$type = $parameter->getName(); |
|
303
|
|
|
/** @psalm-var class-string|null $class */ |
|
304
|
35 |
|
$class = $parameter->isBuiltin() ? null : $type; |
|
305
|
35 |
|
$isClass = $class !== null || $type === 'object'; |
|
306
|
35 |
|
return $isClass && $this->resolveObjectParameter($state, $class, $isVariadic); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @psalm-param class-string|null $class |
|
311
|
|
|
* |
|
312
|
|
|
* @throws ContainerExceptionInterface |
|
313
|
|
|
* @throws NotFoundExceptionInterface |
|
314
|
|
|
* |
|
315
|
|
|
* @return bool True if argument resolved |
|
316
|
|
|
*/ |
|
317
|
29 |
|
private function resolveObjectParameter(ResolvingState $state, ?string $class, bool $isVariadic): bool |
|
318
|
|
|
{ |
|
319
|
29 |
|
$found = $state->resolveParameterByClass($class, $isVariadic); |
|
320
|
29 |
|
if ($found || $isVariadic) { |
|
321
|
13 |
|
return $found; |
|
322
|
|
|
} |
|
323
|
22 |
|
if ($class !== null) { |
|
324
|
21 |
|
if ($this->container === null) { |
|
325
|
1 |
|
throw new ContainerNotSetException($class); |
|
326
|
|
|
} else { |
|
327
|
20 |
|
$argument = $this->container->get($class); |
|
328
|
|
|
} |
|
329
|
16 |
|
$state->addResolvedValue($argument); |
|
330
|
16 |
|
return true; |
|
331
|
|
|
} |
|
332
|
1 |
|
return false; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @psalm-param class-string $class |
|
337
|
|
|
* |
|
338
|
|
|
* @throws ReflectionException |
|
339
|
|
|
*/ |
|
340
|
18 |
|
private function getClassReflection(string $class): ReflectionClass |
|
341
|
|
|
{ |
|
342
|
18 |
|
if ($this->cacheReflections) { |
|
343
|
1 |
|
return $this->reflectionsCache[$class] ??= new ReflectionClass($class); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
17 |
|
return new ReflectionClass($class); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths