-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathArrayContainer.php
More file actions
84 lines (73 loc) · 2.52 KB
/
ArrayContainer.php
File metadata and controls
84 lines (73 loc) · 2.52 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
77
78
79
80
81
82
83
84
<?php
namespace Acclimate\Container;
use Psr\Container\ContainerInterface;
use Acclimate\Container\Exception\ContainerException;
use Acclimate\Container\Exception\NotFoundException;
/**
* The Array Container is a simple container that follows both the `ContainerInterface` and `ArrayAccess` interface.
* The container can be seeded with an array or array-like object. The "get" functionality will evaluate closures and
* cache results.
*/
class ArrayContainer implements ContainerInterface, \ArrayAccess
{
/**
* @var array|\ArrayAccess The container data
*/
protected $data;
/**
* @var ContainerInterface The container that will be used for dependency lookups
*/
protected $delegateLookupContainer;
/**
* @param array|\ArrayAccess|\Traversable $data Data for the container
* @param ContainerInterface $delegateLookupContainer The container that will be used for dependency lookups.
*
* @throws \InvalidArgumentException if the provided data is not an array or array-like object
*/
public function __construct($data = array(), $delegateLookupContainer = null)
{
if (is_array($data) || $data instanceof \ArrayAccess) {
$this->data = $data;
} elseif ($data instanceof \Traversable) {
$this->data = iterator_to_array($data, true);
} else {
throw new \InvalidArgumentException('The ArrayContainer requires either an array or an array-like object');
}
$this->delegateLookupContainer = $delegateLookupContainer ?: $this;
}
public function get($id)
{
if (isset($this->data[$id])) {
try {
if ($this->data[$id] instanceof \Closure) {
$this->data[$id] = call_user_func($this->data[$id], $this->delegateLookupContainer);
}
} catch (\Exception $prev) {
throw ContainerException::fromPrevious($id, $prev);
}
return $this->data[$id];
} else {
throw NotFoundException::fromPrevious($id);
}
}
public function has($identifier)
{
return isset($this->data[$identifier]);
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->get($offset);
}
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}