1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Di\Contracts\DeferredServiceProviderInterface; |
9
|
|
|
use Yiisoft\Di\Contracts\ServiceProviderInterface; |
10
|
|
|
use Yiisoft\Factory\Definitions\Reference; |
11
|
|
|
use Yiisoft\Factory\Exceptions\CircularReferenceException; |
12
|
|
|
use Yiisoft\Factory\Exceptions\InvalidConfigException; |
13
|
|
|
use Yiisoft\Factory\Exceptions\NotFoundException; |
14
|
|
|
use Yiisoft\Factory\Exceptions\NotInstantiableException; |
15
|
|
|
use Yiisoft\Factory\Definitions\Normalizer; |
16
|
|
|
use Yiisoft\Factory\Definitions\ArrayDefinition; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Container implements a [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) container. |
20
|
|
|
*/ |
21
|
|
|
final class Container extends AbstractContainerConfigurator implements ContainerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array object definitions indexed by their types |
25
|
|
|
*/ |
26
|
|
|
private array $definitions = []; |
27
|
|
|
/** |
28
|
|
|
* @var array used to collect ids instantiated during build |
29
|
|
|
* to detect circular references |
30
|
|
|
*/ |
31
|
|
|
private array $building = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var object[] |
35
|
|
|
*/ |
36
|
|
|
private array $instances = []; |
37
|
|
|
|
38
|
|
|
private ?ContainerInterface $rootContainer = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Container constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $definitions Definitions to put into container. |
44
|
|
|
* @param ServiceProviderInterface[]|string[] $providers Service providers to get definitions from. |
45
|
|
|
* |
46
|
|
|
* @param ContainerInterface|null $rootContainer Root container to delegate lookup to in case definition |
47
|
|
|
* is not found in current container. |
48
|
|
|
* @throws InvalidConfigException |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
57 |
|
array $definitions = [], |
52
|
|
|
array $providers = [], |
53
|
|
|
ContainerInterface $rootContainer = null |
54
|
|
|
) { |
55
|
|
|
$this->setMultiple($definitions); |
56
|
57 |
|
$this->addProviders($providers); |
57
|
55 |
|
if ($rootContainer !== null) { |
58
|
54 |
|
$this->delegateLookup($rootContainer); |
59
|
5 |
|
} |
60
|
|
|
} |
61
|
54 |
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
64
|
|
|
* @param string $id class name, interface name or alias name |
65
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
66
|
|
|
* @see set() |
67
|
|
|
*/ |
68
|
|
|
public function has($id): bool |
69
|
24 |
|
{ |
70
|
|
|
return isset($this->definitions[$id]) || class_exists($id); |
71
|
24 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns an instance by either interface name or alias. |
75
|
|
|
* |
76
|
|
|
* Same instance of the class will be returned each time this method is called. |
77
|
|
|
* |
78
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
79
|
|
|
* @return object An instance of the requested interface. |
80
|
|
|
* @throws CircularReferenceException |
81
|
|
|
* @throws InvalidConfigException |
82
|
|
|
* @throws NotFoundException |
83
|
|
|
* @throws NotInstantiableException |
84
|
|
|
*/ |
85
|
|
|
public function get($id) |
86
|
50 |
|
{ |
87
|
|
|
if (!isset($this->instances[$id])) { |
88
|
50 |
|
$this->instances[$id] = $this->build($id); |
89
|
50 |
|
} |
90
|
|
|
|
91
|
|
|
return $this->instances[$id]; |
92
|
41 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delegate service lookup to another container. |
96
|
|
|
* @param ContainerInterface $container |
97
|
|
|
*/ |
98
|
|
|
protected function delegateLookup(ContainerInterface $container): void |
99
|
5 |
|
{ |
100
|
|
|
if ($this->rootContainer === null) { |
101
|
5 |
|
$this->rootContainer = new CompositeContainer(); |
102
|
5 |
|
} |
103
|
|
|
|
104
|
|
|
$this->rootContainer->attach($container); |
|
|
|
|
105
|
5 |
|
} |
106
|
5 |
|
|
107
|
|
|
/** |
108
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
109
|
|
|
* @param string $id |
110
|
|
|
* @param mixed $definition |
111
|
|
|
* @throws InvalidConfigException |
112
|
|
|
* @see `Normalizer::normalize()` |
113
|
|
|
*/ |
114
|
|
|
protected function set(string $id, $definition): void |
115
|
48 |
|
{ |
116
|
|
|
Normalizer::validate($definition); |
|
|
|
|
117
|
48 |
|
$this->instances[$id] = null; |
118
|
47 |
|
$this->definitions[$id] = $definition; |
119
|
47 |
|
} |
120
|
47 |
|
|
121
|
|
|
/** |
122
|
|
|
* Sets multiple definitions at once. |
123
|
|
|
* @param array $config definitions indexed by their ids |
124
|
|
|
* @throws InvalidConfigException |
125
|
|
|
*/ |
126
|
|
|
protected function setMultiple(array $config): void |
127
|
57 |
|
{ |
128
|
|
|
foreach ($config as $id => $definition) { |
129
|
57 |
|
if (!is_string($id)) { |
130
|
46 |
|
throw new InvalidConfigException('Key must be a string'); |
131
|
1 |
|
} |
132
|
|
|
$this->set((string)$id, $definition); |
133
|
45 |
|
} |
134
|
|
|
} |
135
|
55 |
|
|
136
|
|
|
/** |
137
|
|
|
* Creates new instance by either interface name or alias. |
138
|
|
|
* |
139
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
140
|
|
|
* @return object New built instance of the specified class. |
141
|
|
|
* @throws CircularReferenceException |
142
|
|
|
* @throws InvalidConfigException |
143
|
|
|
* @throws NotFoundException |
144
|
|
|
* @internal |
145
|
|
|
*/ |
146
|
|
|
private function build(string $id) |
147
|
50 |
|
{ |
148
|
|
|
if (isset($this->building[$id])) { |
149
|
50 |
|
throw new CircularReferenceException(sprintf( |
150
|
7 |
|
'Circular reference to "%s" detected while building: %s', |
151
|
7 |
|
$id, |
152
|
|
|
implode(',', array_keys($this->building)) |
153
|
7 |
|
)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->building[$id] = 1; |
157
|
50 |
|
$object = $this->buildInternal($id); |
158
|
50 |
|
unset($this->building[$id]); |
159
|
41 |
|
|
160
|
|
|
return $object; |
161
|
41 |
|
} |
162
|
|
|
|
163
|
|
|
private function processDefinition($definition): void |
164
|
43 |
|
{ |
165
|
|
|
if ($definition instanceof DeferredServiceProviderInterface) { |
166
|
43 |
|
$definition->register($this); |
167
|
1 |
|
} |
168
|
|
|
} |
169
|
43 |
|
|
170
|
|
|
/** |
171
|
48 |
|
* @param string $id |
172
|
|
|
* |
173
|
48 |
|
* @return mixed|object |
174
|
3 |
|
* @throws InvalidConfigException |
175
|
|
|
* @throws NotFoundException |
176
|
|
|
*/ |
177
|
47 |
|
private function buildInternal(string $id) |
178
|
38 |
|
{ |
179
|
|
|
if (!isset($this->definitions[$id])) { |
180
|
|
|
return $this->buildPrimitive($id); |
181
|
17 |
|
} |
182
|
5 |
|
$this->processDefinition($this->definitions[$id]); |
183
|
|
|
$definition = Normalizer::normalize($this->definitions[$id], $id); |
184
|
|
|
|
185
|
12 |
|
return $definition->resolve($this->rootContainer ?? $this); |
186
|
8 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
4 |
|
* @param string $class |
190
|
3 |
|
* |
191
|
|
|
* @return mixed|object |
192
|
|
|
* @throws InvalidConfigException |
193
|
1 |
|
* @throws NotFoundException |
194
|
|
|
*/ |
195
|
|
|
private function buildPrimitive(string $class) |
196
|
|
|
{ |
197
|
|
|
if (class_exists($class)) { |
198
|
|
|
$definition = new ArrayDefinition($class); |
199
|
|
|
|
200
|
|
|
return $definition->resolve($this->rootContainer ?? $this); |
201
|
|
|
} |
202
|
|
|
|
203
|
50 |
|
throw new NotFoundException("No definition for $class"); |
204
|
|
|
} |
205
|
50 |
|
|
206
|
35 |
|
private function addProviders(array $providers): void |
207
|
|
|
{ |
208
|
43 |
|
foreach ($providers as $provider) { |
209
|
43 |
|
$this->addProvider($provider); |
210
|
|
|
} |
211
|
43 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Adds service provider to the container. Unless service provider is deferred |
215
|
|
|
* it would be immediately registered. |
216
|
|
|
* |
217
|
|
|
* @param string|array $providerDefinition |
218
|
|
|
* |
219
|
|
|
* @throws InvalidConfigException |
220
|
|
|
* @throws NotInstantiableException |
221
|
35 |
|
* @see ServiceProviderInterface |
222
|
|
|
* @see DeferredServiceProviderInterface |
223
|
35 |
|
*/ |
224
|
33 |
|
private function addProvider($providerDefinition): void |
225
|
|
|
{ |
226
|
33 |
|
$provider = $this->buildProvider($providerDefinition); |
227
|
|
|
|
228
|
|
|
if ($provider instanceof DeferredServiceProviderInterface) { |
229
|
3 |
|
foreach ($provider->provides() as $id) { |
230
|
|
|
$this->definitions[$id] = $provider; |
231
|
|
|
} |
232
|
55 |
|
} else { |
233
|
|
|
$provider->register($this); |
234
|
55 |
|
} |
235
|
4 |
|
} |
236
|
|
|
|
237
|
54 |
|
/** |
238
|
|
|
* Builds service provider by definition. |
239
|
|
|
* |
240
|
|
|
* @param string|array $providerDefinition class name or definition of provider. |
241
|
|
|
* @return ServiceProviderInterface instance of service provider; |
242
|
|
|
* |
243
|
|
|
* @throws InvalidConfigException |
244
|
|
|
*/ |
245
|
|
|
private function buildProvider($providerDefinition): ServiceProviderInterface |
246
|
|
|
{ |
247
|
|
|
$provider = Normalizer::normalize($providerDefinition)->resolve($this); |
248
|
|
|
if (!($provider instanceof ServiceProviderInterface)) { |
249
|
|
|
throw new InvalidConfigException( |
250
|
4 |
|
'Service provider should be an instance of ' . ServiceProviderInterface::class |
251
|
|
|
); |
252
|
4 |
|
} |
253
|
|
|
|
254
|
3 |
|
return $provider; |
255
|
1 |
|
} |
256
|
|
|
} |
257
|
|
|
|