|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acclimate\Container; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Acclimate\Container\Exception\ContainerException; |
|
7
|
|
|
use Acclimate\Container\Exception\NotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The Array Container is a simple container that follows both the `ContainerInterface` and `ArrayAccess` interface. |
|
11
|
|
|
* The container can be seeded with an array or array-like object. The "get" functionality will evaluate closures and |
|
12
|
|
|
* cache results. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArrayContainer implements ContainerInterface, \ArrayAccess |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array|\ArrayAccess The container data |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $data; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ContainerInterface The container that will be used for dependency lookups |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $delegateLookupContainer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array|\ArrayAccess|\Traversable $data Data for the container |
|
28
|
|
|
* @param ContainerInterface $delegateLookupContainer The container that will be used for dependency lookups. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \InvalidArgumentException if the provided data is not an array or array-like object |
|
31
|
|
|
*/ |
|
32
|
7 |
|
public function __construct($data = array(), $delegateLookupContainer = null) |
|
33
|
|
|
{ |
|
34
|
7 |
|
if (is_array($data) || $data instanceof \ArrayAccess) { |
|
35
|
7 |
|
$this->data = $data; |
|
36
|
7 |
|
} elseif ($data instanceof \Traversable) { |
|
37
|
1 |
|
$this->data = iterator_to_array($data, true); |
|
38
|
1 |
|
} else { |
|
39
|
1 |
|
throw new \InvalidArgumentException('The ArrayContainer requires either an array or an array-like object'); |
|
40
|
|
|
} |
|
41
|
7 |
|
$this->delegateLookupContainer = $delegateLookupContainer ?: $this; |
|
42
|
7 |
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
public function get($id) |
|
45
|
|
|
{ |
|
46
|
6 |
|
if (isset($this->data[$id])) { |
|
47
|
|
|
try { |
|
48
|
5 |
|
if ($this->data[$id] instanceof \Closure) { |
|
49
|
3 |
|
$this->data[$id] = call_user_func($this->data[$id], $this->delegateLookupContainer); |
|
50
|
2 |
|
} |
|
51
|
5 |
|
} catch (\Exception $prev) { |
|
52
|
1 |
|
throw ContainerException::fromPrevious($id, $prev); |
|
53
|
|
|
} |
|
54
|
4 |
|
return $this->data[$id]; |
|
55
|
|
|
} else { |
|
56
|
1 |
|
throw NotFoundException::fromPrevious($id); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function has($identifier) |
|
61
|
|
|
{ |
|
62
|
2 |
|
return isset($this->data[$identifier]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function offsetExists($offset) |
|
66
|
|
|
{ |
|
67
|
1 |
|
return isset($this->data[$offset]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
public function offsetGet($offset) |
|
71
|
|
|
{ |
|
72
|
2 |
|
return $this->get($offset); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function offsetSet($offset, $value) |
|
76
|
1 |
|
{ |
|
77
|
3 |
|
$this->data[$offset] = $value; |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function offsetUnset($offset) |
|
81
|
|
|
{ |
|
82
|
1 |
|
unset($this->data[$offset]); |
|
83
|
1 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|