Completed
Push — master ( f51f05...f3abab )
by Ilias
04:15
created

Container::ensureNamespacesLoaded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
4
namespace Dimple;
5
6
7
use Pimple\ServiceProviderInterface;
8
9
class Container extends \Pimple\Container
10
{
11
    /** @var \Pimple\Container */
12
    protected $pimpleContainer;
13
14
    /** @var string[] */
15
    protected $loadedNamespaces = array();
16
17
    /** @var ServiceProviderInterface[] */
18
    protected $namespaceProviders = array();
19
20 11
    public function __construct(array $values = array())
21
    {
22 11
        $this->pimpleContainer = new \Pimple\Container($values);
23 11
    }
24
25 6
    public function offsetSet($id, $value)
26
    {
27 6
        $this->pimpleContainer->offsetSet($id, $value);
28 6
    }
29
30 5
    public function offsetGet($id)
31
    {
32 5
        return $this->pimpleContainer->offsetGet(
33 5
            $this->ensureNamespacesLoaded($id)
34 4
        );
35
    }
36
37 1
    public function offsetExists($id)
38
    {
39 1
        return $this->pimpleContainer->offsetExists(
40 1
            $this->ensureNamespacesLoaded($id)
41 1
        );
42
    }
43
44
    public function offsetUnset($id)
45
    {
46
        $this->pimpleContainer->offsetUnset($id);
47
    }
48
49
    public function factory($callable)
50
    {
51
        return $this->pimpleContainer->factory($callable);
52
    }
53
54
    public function protect($callable)
55
    {
56
        return $this->pimpleContainer->protect($callable);
57
    }
58
59 1
    public function raw($id)
60
    {
61 1
        return $this->pimpleContainer->raw(
62 1
            $this->ensureNamespacesLoaded($id)
63 1
        );
64
    }
65
66 1
    public function extend($id, $callable)
67
    {
68 1
        $this->ensureNamespacesLoaded($id);
69
70 1
        return $this->pimpleContainer->extend(
71 1
            $this->ensureNamespacesLoaded($id), $callable
72 1
        );
73
    }
74
75
    public function keys()
76
    {
77
        return $this->pimpleContainer->keys();
78
    }
79
80
    public function register(ServiceProviderInterface $provider, array $values = array())
81
    {
82
        return $this->pimpleContainer->register($provider, $values);
83
    }
84
85 9
    public function registerServiceProviderProvider(ServiceProviderProviderInterface $providerProvider)
86
    {
87 9
        foreach ($providerProvider->provideServiceProviders($this) as $nameSpace => $serviceProvider) {
88 9
            if (isset($this->namespaceProviders[$nameSpace])) {
89 1
                throw new \RuntimeException('Can\'t redefine serviceProvider for namespace ' . $nameSpace);
90
            }
91
92 9
            if (!is_string($nameSpace)) {
93 1
                throw new \RuntimeException('Namespace must be a string');
94
            }
95
96 8
            if (! $serviceProvider instanceof ServiceProviderInterface) {
97 1
                throw new \RuntimeException('ServiceProviderProvider must provide map of ServiceProviders');
98
            }
99
100 7
            $this->namespaceProviders[$nameSpace] = $serviceProvider;
101 7
        }
102 7
    }
103
104 6
    protected function loadNamespace($name)
105
    {
106 6
        if (in_array($name, $this->loadedNamespaces)) {
107 2
            return;
108 3
        }
109
110 6
        if (! isset($this->namespaceProviders[$name])) {
111 1
            throw new \InvalidArgumentException('No providerProvider registered for namespace ' . $name);
112
        }
113
114 5
        $this->namespaceProviders[$name]->register($this);
115 5
        $this->loadedNamespaces[] = $name;
116 5
    }
117
118 7
    protected function ensureNamespacesLoaded($id)
119
    {
120 7
        if (($split = strpos($id, '::')) !== false) {
121 6
            $this->loadNameSpace(substr($id, 0, $split));
122 5
        }
123
124 6
        return $id;
125
    }
126
}
127