|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Injector\ParameterResolver; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
10
|
|
|
use ReflectionNamedType; |
|
11
|
|
|
use ReflectionParameter; |
|
12
|
|
|
use ReflectionType; |
|
13
|
|
|
use ReflectionUnionType; |
|
14
|
|
|
|
|
15
|
|
|
final class ContainerParameterResolver implements ParameterResolverInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private ContainerInterface $container; |
|
18
|
|
|
|
|
19
|
54 |
|
public function __construct(ContainerInterface $container) |
|
20
|
|
|
{ |
|
21
|
54 |
|
$this->container = $container; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @throws ParameterNotResolvedException |
|
26
|
|
|
* @throws NotFoundExceptionInterface |
|
27
|
|
|
* @throws ContainerExceptionInterface |
|
28
|
|
|
*/ |
|
29
|
26 |
|
public function resolve(ReflectionParameter $parameter) |
|
30
|
|
|
{ |
|
31
|
26 |
|
if ($parameter->isVariadic()) { |
|
32
|
1 |
|
throw new ParameterNotResolvedException(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
25 |
|
if ($parameter->hasType()) { |
|
36
|
24 |
|
$reflectionType = $parameter->getType(); |
|
37
|
24 |
|
if ($reflectionType === null) { |
|
38
|
|
|
throw new ParameterNotResolvedException(); |
|
39
|
|
|
} |
|
40
|
24 |
|
return $this->resolveType($reflectionType); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
throw new ParameterNotResolvedException(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws ParameterNotResolvedException |
|
48
|
|
|
* @throws ContainerExceptionInterface |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
24 |
|
private function resolveType(ReflectionType $type) |
|
53
|
|
|
{ |
|
54
|
24 |
|
if ($type instanceof ReflectionNamedType) { |
|
55
|
23 |
|
return $this->resolveNamedType($type); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if ($type instanceof ReflectionUnionType) { |
|
59
|
|
|
/** @var ReflectionNamedType $namedType */ |
|
60
|
1 |
|
foreach ($type->getTypes() as $namedType) { |
|
61
|
|
|
try { |
|
62
|
1 |
|
return $this->resolveNamedType($namedType); |
|
63
|
|
|
} catch (ParameterNotResolvedException $e) { |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
throw new ParameterNotResolvedException(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws ParameterNotResolvedException |
|
73
|
|
|
* @throws ContainerExceptionInterface |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
24 |
|
private function resolveNamedType(ReflectionNamedType $parameter) |
|
78
|
|
|
{ |
|
79
|
24 |
|
if ($parameter->isBuiltin()) { |
|
80
|
9 |
|
throw new ParameterNotResolvedException(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
18 |
|
return $this->container->get($parameter->getName()); |
|
85
|
5 |
|
} catch (NotFoundExceptionInterface $e) { |
|
86
|
5 |
|
throw new ParameterNotResolvedException(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|