Passed
Branch dev (45cd88)
by Raffael
02:16
created

AbstractContainer::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Micro\Container
7
 *
8
 * @copyright   Copryright (c) 2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     MIT https://opensource.org/licenses/MIT
10
 */
11
12
namespace Micro\Container;
13
14
use Psr\Container\ContainerInterface;
15
16
abstract class AbstractContainer implements ContainerInterface
17
{
18
    /**
19
     * Config.
20
     *
21
     * @var Config
22
     */
23
    protected $config;
24
25
    /**
26
     * Service registry.
27
     *
28
     * @var array
29
     */
30
    protected $service = [];
31
32
    /**
33
     * Registered but not initialized service registry.
34
     *
35
     * @var array
36
     */
37
    protected $registry = [];
38
39
    /**
40
     * Parent container.
41
     *
42
     * @var ContainerInterface
43
     */
44
    protected $parent;
45
46
    /**
47
     * Children container.
48
     *
49
     * @var ContainerInterface[]
50
     */
51
    protected $children = [];
52
53
    /**
54
     * Parent service.
55
     *
56
     * @var mixed
57
     */
58
    protected $parent_service;
59
60
    /**
61
     * Create container.
62
     *
63
     * @param iterable           $config
64
     * @param ContainerInterface $parent
65
     */
66 42
    public function __construct(Iterable $config = [], ?ContainerInterface $parent = null)
67
    {
68 42
        $this->config = new Config($config, $this);
69 42
        $this->parent = $parent;
70 42
        $this->service[ContainerInterface::class] = $this;
71 42
    }
72
73
    /**
74
     * Get parent container.
75
     *
76
     * @return ContainerInterface
77
     */
78 35
    public function getParent(): ?ContainerInterface
79
    {
80 35
        return $this->parent;
81
    }
82
83
    /**
84
     * Add service.
85
     *
86
     * @param string $name
87
     * @param mixed  $service
88
     *
89
     * @return Container
90
     */
91 3
    public function add(string $name, $service): self
92
    {
93 3
        if ($this->has($name)) {
94 1
            throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered');
95
        }
96
97 3
        $this->registry[$name] = $service;
98
99 3
        return $this;
100
    }
101
102
    /**
103
     * Check if service is registered.
104
     *
105
     * @param mixed $name
106
     *
107
     * @return bool
108
     */
109 40
    public function has($name): bool
110
    {
111 40
        return isset($this->service[$name]);
112
    }
113
114
    /**
115
     * Set parent service on container
116
     * (Used internally, there is no point to call this method directly).
117
     *
118
     * @param mixed $service
119
     *
120
     * @return ContainerInterface
121
     */
122 2
    public function setParentService($service): ContainerInterface
123
    {
124 2
        $this->parent_service = $service;
125
126 2
        return $this;
127
    }
128
129
    /**
130
     * Get config.
131
     *
132
     * @return Config
133
     */
134 2
    public function getConfig(): Config
135
    {
136 2
        return $this->config;
137
    }
138
}
139