/
Container.php
305 lines (256 loc) · 8.45 KB
/
Container.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace League\Container;
use Interop\Container\ContainerInterface as InteropContainerInterface;
use League\Container\Argument\RawArgumentInterface;
use League\Container\Definition\DefinitionFactory;
use League\Container\Definition\DefinitionFactoryInterface;
use League\Container\Definition\DefinitionInterface;
use League\Container\Exception\NotFoundException;
use League\Container\Inflector\InflectorAggregate;
use League\Container\Inflector\InflectorAggregateInterface;
use League\Container\ServiceProvider\ServiceProviderAggregate;
use League\Container\ServiceProvider\ServiceProviderAggregateInterface;
class Container implements ContainerInterface
{
/**
* @var \League\Container\Definition\DefinitionFactoryInterface
*/
protected $definitionFactory;
/**
* @var \League\Container\Definition\DefinitionInterface[]
*/
protected $definitions = [];
/**
* @var \League\Container\Definition\DefinitionInterface[]
*/
protected $sharedDefinitions = [];
/**
* @var \League\Container\Inflector\InflectorAggregateInterface
*/
protected $inflectors;
/**
* @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface
*/
protected $providers;
/**
* @var array
*/
protected $shared = [];
/**
* @var \Interop\Container\ContainerInterface[]
*/
protected $delegates = [];
/**
* Constructor.
*
* @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers
* @param \League\Container\Inflector\InflectorAggregateInterface|null $inflectors
* @param \League\Container\Definition\DefinitionFactoryInterface|null $definitionFactory
*/
public function __construct(
ServiceProviderAggregateInterface $providers = null,
InflectorAggregateInterface $inflectors = null,
DefinitionFactoryInterface $definitionFactory = null
) {
// set required dependencies
$this->providers = (is_null($providers))
? (new ServiceProviderAggregate)->setContainer($this)
: $providers->setContainer($this);
$this->inflectors = (is_null($inflectors))
? (new InflectorAggregate)->setContainer($this)
: $inflectors->setContainer($this);
$this->definitionFactory = (is_null($definitionFactory))
? (new DefinitionFactory)->setContainer($this)
: $definitionFactory->setContainer($this);
}
/**
* {@inheritdoc}
*/
public function get($alias, array $args = [])
{
try {
return $this->getFromThisContainer($alias, $args);
} catch (NotFoundException $exception) {
if ($this->providers->provides($alias)) {
$this->providers->register($alias);
return $this->getFromThisContainer($alias, $args);
}
$resolved = $this->getFromDelegate($alias, $args);
return $this->inflectors->inflect($resolved);
}
}
/**
* {@inheritdoc}
*/
public function has($alias)
{
if (array_key_exists($alias, $this->definitions) || $this->hasShared($alias)) {
return true;
}
if ($this->providers->provides($alias)) {
return true;
}
return $this->hasInDelegate($alias);
}
/**
* Returns a boolean to determine if the container has a shared instance of an alias.
*
* @param string $alias
* @param boolean $resolved
* @return boolean
*/
public function hasShared($alias, $resolved = false)
{
$shared = ($resolved === false) ? array_merge($this->shared, $this->sharedDefinitions) : $this->shared;
return (array_key_exists($alias, $shared));
}
/**
* {@inheritdoc}
*/
public function add($alias, $concrete = null, $share = false)
{
unset($this->shared[$alias]);
unset($this->definitions[$alias]);
unset($this->sharedDefinitions[$alias]);
if (is_null($concrete)) {
$concrete = $alias;
}
$definition = $this->definitionFactory->getDefinition($alias, $concrete);
if ($definition instanceof DefinitionInterface) {
if ($share === false) {
$this->definitions[$alias] = $definition;
} else {
$this->sharedDefinitions[$alias] = $definition;
}
return $definition;
}
// dealing with a value that cannot build a definition
$this->shared[$alias] = $concrete;
}
/**
* {@inheritdoc}
*/
public function share($alias, $concrete = null)
{
return $this->add($alias, $concrete, true);
}
/**
* {@inheritdoc}
*/
public function addServiceProvider($provider)
{
$this->providers->add($provider);
return $this;
}
/**
* {@inheritdoc}
*/
public function extend($alias)
{
if ($this->providers->provides($alias)) {
$this->providers->register($alias);
}
if (array_key_exists($alias, $this->definitions)) {
return $this->definitions[$alias];
}
if (array_key_exists($alias, $this->sharedDefinitions)) {
return $this->sharedDefinitions[$alias];
}
throw new NotFoundException(
sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias)
);
}
/**
* {@inheritdoc}
*/
public function inflector($type, callable $callback = null)
{
return $this->inflectors->add($type, $callback);
}
/**
* {@inheritdoc}
*/
public function call(callable $callable, array $args = [])
{
return (new ReflectionContainer)->setContainer($this)->call($callable, $args);
}
/**
* Delegate a backup container to be checked for services if it
* cannot be resolved via this container.
*
* @param \Interop\Container\ContainerInterface $container
* @return $this
*/
public function delegate(InteropContainerInterface $container)
{
$this->delegates[] = $container;
if ($container instanceof ImmutableContainerAwareInterface) {
$container->setContainer($this);
}
return $this;
}
/**
* Returns true if service is registered in one of the delegated backup containers.
*
* @param string $alias
* @return boolean
*/
public function hasInDelegate($alias)
{
foreach ($this->delegates as $container) {
if ($container->has($alias)) {
return true;
}
}
return false;
}
/**
* Attempt to get a service from the stack of delegated backup containers.
*
* @param string $alias
* @param array $args
* @return mixed
*/
protected function getFromDelegate($alias, array $args = [])
{
foreach ($this->delegates as $container) {
if ($container->has($alias)) {
return $container->get($alias, $args);
}
continue;
}
throw new NotFoundException(
sprintf('Alias (%s) is not being managed by the container', $alias)
);
}
/**
* Get a service that has been registered in this container.
*
* @param string $alias
* @param array $args
* @return mixed
*/
protected function getFromThisContainer($alias, array $args = [])
{
if ($this->hasShared($alias, true)) {
$shared = $this->inflectors->inflect($this->shared[$alias]);
if ($shared instanceof RawArgumentInterface) {
return $shared->getValue();
}
return $shared;
}
if (array_key_exists($alias, $this->sharedDefinitions)) {
$shared = $this->inflectors->inflect($this->sharedDefinitions[$alias]->build());
$this->shared[$alias] = $shared;
return $shared;
}
if (array_key_exists($alias, $this->definitions)) {
return $this->inflectors->inflect(
$this->definitions[$alias]->build($args)
);
}
throw new NotFoundException(
sprintf('Alias (%s) is not being managed by the container', $alias)
);
}
}