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