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\Exceptions\CircularReferenceException; |
11
|
|
|
use Yiisoft\Factory\Exceptions\InvalidConfigException; |
12
|
|
|
use Yiisoft\Factory\Exceptions\NotFoundException; |
13
|
|
|
use Yiisoft\Factory\Exceptions\NotInstantiableException; |
14
|
|
|
use Yiisoft\Factory\Definitions\Normalizer; |
15
|
|
|
use Yiisoft\Factory\Definitions\ArrayDefinition; |
16
|
|
|
use Yiisoft\Injector\Injector; |
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 ?CompositeContainer $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
|
65 |
|
public function __construct( |
51
|
|
|
array $definitions = [], |
52
|
|
|
array $providers = [], |
53
|
|
|
ContainerInterface $rootContainer = null |
54
|
|
|
) { |
55
|
65 |
|
$this->delegateLookup($rootContainer); |
56
|
65 |
|
$this->setMultiple($definitions); |
57
|
63 |
|
$this->addProviders($providers); |
58
|
63 |
|
$this->resolveRecursiveContainer(); |
59
|
|
|
} |
60
|
63 |
|
|
61
|
|
|
private function resolveRecursiveContainer(): void |
62
|
|
|
{ |
63
|
62 |
|
if ($this->has(ContainerInterface::class)) { |
64
|
62 |
|
$this->get(ContainerInterface::class); |
65
|
|
|
} else { |
66
|
|
|
$this->set(ContainerInterface::class, $this->rootContainer ?? $this); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
72
|
63 |
|
* @param string $id class name, interface name or alias name |
73
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
74
|
63 |
|
* @see set() |
75
|
|
|
*/ |
76
|
|
|
public function has($id): bool |
77
|
|
|
{ |
78
|
|
|
return isset($this->definitions[$id]) || class_exists($id); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns an instance by either interface name or alias. |
83
|
|
|
* |
84
|
|
|
* Same instance of the class will be returned each time this method is called. |
85
|
|
|
* |
86
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
87
|
|
|
* @return object An instance of the requested interface. |
88
|
|
|
* @throws CircularReferenceException |
89
|
62 |
|
* @throws InvalidConfigException |
90
|
|
|
* @throws NotFoundException |
91
|
62 |
|
* @throws NotInstantiableException |
92
|
62 |
|
*/ |
93
|
|
|
public function get($id) |
94
|
|
|
{ |
95
|
62 |
|
if (!isset($this->instances[$id])) { |
96
|
|
|
$this->instances[$id] = $this->build($id); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->instances[$id]; |
100
|
|
|
} |
101
|
|
|
|
102
|
65 |
|
/** |
103
|
|
|
* Delegate service lookup to another container. |
104
|
65 |
|
* @param ContainerInterface $container |
105
|
65 |
|
*/ |
106
|
|
|
protected function delegateLookup(?ContainerInterface $container): void |
107
|
7 |
|
{ |
108
|
7 |
|
if ($container === null) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
7 |
|
if ($this->rootContainer === null) { |
112
|
7 |
|
$this->rootContainer = new CompositeContainer(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->rootContainer->attach($container); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
120
|
|
|
* @param string $id |
121
|
64 |
|
* @param mixed $definition |
122
|
|
|
* @throws InvalidConfigException |
123
|
64 |
|
* @see `Normalizer::normalize()` |
124
|
63 |
|
*/ |
125
|
63 |
|
protected function set(string $id, $definition): void |
126
|
63 |
|
{ |
127
|
|
|
Normalizer::validate($definition); |
128
|
|
|
unset($this->instances[$id]); |
129
|
|
|
$this->definitions[$id] = $definition; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
65 |
|
* Sets multiple definitions at once. |
134
|
|
|
* @param array $config definitions indexed by their ids |
135
|
65 |
|
* @throws InvalidConfigException |
136
|
54 |
|
*/ |
137
|
1 |
|
protected function setMultiple(array $config): void |
138
|
|
|
{ |
139
|
53 |
|
foreach ($config as $id => $definition) { |
140
|
|
|
if (!is_string($id)) { |
141
|
63 |
|
throw new InvalidConfigException('Key must be a string'); |
142
|
|
|
} |
143
|
|
|
$this->set($id, $definition); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Creates new instance by either interface name or alias. |
149
|
|
|
* |
150
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
151
|
|
|
* @return object New built instance of the specified class. |
152
|
|
|
* @throws CircularReferenceException |
153
|
62 |
|
* @throws InvalidConfigException |
154
|
|
|
* @throws NotFoundException |
155
|
62 |
|
* @internal |
156
|
12 |
|
*/ |
157
|
|
|
private function build(string $id) |
158
|
62 |
|
{ |
159
|
9 |
|
if ($id === Injector::class) { |
160
|
2 |
|
return new Injector($this); |
161
|
|
|
} |
162
|
7 |
|
if (isset($this->building[$id])) { |
163
|
7 |
|
if ($id === ContainerInterface::class) { |
164
|
|
|
return $this; |
165
|
7 |
|
} |
166
|
|
|
throw new CircularReferenceException(sprintf( |
167
|
|
|
'Circular reference to "%s" detected while building: %s', |
168
|
|
|
$id, |
169
|
62 |
|
implode(',', array_keys($this->building)) |
170
|
62 |
|
)); |
171
|
62 |
|
} |
172
|
|
|
|
173
|
62 |
|
$this->building[$id] = 1; |
174
|
|
|
$object = $this->buildInternal($id); |
175
|
|
|
unset($this->building[$id]); |
176
|
|
|
|
177
|
|
|
return $object; |
178
|
|
|
} |
179
|
62 |
|
|
180
|
|
|
/** |
181
|
62 |
|
* @param mixed $definition |
182
|
1 |
|
*/ |
183
|
|
|
private function processDefinition($definition): void |
184
|
62 |
|
{ |
185
|
|
|
if ($definition instanceof DeferredServiceProviderInterface) { |
186
|
|
|
$definition->register($this); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $id |
192
|
|
|
* |
193
|
62 |
|
* @return mixed|object |
194
|
|
|
* @throws InvalidConfigException |
195
|
62 |
|
* @throws NotFoundException |
196
|
41 |
|
*/ |
197
|
|
|
private function buildInternal(string $id) |
198
|
62 |
|
{ |
199
|
62 |
|
if (!isset($this->definitions[$id])) { |
200
|
|
|
return $this->buildPrimitive($id); |
201
|
62 |
|
} |
202
|
|
|
$this->processDefinition($this->definitions[$id]); |
203
|
|
|
$definition = Normalizer::normalize($this->definitions[$id], $id); |
204
|
|
|
|
205
|
|
|
return $definition->resolve($this->rootContainer ?? $this); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $class |
210
|
|
|
* |
211
|
41 |
|
* @return mixed|object |
212
|
|
|
* @throws InvalidConfigException |
213
|
41 |
|
* @throws NotFoundException |
214
|
39 |
|
*/ |
215
|
|
|
private function buildPrimitive(string $class) |
216
|
39 |
|
{ |
217
|
|
|
if (class_exists($class)) { |
218
|
|
|
$definition = new ArrayDefinition($class); |
219
|
4 |
|
|
220
|
|
|
return $definition->resolve($this->rootContainer ?? $this); |
221
|
|
|
} |
222
|
63 |
|
|
223
|
|
|
throw new NotFoundException("No definition for $class"); |
224
|
63 |
|
} |
225
|
5 |
|
|
226
|
|
|
private function addProviders(array $providers): void |
227
|
62 |
|
{ |
228
|
|
|
foreach ($providers as $provider) { |
229
|
|
|
$this->addProvider($provider); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Adds service provider to the container. Unless service provider is deferred |
235
|
|
|
* it would be immediately registered. |
236
|
|
|
* |
237
|
|
|
* @param mixed $providerDefinition |
238
|
|
|
* |
239
|
|
|
* @throws InvalidConfigException |
240
|
5 |
|
* @throws NotInstantiableException |
241
|
|
|
* @see ServiceProviderInterface |
242
|
5 |
|
* @see DeferredServiceProviderInterface |
243
|
|
|
*/ |
244
|
4 |
|
private function addProvider($providerDefinition): void |
245
|
1 |
|
{ |
246
|
1 |
|
$provider = $this->buildProvider($providerDefinition); |
247
|
|
|
|
248
|
|
|
if ($provider instanceof DeferredServiceProviderInterface) { |
249
|
3 |
|
foreach ($provider->provides() as $id) { |
250
|
|
|
$this->definitions[$id] = $provider; |
251
|
4 |
|
} |
252
|
|
|
} else { |
253
|
|
|
$provider->register($this); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Builds service provider by definition. |
259
|
|
|
* |
260
|
|
|
* @param mixed $providerDefinition class name or definition of provider. |
261
|
5 |
|
* @return ServiceProviderInterface instance of service provider; |
262
|
|
|
* |
263
|
5 |
|
* @throws InvalidConfigException |
264
|
|
|
*/ |
265
|
|
|
private function buildProvider($providerDefinition): ServiceProviderInterface |
266
|
|
|
{ |
267
|
|
|
$provider = Normalizer::normalize($providerDefinition)->resolve($this); |
268
|
4 |
|
assert($provider instanceof ServiceProviderInterface, new InvalidConfigException( |
269
|
|
|
'Service provider should be an instance of ' . ServiceProviderInterface::class |
270
|
|
|
)); |
271
|
|
|
|
272
|
|
|
return $provider; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.