Completed
Pull Request — master (#96)
by Andreas
26:04 queued 11:13
created

Container::add()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 9
Bugs 2 Features 1
Metric Value
c 9
b 2
f 1
dl 0
loc 25
ccs 10
cts 10
cp 1
rs 8.439
cc 5
eloc 13
nc 7
nop 3
crap 5
1
<?php
2
3
namespace League\Container;
4
5
use Interop\Container\ContainerInterface as InteropContainerInterface;
6
use League\Container\Definition\DefinitionFactory;
7
use League\Container\Definition\DefinitionFactoryInterface;
8
use League\Container\Definition\DefinitionInterface;
9
use League\Container\Exception\ProtectedException;
10
use League\Container\Exception\NotFoundException;
11
use League\Container\Inflector\InflectorAggregate;
12
use League\Container\Inflector\InflectorAggregateInterface;
13
use League\Container\ServiceProvider\ServiceProviderAggregate;
14
use League\Container\ServiceProvider\ServiceProviderAggregateInterface;
15
use League\Container\ServiceProvider\ServiceProviderInterface;
16
17
class Container implements ContainerInterface
18
{
19
    /**
20
     * @var \League\Container\Definition\DefinitionFactoryInterface
21
     */
22
    protected $definitionFactory;
23
24
    /**
25
     * @var \League\Container\Definition\DefinitionInterface[]
26
     */
27
    protected $definitions = [];
28
29
    /**
30
     * @var \League\Container\Definition\DefinitionInterface[]
31
     */
32
    protected $sharedDefinitions = [];
33
34
    /**
35
     * @var \League\Container\Inflector\InflectorAggregateInterface
36
     */
37
    protected $inflectors;
38
39
    /**
40
     * @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface
41
     */
42
    protected $providers;
43
44
    /**
45
     * @var array
46
     */
47
    protected $shared = [];
48
49
    /**
50
     * @var \Interop\Container\ContainerInterface[]
51
     */
52
    protected $delegates = [];
53
54
    /**
55
     * @var bool
56
     */
57
    protected $isProtected = false;
58
59 42
    /**
60
     * Constructor.
61
     *
62
     * @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers
63
     * @param \League\Container\Inflector\InflectorAggregateInterface|null             $inflectors
64
     * @param \League\Container\Definition\DefinitionFactoryInterface|null             $definitionFactory
65 42
     */
66 42
    public function __construct(
67 42
        ServiceProviderAggregateInterface $providers         = null,
68
        InflectorAggregateInterface       $inflectors        = null,
69 42
        DefinitionFactoryInterface        $definitionFactory = null
70 42
    ) {
71 42
        // set required dependencies
72
        $this->providers         = (is_null($providers))
73 42
                                 ? (new ServiceProviderAggregate)->setContainer($this)
74 42
                                 : $providers->setContainer($this);
75 42
76 42
        $this->inflectors        = (is_null($inflectors))
77
                                 ? (new InflectorAggregate)->setContainer($this)
78
                                 : $inflectors->setContainer($this);
79
80
        $this->definitionFactory = (is_null($definitionFactory))
81 30
                                 ? (new DefinitionFactory)->setContainer($this)
82
                                 : $definitionFactory->setContainer($this);
83 30
    }
84
85 30
    /**
86 12
     * {@inheritdoc}
87 12
     */
88 12
    public function get($alias, array $args = [])
89
    {
90 30
        $service = $this->getFromThisContainer($alias, $args);
91 24
92
        if ($service === false && $this->providers->provides($alias)) {
93
            $this->providers->register($alias);
94 12
            $service = $this->getFromThisContainer($alias, $args);
95 6
        }
96
97
        if ($service !== false) {
98 6
            return $service;
99 6
        }
100 6
101
        if ($resolved = $this->getFromDelegate($alias, $args)) {
102
            return $this->inflectors->inflect($resolved);
103
        }
104
105
        throw new NotFoundException(
106 18
            sprintf('Alias (%s) is not being managed by the container', $alias)
107
        );
108 18
    }
109 12
110
    /**
111
     * {@inheritdoc}
112 9
     */
113 3
    public function has($alias)
114
    {
115
        if (array_key_exists($alias, $this->definitions) || $this->hasShared($alias)) {
116 9
            return true;
117
        }
118
119
        if ($this->providers->provides($alias)) {
120
            return true;
121
        }
122
123
        return $this->hasInDelegate($alias);
124
    }
125
126 36
    /**
127
     * Returns a boolean to determine if the container has a shared instance of an alias.
128 36
     *
129
     * @param  string  $alias
130 36
     * @param  boolean $resolved
131
     * @return boolean
132
     */
133
    public function hasShared($alias, $resolved = false)
134
    {
135
        $shared = ($resolved === false) ? array_merge($this->shared, $this->sharedDefinitions) : $this->shared;
136 30
137
        return (array_key_exists($alias, $shared));
138 30
    }
139 3
140 3
    /**
141
     * {@inheritdoc}
142 30
     */
143
    public function add($alias, $concrete = null, $share = false)
144 30
    {
145 27
        if ($this->isProtected()) {
146 9
            throw new ProtectedException('Container has been protected, adding services is not allowed.');
147 9
        }
148 21
149
        if (is_null($concrete)) {
150
            $concrete = $alias;
151 27
        }
152
153
        $definition = $this->definitionFactory->getDefinition($alias, $concrete);
154
155 3
        if ($definition instanceof DefinitionInterface) {
156 3
            if ($share === false) {
157
                $this->definitions[$alias] = $definition;
158
            } else {
159
                $this->sharedDefinitions[$alias] = $definition;
160
            }
161 21
162
            return $definition;
163 21
        }
164
165
        // dealing with a value that cannot build a definition
166
        $this->shared[$alias] = $concrete;
167
    }
168
169 12
    /**
170
     * {@inheritdoc}
171 12
     */
172
    public function share($alias, $concrete = null)
173 12
    {
174
        return $this->add($alias, $concrete, true);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179 6
     */
180
    public function addServiceProvider($provider)
181 6
    {
182 3
        if ($this->isProtected()) {
183 3
            throw new ProtectedException('Container has been protected, adding service providers is not allowed');
184
        }
185 6
186 3
        $this->providers->add($provider);
187
188
        return $this;
189 6
    }
190 3
191
    /**
192
     * {@inheritdoc}
193 3
     */
194 3
    public function extend($alias)
195 3
    {
196
        if ($this->isProtected()) {
197
            throw new ProtectedException('Container has been protected, extending services is not allowed');
198
        }
199
        
200
        if ($this->providers->provides($alias)) {
201
            $this->providers->register($alias);
202
        }
203
204
        if (array_key_exists($alias, $this->definitions)) {
205
            return $this->definitions[$alias];
206
        }
207
208
        if (array_key_exists($alias, $this->sharedDefinitions)) {
209
            return $this->sharedDefinitions[$alias];
210
        }
211
212
        throw new NotFoundException(
213
            sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias)
214
        );
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function inflector($type, callable $callback = null)
221 9
    {
222
        if ($this->isProtected()) {
223 9
            throw new ProtectedException('Container has been protected, adding inflectors is not allowed');
224
        }
225 9
226 3
        return $this->inflectors->add($type, $callback);
227 3
    }
228
229 9
    /**
230
     * {@inheritdoc}
231
     */
232
    public function call(callable $callable, array $args = [])
233
    {
234
        return (new ReflectionContainer)->setContainer($this)->call($callable, $args);
235
    }
236
237
    /**
238 9
     * Marks the container as protected.
239
     *
240 9
     * Prevents overriding services and delegating to other containers once the container is marked as protected.
241 3
     */
242 3
    public function protect()
243
    {
244 9
        if ($this->providers instanceof ServiceProviderAggregate) {
245
            $reflection = new \ReflectionProperty(ServiceProviderAggregate::class, 'providers');
246 6
            $reflection->setAccessible(true);
247
248
            /* @var ServiceProviderInterface[] $providers */
249
            $providers = array_unique($reflection->getValue($this->providers));
250
251
            foreach ($providers as $provider) {
252
                $provider->register();
253
            }
254
        }
255
256 12
        $this->isProtected = true;
257
    }
258 12
259 6
    /**
260 6
     * Marks the container as unprotected.
261
     *
262
     * Allows overriding services and delegating to other containers (this is the default).
263 3
     */
264 6
    public function unprotect()
265
    {
266 6
        $this->isProtected = false;
267
    }
268
269
    /**
270
     * Returns true if the container is protected.
271
     *
272
     * @return bool
273
     */
274
    public function isProtected()
275
    {
276 30
        return $this->isProtected;
277
    }
278 30
279 9
    /**
280
     * Delegate a backup container to be checked for services if it
281
     * cannot be resolved via this container.
282 27
     *
283 15
     * @param  \Interop\Container\ContainerInterface $container
284 15
     * @return $this
285 15
     */
286
    public function delegate(InteropContainerInterface $container)
287
    {
288 24
        if ($this->isProtected()) {
289 6
            throw new ProtectedException('Container has been protected, delegating to additional containers is not allowed');
290 6
        }
291 6
292
        $this->delegates[] = $container;
293
294 21
        if ($container instanceof ImmutableContainerAwareInterface) {
295
            $container->setContainer($this);
296
        }
297
298
        return $this;
299
    }
300
301
    /**
302
     * Returns true if service is registered in one of the delegated backup containers.
303
     *
304
     * @param  string $alias
305
     * @return boolean
306
     */
307
    public function hasInDelegate($alias)
308
    {
309
        foreach ($this->delegates as $container) {
310
            if ($container->has($alias)) {
311
                return true;
312
            }
313
        }
314
315
        return false;
316
    }
317
318
    /**
319
     * Attempt to get a service from the stack of delegated backup containers.
320
     *
321
     * @param  string $alias
322
     * @param  array  $args
323
     * @return mixed
324
     */
325
    protected function getFromDelegate($alias, array $args = [])
326
    {
327
        foreach ($this->delegates as $container) {
328
            if ($container->has($alias)) {
329
                return $container->get($alias, $args);
330
            }
331
332
            continue;
333
        }
334
335
        return false;
336
    }
337
338
    /**
339
     * Get a service that has been registered in this container.
340
     *
341
     * @param  string $alias
342
     * @param  array $args
343
     * @return mixed
344
     */
345
    protected function getFromThisContainer($alias, array $args = [])
346
    {
347
        if ($this->hasShared($alias, true)) {
348
            return $this->inflectors->inflect($this->shared[$alias]);
349
        }
350
351
        if (array_key_exists($alias, $this->sharedDefinitions)) {
352
            $shared = $this->inflectors->inflect($this->sharedDefinitions[$alias]->build());
353
            $this->shared[$alias] = $shared;
354
            return $shared;
355
        }
356
357
        if (array_key_exists($alias, $this->definitions)) {
358
            return $this->inflectors->inflect(
359
                $this->definitions[$alias]->build($args)
360
            );
361
        }
362
363
        return false;
364
    }
365
}
366