Completed
Push — master ( e9cff8...bf916e )
by Phil
01:58
created

Container::getFromThisContainer()   A

Complexity

Conditions 4
Paths 4

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 20
Code Lines 11

Code Coverage

Tests 12
CRAP Score 4

Importance

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