-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCompositeContainer.php
More file actions
76 lines (66 loc) · 1.89 KB
/
CompositeContainer.php
File metadata and controls
76 lines (66 loc) · 1.89 KB
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
<?php
namespace Acclimate\Container;
use Psr\Container\ContainerInterface;
use Acclimate\Container\Exception\NotFoundException;
/**
* A composite container that acts as a normal container, but delegates method calls to one or more internal containers
*/
class CompositeContainer implements ContainerInterface
{
/**
* @var array Containers that are contained within this composite container
*/
protected $containers = array();
/**
* @param array $containers Containers to add to this composite container
*/
public function __construct(array $containers = array())
{
foreach ($containers as $container) {
$this->addContainer($container);
}
}
/**
* Adds a container to an internal queue of containers
*
* @param ContainerInterface $container The container to add
*
* @return $this
*/
public function addContainer(ContainerInterface $container)
{
$this->containers[] = $container;
return $this;
}
/**
* Finds an entry of the container by delegating the get call to a FIFO queue of internal containers
*
* {@inheritDoc}
*/
public function get($id)
{
/** @var ContainerInterface $container */
foreach ($this->containers as $container) {
if ($container->has($id)) {
return $container->get($id);
}
}
throw NotFoundException::fromPrevious($id);
}
/**
* Returns true if the at least one of the internal containers can return an entry for the given identifier
* Returns false otherwise.
*
* {@inheritDoc}
*/
public function has($id)
{
/** @var ContainerInterface $container */
foreach ($this->containers as $container) {
if ($container->has($id)) {
return true;
}
}
return false;
}
}