-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
CompositeContainer.php
133 lines (117 loc) · 3.76 KB
/
CompositeContainer.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
<?php
declare(strict_types=1);
namespace Yiisoft\Di;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Throwable;
use Yiisoft\Di\Reference\TagReference;
use function is_string;
/**
* A composite container for use with containers that support the delegate lookup feature.
*/
final class CompositeContainer implements ContainerInterface
{
/**
* Containers to look into starting from the beginning of the array.
*
* @var ContainerInterface[] The list of containers.
*/
private array $containers = [];
/**
* @psalm-template T
* @psalm-param string|class-string<T> $id
* @psalm-return ($id is class-string ? T : mixed)
*/
public function get($id)
{
/** @psalm-suppress TypeDoesNotContainType */
if (!is_string($id)) {
throw new InvalidArgumentException(
sprintf(
'ID must be a string, %s given.',
get_debug_type($id)
)
);
}
if ($id === StateResetter::class) {
$resetters = [];
foreach ($this->containers as $container) {
if ($container->has(StateResetter::class)) {
$resetters[] = $container->get(StateResetter::class);
}
}
$stateResetter = new StateResetter($this);
$stateResetter->setResetters($resetters);
return $stateResetter;
}
if (TagReference::isTagAlias($id)) {
$tags = [];
foreach ($this->containers as $container) {
if (!$container instanceof Container) {
continue;
}
if ($container->has($id)) {
/** @psalm-suppress MixedArgument `Container::get()` always return array for tag */
array_unshift($tags, $container->get($id));
}
}
/** @psalm-suppress MixedArgument `Container::get()` always return array for tag */
return array_merge(...$tags);
}
foreach ($this->containers as $container) {
if ($container->has($id)) {
/** @psalm-suppress MixedReturnStatement */
return $container->get($id);
}
}
// Collect details from containers
$exceptions = [];
foreach ($this->containers as $container) {
$hasException = false;
try {
$container->get($id);
} catch (Throwable $t) {
$hasException = true;
$exceptions[] = [$t, $container];
} finally {
if (!$hasException) {
$exceptions[] = [
new RuntimeException(
'Container "has()" returned false, but no exception was thrown from "get()".'
),
$container,
];
}
}
}
throw new CompositeNotFoundException($exceptions);
}
public function has($id): bool
{
foreach ($this->containers as $container) {
if ($container->has($id)) {
return true;
}
}
return false;
}
/**
* Attaches a container to the composite container.
*/
public function attach(ContainerInterface $container): void
{
$this->containers[] = $container;
}
/**
* Removes a container from the list of containers.
*/
public function detach(ContainerInterface $container): void
{
foreach ($this->containers as $i => $c) {
if ($container === $c) {
unset($this->containers[$i]);
}
}
}
}