|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Injector; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use ReflectionFunctionAbstract; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Intermediate arguments resolving data to pass around until resolving is finished. |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ResolvingState |
|
15
|
|
|
{ |
|
16
|
|
|
private ReflectionFunctionAbstract $reflection; |
|
17
|
|
|
/** @var array<int, object> */ |
|
18
|
|
|
private array $numericArguments = []; |
|
19
|
|
|
/** @var array<string, mixed> */ |
|
20
|
|
|
private array $namedArguments = []; |
|
21
|
|
|
private bool $shouldPushTrailingArguments; |
|
22
|
|
|
private array $resolvedValues = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ReflectionFunctionAbstract $reflection Function reflection. |
|
26
|
|
|
* @param array $arguments User arguments. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(ReflectionFunctionAbstract $reflection, array $arguments) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->reflection = $reflection; |
|
31
|
|
|
$this->shouldPushTrailingArguments = !$reflection->isInternal(); |
|
32
|
|
|
$this->sortArguments($arguments); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function hasNamedArgument(string $name): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return array_key_exists($name, $this->namedArguments); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param bool $condition If true then trailing arguments will not be passed. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function disablePushTrailingArguments(bool $condition): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->shouldPushTrailingArguments = $this->shouldPushTrailingArguments && !$condition; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function addResolvedValue(&$value): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->resolvedValues[] = &$value; |
|
51
|
|
|
} |
|
52
|
|
|
public function resolveParameterByName(string $name, bool $variadic): bool |
|
53
|
|
|
{ |
|
54
|
|
|
if (!array_key_exists($name, $this->namedArguments)) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
if ($variadic && is_array($this->namedArguments[$name])) { |
|
58
|
|
|
array_walk($this->namedArguments[$name], [$this, 'addResolvedValue']); |
|
59
|
|
|
} else { |
|
60
|
|
|
$this->addResolvedValue($this->namedArguments[$name]); |
|
61
|
|
|
} |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
public function resolveParameterByClass(?string $className, bool $variadic): bool |
|
65
|
|
|
{ |
|
66
|
|
|
$generator = $this->pullNumericArgument($className); |
|
67
|
|
|
if (!$variadic) { |
|
68
|
|
|
if (!$generator->valid()) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
$value = $generator->current(); |
|
72
|
|
|
$this->addResolvedValue($value); |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
foreach ($generator as &$value) { |
|
76
|
|
|
$this->addResolvedValue($value); |
|
77
|
|
|
} |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getResolvedValues(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->shouldPushTrailingArguments |
|
84
|
|
|
? [...$this->resolvedValues, ...$this->numericArguments] |
|
85
|
|
|
: $this->resolvedValues; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param null|string $className |
|
90
|
|
|
* @return Generator<void, object> |
|
91
|
|
|
*/ |
|
92
|
|
|
private function &pullNumericArgument(?string $className): Generator |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->numericArguments as $key => &$value) { |
|
95
|
|
|
if ($className === null || $value instanceof $className) { |
|
96
|
|
|
unset($this->numericArguments[$key]); |
|
97
|
|
|
yield $value; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $arguments |
|
103
|
|
|
* @throws InvalidArgumentException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function sortArguments(array $arguments): void |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($arguments as $key => &$value) { |
|
108
|
|
|
if (is_int($key)) { |
|
109
|
|
|
if (!is_object($value)) { |
|
110
|
|
|
throw new InvalidArgumentException($this->reflection, (string)$key); |
|
111
|
|
|
} |
|
112
|
|
|
$this->numericArguments[] = &$value; |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->namedArguments[$key] = &$value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|