|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thruster\Component\Container; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Thruster\Component\Container\Exception\IdentifierFrozenException; |
|
7
|
|
|
use Thruster\Component\Container\Exception\NotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Container |
|
11
|
|
|
* |
|
12
|
|
|
* @package Thruster\Component\Container |
|
13
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Container implements \ArrayAccess, ContainerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $values; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \SplObjectStorage |
|
24
|
|
|
*/ |
|
25
|
|
|
private $factories; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $frozen; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $raw; |
|
36
|
|
|
|
|
37
|
10 |
|
public function __construct(array $values = []) |
|
38
|
|
|
{ |
|
39
|
10 |
|
$this->values = []; |
|
40
|
10 |
|
$this->frozen = []; |
|
41
|
10 |
|
$this->raw = []; |
|
42
|
10 |
|
$this->factories = []; |
|
43
|
|
|
|
|
44
|
10 |
|
foreach ($values as $key => $value) { |
|
45
|
1 |
|
$this->set($key, $value); |
|
46
|
|
|
} |
|
47
|
10 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function has($id) : bool |
|
53
|
|
|
{ |
|
54
|
9 |
|
return isset($this->raw[$id]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public function get($id) |
|
61
|
|
|
{ |
|
62
|
9 |
|
if (false === $this->has($id)) { |
|
63
|
1 |
|
throw new NotFoundException($id); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
8 |
|
if (isset($this->values[$id])) { |
|
67
|
3 |
|
return $this->values[$id]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
if (isset($this->factories[$id])) { |
|
71
|
1 |
|
return $this->raw[$id]($this); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
7 |
|
$raw = $this->raw[$id]; |
|
75
|
7 |
|
$value = $this->values[$id] = $raw($this); |
|
76
|
|
|
|
|
77
|
7 |
|
$this->frozen[$id] = true; |
|
78
|
|
|
|
|
79
|
7 |
|
return $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
7 |
View Code Duplication |
public function set($id, callable $value) : self |
|
83
|
|
|
{ |
|
84
|
7 |
|
if (isset($this->frozen[$id])) { |
|
85
|
1 |
|
throw new IdentifierFrozenException($id); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
7 |
|
$this->raw[$id] = $value; |
|
89
|
|
|
|
|
90
|
7 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
View Code Duplication |
public function factory($id, callable $value) : self |
|
94
|
|
|
{ |
|
95
|
2 |
|
if (isset($this->frozen[$id])) { |
|
96
|
1 |
|
throw new IdentifierFrozenException($id); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
$this->factories[$id] = true; |
|
100
|
1 |
|
$this->raw[$id] = $value; |
|
101
|
|
|
|
|
102
|
1 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
public function remove($id) : self |
|
106
|
|
|
{ |
|
107
|
|
|
unset( |
|
108
|
3 |
|
$this->raw[$id], |
|
109
|
3 |
|
$this->values[$id], |
|
110
|
3 |
|
$this->frozen[$id], |
|
111
|
3 |
|
$this->factories[$id] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
3 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function addProvider(ContainerProviderInterface $provider) : self |
|
118
|
|
|
{ |
|
119
|
1 |
|
$provider->register($this); |
|
120
|
|
|
|
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritDoc |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function offsetExists($offset) : bool |
|
128
|
|
|
{ |
|
129
|
1 |
|
return $this->has($offset); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @inheritDoc |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function offsetGet($offset) |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->get($offset); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritDoc |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function offsetSet($offset, $value) |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->set($offset, $value); |
|
|
|
|
|
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritDoc |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function offsetUnset($offset) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$this->remove($offset); |
|
154
|
1 |
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: