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
|
22 |
|
public function get($id) |
24
|
|
|
{ |
25
|
22 |
|
if ($id === StateResetter::class) { |
26
|
2 |
|
$resetters = []; |
27
|
2 |
|
foreach ($this->containers as $container) { |
28
|
2 |
|
if ($container->has(StateResetter::class)) { |
29
|
2 |
|
$resetters[] = $container->get(StateResetter::class); |
30
|
|
|
} |
31
|
|
|
} |
32
|
2 |
|
return new StateResetter($resetters, $this); |
33
|
|
|
} |
34
|
|
|
|
35
|
22 |
|
if ($this->isTagAlias($id)) { |
36
|
2 |
|
$tags = []; |
37
|
2 |
|
foreach ($this->containers as $container) { |
38
|
2 |
|
if (!$container instanceof Container) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
2 |
|
if ($container->has($id)) { |
42
|
2 |
|
$tags = array_merge($container->get($id), $tags); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return $tags; |
47
|
|
|
} |
48
|
|
|
|
49
|
20 |
|
foreach ($this->containers as $container) { |
50
|
20 |
|
if ($container->has($id)) { |
51
|
|
|
try { |
52
|
17 |
|
return $container->get($id); |
53
|
1 |
|
} catch (\Throwable $e) { |
54
|
1 |
|
$firstError = $firstError ?? $e; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
6 |
|
throw $firstError ?? new NotFoundException($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function has($id): bool |
62
|
|
|
{ |
63
|
4 |
|
foreach ($this->containers as $container) { |
64
|
4 |
|
if ($container->has($id)) { |
65
|
4 |
|
return true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Attaches a container to the composite container. |
73
|
|
|
* |
74
|
|
|
* @param ContainerInterface $container |
75
|
|
|
*/ |
76
|
24 |
|
public function attach(ContainerInterface $container): void |
77
|
|
|
{ |
78
|
24 |
|
array_unshift($this->containers, $container); |
79
|
24 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Removes a container from the list of containers. |
83
|
|
|
* |
84
|
|
|
* @param ContainerInterface $container |
85
|
|
|
*/ |
86
|
2 |
|
public function detach(ContainerInterface $container): void |
87
|
|
|
{ |
88
|
2 |
|
foreach ($this->containers as $i => $c) { |
89
|
2 |
|
if ($container === $c) { |
90
|
2 |
|
unset($this->containers[$i]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
2 |
|
} |
94
|
|
|
|
95
|
22 |
|
private function isTagAlias(string $id): bool |
96
|
|
|
{ |
97
|
22 |
|
return strpos($id, 'tag@') === 0; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|