-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCompositeContainer.php
90 lines (80 loc) · 2.55 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
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Dhii\Container\Exception\ContainerException;
use Dhii\Container\Exception\NotFoundException;
use Dhii\Container\Util\StringTranslatingTrait;
use Exception;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class CompositeContainer implements ContainerInterface
{
use StringTranslatingTrait;
/**
* @var iterable<PsrContainerInterface>
*/
protected $containers;
/**
* @param iterable<PsrContainerInterface> $containers The list of containers.
*/
public function __construct(iterable $containers)
{
$this->containers = $containers;
}
/**
* {@inheritDoc}
*/
public function get(string $key)
{
foreach ($this->containers as $index => $container) {
/**
* @psalm-suppress InvalidCatch
* The base interface does not extend Throwable, but in fact everything that is possible
* in theory to catch will be Throwable, and PSR-11 exceptions will implement this interface
*/
try {
if ($container->has($key)) {
return $container->get($key);
}
} catch (NotFoundExceptionInterface $e) {
throw new NotFoundException(
$this->__('Failed to retrieve value for key "%1$s" from container at index "%2$s"', [$key, $index]),
0,
$e
);
} catch (Exception $e) {
throw new ContainerException(
$this->__('Failed check for key "%1$s" on container at index "%2$s"', [$key, $index]),
0,
$e
);
}
}
throw new NotFoundException(
$this->__('Key "%1$s" not found in any of the containers', [$key]),
0,
null
);
}
/**
* {@inheritDoc}
*/
public function has(string $key): bool
{
foreach ($this->containers as $index => $container) {
try {
if ($container->has($key)) {
return true;
}
} catch (Exception $e) {
throw new ContainerException(
$this->__('Failed check for key "%1$s" on container at index "%2$s"', [$key, $index]),
0,
$e
);
}
}
return false;
}
}