|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Container\Argument; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Container; |
|
6
|
|
|
use League\Container\Exception\{ContainerException, NotFoundException}; |
|
7
|
|
|
use League\Container\ReflectionContainer; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use ReflectionFunctionAbstract; |
|
10
|
|
|
use ReflectionParameter; |
|
11
|
|
|
|
|
12
|
|
|
trait ArgumentResolverTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
34 |
|
public function resolveArguments(array $arguments) : array |
|
18
|
|
|
{ |
|
19
|
68 |
|
return array_map(function ($argument) { |
|
20
|
75 |
|
$justStringValue = false; |
|
21
|
|
|
|
|
22
|
75 |
|
if ($argument instanceof RawArgumentInterface) { |
|
23
|
12 |
|
return $argument->getValue(); |
|
24
|
72 |
|
} elseif ($argument instanceof ClassNameInterface) { |
|
25
|
36 |
|
$id = $argument->getClassName(); |
|
26
|
36 |
|
} elseif (!is_string($argument)) { |
|
27
|
12 |
|
return $argument; |
|
28
|
|
|
} else { |
|
29
|
27 |
|
$justStringValue = true; |
|
30
|
27 |
|
$id = $argument; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
63 |
|
$container = null; |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
63 |
|
$container = $this->getLeagueContainer(); |
|
37
|
27 |
|
} catch (ContainerException $e) { |
|
38
|
27 |
|
if ($this instanceof ReflectionContainer) { |
|
39
|
18 |
|
$container = $this; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
63 |
|
if ($container !== null) { |
|
44
|
|
|
try { |
|
45
|
57 |
|
return $container->get($id); |
|
46
|
9 |
|
} catch (NotFoundException $exception) { |
|
47
|
9 |
|
if ($argument instanceof ClassNameWithOptionalValue) { |
|
48
|
|
|
return $argument->getOptionalValue(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
if ($justStringValue) { |
|
52
|
6 |
|
return $id; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
throw $exception; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
9 |
|
if ($argument instanceof ClassNameWithOptionalValue) { |
|
60
|
3 |
|
return $argument->getOptionalValue(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Just a string value. |
|
64
|
6 |
|
return $id; |
|
65
|
102 |
|
}, $arguments); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
14 |
|
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array |
|
72
|
|
|
{ |
|
73
|
28 |
|
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) { |
|
74
|
42 |
|
$name = $param->getName(); |
|
75
|
42 |
|
$type = $param->getType(); |
|
76
|
|
|
|
|
77
|
42 |
|
if (array_key_exists($name, $args)) { |
|
78
|
6 |
|
return new RawArgument($args[$name]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
39 |
|
if ($type) { |
|
82
|
33 |
|
if (PHP_VERSION_ID >= 70200) { |
|
83
|
11 |
|
$typeName = $type->getName(); |
|
84
|
|
|
} else { |
|
85
|
22 |
|
$typeName = (string) $type; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
33 |
|
$typeName = ltrim($typeName, '?'); |
|
89
|
|
|
|
|
90
|
33 |
|
if ($param->isDefaultValueAvailable()) { |
|
91
|
21 |
|
return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
18 |
|
return new ClassName($typeName); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
12 |
|
if ($param->isDefaultValueAvailable()) { |
|
98
|
6 |
|
return new RawArgument($param->getDefaultValue()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
throw new NotFoundException(sprintf( |
|
102
|
6 |
|
'Unable to resolve a value for parameter (%s) in the function/method (%s)', |
|
103
|
4 |
|
$name, |
|
104
|
6 |
|
$method->getName() |
|
105
|
|
|
)); |
|
106
|
42 |
|
}, $method->getParameters()); |
|
107
|
|
|
|
|
108
|
36 |
|
return $this->resolveArguments($arguments); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return ContainerInterface |
|
113
|
|
|
*/ |
|
114
|
|
|
abstract public function getContainer() : ContainerInterface; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return Container |
|
118
|
|
|
*/ |
|
119
|
|
|
abstract public function getLeagueContainer() : Container; |
|
120
|
|
|
} |
|
121
|
|
|
|