|
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 Closure; |
|
15
|
|
|
use Psr\Container\ContainerInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractContainer implements ContainerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Config. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Config |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Service registry. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $service = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Registered but not initialized service registry. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $registry = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Parent container. |
|
42
|
|
|
* |
|
43
|
|
|
* @var ContainerInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $parent; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Children container. |
|
49
|
|
|
* |
|
50
|
|
|
* @var ContainerInterface[] |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $children = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Parent service. |
|
56
|
|
|
* |
|
57
|
|
|
* @var mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $parent_service; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create container. |
|
63
|
|
|
* |
|
64
|
|
|
* @param iterable $config |
|
65
|
|
|
* @param ContainerInterface $parent |
|
66
|
|
|
*/ |
|
67
|
42 |
|
public function __construct(Iterable $config = [], ?ContainerInterface $parent = null) |
|
68
|
|
|
{ |
|
69
|
42 |
|
$this->config = new Config($config, $this); |
|
70
|
42 |
|
$this->parent = $parent; |
|
71
|
42 |
|
$this->service[ContainerInterface::class] = $this; |
|
72
|
42 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get parent container. |
|
76
|
|
|
* |
|
77
|
|
|
* @return ContainerInterface |
|
78
|
|
|
*/ |
|
79
|
35 |
|
public function getParent(): ?ContainerInterface |
|
80
|
|
|
{ |
|
81
|
35 |
|
return $this->parent; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add service. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* @param mixed $service |
|
89
|
|
|
* |
|
90
|
|
|
* @return Container |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function add(string $name, $service): self |
|
93
|
|
|
{ |
|
94
|
3 |
|
if ($this->has($name)) { |
|
95
|
1 |
|
throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
$this->registry[$name] = $service; |
|
99
|
|
|
|
|
100
|
3 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if service is registered. |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $name |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
40 |
|
public function has($name): bool |
|
111
|
|
|
{ |
|
112
|
40 |
|
return isset($this->service[$name]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set parent service on container |
|
117
|
|
|
* (Used internally, there is no point to call this method directly). |
|
118
|
|
|
* |
|
119
|
|
|
* @param mixed $service |
|
120
|
|
|
* |
|
121
|
|
|
* @return ContainerInterface |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function setParentService($service): ContainerInterface |
|
124
|
|
|
{ |
|
125
|
2 |
|
$this->parent_service = $service; |
|
126
|
|
|
|
|
127
|
2 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get config. |
|
132
|
|
|
* |
|
133
|
|
|
* @return Config |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function getConfig(): Config |
|
136
|
|
|
{ |
|
137
|
2 |
|
return $this->config; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Check for static injections. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* |
|
145
|
|
|
* @return mixed |
|
146
|
|
|
*/ |
|
147
|
3 |
|
protected function addStaticService(string $name) |
|
148
|
|
|
{ |
|
149
|
3 |
|
if ($this->registry[$name] instanceof Closure) { |
|
150
|
1 |
|
$this->service[$name] = $this->registry[$name]->call($this); |
|
151
|
|
|
} else { |
|
152
|
2 |
|
$this->service[$name] = $this->registry[$name]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
3 |
|
unset($this->registry[$name]); |
|
156
|
|
|
|
|
157
|
3 |
|
return $this->service[$name]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Store service. |
|
162
|
|
|
* |
|
163
|
|
|
* @param param string $name |
|
164
|
|
|
* @param array $config |
|
165
|
|
|
* @param mixed $service |
|
166
|
|
|
* |
|
167
|
|
|
* @return mixed |
|
168
|
|
|
*/ |
|
169
|
31 |
|
protected function storeService(string $name, array $config, $service) |
|
170
|
|
|
{ |
|
171
|
31 |
|
if (true === $config['singleton']) { |
|
172
|
2 |
|
return $service; |
|
173
|
|
|
} |
|
174
|
30 |
|
$this->service[$name] = $service; |
|
175
|
|
|
|
|
176
|
30 |
|
if (isset($this->children[$name])) { |
|
177
|
2 |
|
$this->children[$name]->setParentService($service); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
30 |
|
return $service; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|