generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DefinitionStorage.php
223 lines (195 loc) · 6.96 KB
/
DefinitionStorage.php
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
namespace Yiisoft\Definitions;
use Psr\Container\ContainerInterface;
use ReflectionNamedType;
use ReflectionUnionType;
use RuntimeException;
use Throwable;
use Yiisoft\Definitions\Exception\CircularReferenceException;
use Yiisoft\Definitions\Helpers\DefinitionExtractor;
/**
* Stores service definitions and checks if a definition could be instantiated.
*/
final class DefinitionStorage
{
private array $buildStack = [];
private ?ContainerInterface $delegateContainer = null;
/**
* @param array $definitions Definitions to store.
* @param bool $useStrictMode If every dependency should be defined explicitly including classes.
*/
public function __construct(
private array $definitions = [],
private bool $useStrictMode = false
) {
}
/**
* @param ContainerInterface $delegateContainer Container to fall back to when dependency is not found.
*/
public function setDelegateContainer(ContainerInterface $delegateContainer): void
{
$this->delegateContainer = $delegateContainer;
}
/**
* Checks if there is a definition with ID specified and that it can be created.
*
* @param string $id class name, interface name or alias name
*
* @throws CircularReferenceException
*/
public function has(string $id): bool
{
$this->buildStack = [];
return $this->isResolvable($id, []);
}
/**
* Returns a stack with definition IDs as keys and 1 as values in the order the latest dependency obtained would
* be built.
*
* @return array Build stack.
*/
public function getBuildStack(): array
{
return $this->buildStack;
}
/**
* Get a definition with a given ID.
*
* @return mixed|object Definition with a given ID.
*/
public function get(string $id): mixed
{
if (!$this->has($id)) {
throw new RuntimeException("Service $id doesn't exist in DefinitionStorage.");
}
return $this->definitions[$id];
}
/**
* Set a definition.
*
* @param string $id ID to set definition for.
* @param mixed $definition Definition to set.
*/
public function set(string $id, mixed $definition): void
{
$this->definitions[$id] = $definition;
}
/**
* @throws CircularReferenceException
*/
private function isResolvable(string $id, array $building): bool
{
if (isset($this->definitions[$id])) {
return true;
}
if ($this->useStrictMode || !class_exists($id)) {
$this->buildStack += $building + [$id => 1];
return false;
}
if (isset($building[$id])) {
throw new CircularReferenceException(sprintf(
'Circular reference to "%s" detected while building: %s.',
$id,
implode(', ', array_keys($building))
));
}
try {
$dependencies = DefinitionExtractor::fromClassName($id);
} catch (Throwable) {
$this->buildStack += $building + [$id => 1];
return false;
}
if ($dependencies === []) {
$this->definitions[$id] = $id;
return true;
}
$isResolvable = true;
$building[$id] = 1;
try {
foreach ($dependencies as $dependency) {
$parameter = $dependency->getReflection();
$type = $parameter->getType();
if ($parameter->isVariadic() || $parameter->isOptional()) {
break;
}
if (
($type instanceof ReflectionNamedType && $type->isBuiltin())
|| (!$type instanceof ReflectionNamedType && !$type instanceof ReflectionUnionType)
) {
$isResolvable = false;
break;
}
/** @var ReflectionNamedType|ReflectionUnionType $type */
// Union type is used as type hint
if ($type instanceof ReflectionUnionType) {
$isUnionTypeResolvable = false;
$unionTypes = [];
foreach ($type->getTypes() as $unionType) {
if (!$unionType->isBuiltin()) {
$typeName = $unionType->getName();
/**
* @psalm-suppress TypeDoesNotContainType
*
* @link https://github.com/vimeo/psalm/issues/6756
*/
if ($typeName === 'self') {
continue;
}
$unionTypes[] = $typeName;
if ($this->isResolvable($typeName, $building)) {
$isUnionTypeResolvable = true;
break;
}
}
}
if (!$isUnionTypeResolvable) {
foreach ($unionTypes as $typeName) {
if ($this->delegateContainer !== null && $this->delegateContainer->has($typeName)) {
$isUnionTypeResolvable = true;
break;
}
}
$isResolvable = $isUnionTypeResolvable;
if (!$isResolvable) {
break;
}
}
continue;
}
// Our parameter has a class type hint
if (!$type->isBuiltin()) {
$typeName = $type->getName();
/**
* @psalm-suppress TypeDoesNotContainType
*
* @link https://github.com/vimeo/psalm/issues/6756
*/
if ($typeName === 'self') {
throw new CircularReferenceException(
sprintf(
'Circular reference to "%s" detected while building: %s.',
$id,
implode(', ', array_keys($building))
)
);
}
if (
!$this->isResolvable($typeName, $building)
&& ($this->delegateContainer === null || !$this->delegateContainer->has($typeName))
) {
$isResolvable = false;
break;
}
}
}
} finally {
$this->buildStack += $building;
unset($building[$id]);
}
if ($isResolvable) {
$this->definitions[$id] = $id;
}
return $isResolvable;
}
}