1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Factory\Exception\NotFoundException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class implements a composite container for use with containers that support the delegate lookup feature. |
12
|
|
|
* The goal of the implementation is simplicity. |
13
|
|
|
*/ |
14
|
|
|
final class CompositeContainer implements ContainerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Containers to look into starting from the beginning of the array. |
18
|
|
|
* |
19
|
|
|
* @var ContainerInterface[] The list of containers |
20
|
|
|
*/ |
21
|
|
|
private array $containers = []; |
22
|
|
|
|
23
|
25 |
|
public function get($id) |
24
|
|
|
{ |
25
|
|
|
/** @psalm-suppress TypeDoesNotContainType */ |
26
|
25 |
|
if (!is_string($id)) { |
27
|
|
|
throw new \InvalidArgumentException("Id must be a string, {$this->getVariableType($id)} given."); |
28
|
|
|
} |
29
|
|
|
|
30
|
25 |
|
if ($id === StateResetter::class) { |
31
|
2 |
|
$resetters = []; |
32
|
2 |
|
foreach ($this->containers as $container) { |
33
|
2 |
|
if ($container->has(StateResetter::class)) { |
34
|
2 |
|
$resetters[] = $container->get(StateResetter::class); |
35
|
|
|
} |
36
|
|
|
} |
37
|
2 |
|
$stateResetter = new StateResetter($this); |
38
|
2 |
|
$stateResetter->setResetters($resetters); |
39
|
|
|
|
40
|
2 |
|
return $stateResetter; |
41
|
|
|
} |
42
|
|
|
|
43
|
25 |
|
if ($this->isTagAlias($id)) { |
44
|
2 |
|
$tags = []; |
45
|
2 |
|
foreach ($this->containers as $container) { |
46
|
2 |
|
if (!$container instanceof Container) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
2 |
|
if ($container->has($id)) { |
50
|
2 |
|
$tags = array_merge($container->get($id), $tags); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return $tags; |
55
|
|
|
} |
56
|
|
|
|
57
|
23 |
|
foreach ($this->containers as $container) { |
58
|
23 |
|
if ($container->has($id)) { |
59
|
20 |
|
return $container->get($id); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
throw new NotFoundException($id); |
64
|
|
|
} |
65
|
|
|
|
66
|
14 |
|
public function has($id): bool |
67
|
|
|
{ |
68
|
14 |
|
foreach ($this->containers as $container) { |
69
|
5 |
|
if ($container->has($id)) { |
70
|
5 |
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
9 |
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Attaches a container to the composite container. |
78
|
|
|
* |
79
|
|
|
* @param ContainerInterface $container |
80
|
|
|
*/ |
81
|
27 |
|
public function attach(ContainerInterface $container): void |
82
|
|
|
{ |
83
|
27 |
|
$this->containers[] = $container; |
84
|
27 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Removes a container from the list of containers. |
88
|
|
|
* |
89
|
|
|
* @param ContainerInterface $container |
90
|
|
|
*/ |
91
|
2 |
|
public function detach(ContainerInterface $container): void |
92
|
|
|
{ |
93
|
2 |
|
foreach ($this->containers as $i => $c) { |
94
|
2 |
|
if ($container === $c) { |
95
|
2 |
|
unset($this->containers[$i]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
2 |
|
} |
99
|
|
|
|
100
|
25 |
|
private function isTagAlias(string $id): bool |
101
|
|
|
{ |
102
|
25 |
|
return strpos($id, 'tag@') === 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $variable |
107
|
|
|
*/ |
108
|
|
|
private function getVariableType($variable): string |
109
|
|
|
{ |
110
|
|
|
return is_object($variable) ? get_class($variable) : gettype($variable); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|