Completed
Push — master ( f3abab...b4a4a2 )
by Ilias
02:14
created

Container::checkNamespaceProvider()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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