1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acclimate\Container; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Acclimate\Container\Exception\NotFoundException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A composite container that acts as a normal container, but delegates method calls to one or more internal containers |
10
|
|
|
*/ |
11
|
|
|
class CompositeContainer implements ContainerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array Containers that are contained within this composite container |
15
|
|
|
*/ |
16
|
|
|
protected $containers = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $containers Containers to add to this composite container |
20
|
|
|
*/ |
21
|
3 |
|
public function __construct(array $containers = array()) |
22
|
|
|
{ |
23
|
3 |
|
foreach ($containers as $container) { |
24
|
1 |
|
$this->addContainer($container); |
25
|
3 |
|
} |
26
|
3 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds a container to an internal queue of containers |
30
|
|
|
* |
31
|
|
|
* @param ContainerInterface $container The container to add |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
2 |
|
public function addContainer(ContainerInterface $container) |
36
|
|
|
{ |
37
|
2 |
|
$this->containers[] = $container; |
38
|
|
|
|
39
|
2 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Finds an entry of the container by delegating the get call to a FIFO queue of internal containers |
44
|
|
|
* |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
3 |
|
public function get($id) |
48
|
|
|
{ |
49
|
|
|
/** @var ContainerInterface $container */ |
50
|
3 |
|
foreach ($this->containers as $container) { |
51
|
2 |
|
if ($container->has($id)) { |
52
|
1 |
|
return $container->get($id); |
53
|
|
|
} |
54
|
2 |
|
} |
55
|
|
|
|
56
|
2 |
|
throw NotFoundException::fromPrevious($id); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns true if the at least one of the internal containers can return an entry for the given identifier |
61
|
|
|
* Returns false otherwise. |
62
|
|
|
* |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
3 |
|
public function has($id) |
66
|
|
|
{ |
67
|
|
|
/** @var ContainerInterface $container */ |
68
|
3 |
|
foreach ($this->containers as $container) { |
69
|
2 |
|
if ($container->has($id)) { |
70
|
1 |
|
return true; |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|