-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIContainer.php
294 lines (267 loc) · 9.26 KB
/
DIContainer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php declare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded;
use Psr\Container\ContainerInterface;
use function assert;
use function call_user_func_array;
use function class_implements;
use function interface_exists;
use function preg_match;
/**
* Interface DIModule contributes the application configuration,
* typically the interface binding which are used to inject the dependencies.
*
* The application is composed of a set of DIModules and some bootstrapping code.
*/
interface DIModule
{
/**
* Provides bindings and other configurations for this app module.
* Also reduces the repetition and results in a more readable configuration.
* Implement the `configure()` method to bind your interfaces.
*
* ex: `$container->bind(MyInterface::class, MyImplementation::class);`
*
* @param DIContainer $container
*/
public function configure(DIContainer $container): void;
}
interface IContainer extends ContainerInterface
{
/**
* Creates a new instance of a class. Builds the graph of objects that make up the application.
* It can also inject already created dependencies behind the scene (with singleton and share).
*
* @param string $class FQCN
* @param array $arguments [optional] The arguments for the class constructor.
* They have top precedence over the shared dependencies
* @return object|null
*/
public function new(string $class, array $arguments = []): ?object;
/**
* Binds the interface to concrete class implementation.
* It does not create objects, but prepares the container for dependency injection.
*
* This method should be used in the app modules (DIModule).
*
* @param string $interface FQN of the interface
* @param string $class FQCN of the concrete class implementation,
* or empty value for deferred binding
* @return DIContainer
*/
public function bind(string $interface, string $class = ''): IContainer;
/**
* Create once and share an object throughout the application lifecycle.
* Internally the object is immutable, but it can be replaced with share() method.
*
* @param string $class FQCN
* @param array $arguments [optional] See new() description
* @return object
*/
public function singleton(string $class, array $arguments = []): object;
/**
* Share already created instance of an object throughout the app lifecycle.
*
* @param object $instance The object that will be shared as dependency
* @param array $exclude [optional] A list of FQCNs that should
* be excluded from injecting this instance.
* In this case, a new object will be created and
* injected for these classes
* @return DIContainer
*/
public function share(object $instance, array $exclude = []): IContainer;
/**
* Shares an object globally by argument name.
*
* @param string $name The name of the argument
* @param mixed $value The actual value
* @return DIContainer
*/
public function named(string $name, mixed $value): IContainer;
}
/**
* Storage types for internal bindings, instances, etc.
*/
enum DIStorage
{
case SINGLETONS;
case BINDINGS;
case EXCLUDE;
case NAMED;
}
/**
* The entry point of the DIContainer that draws the lines between the
* APIs, implementation of these APIs, modules that configure these
* implementations and applications that consist of a collection of modules.
*
* ```
* $container = new DIContainer(new ModuleA, new ModuleB, ... new ModuleZ);
* ($container)([AppEntry::class, 'method']);
* ```
*/
class DIContainer implements IContainer
{
protected DIReflector $reflector;
private array $inProgress = [];
private array $singletons = [];
private array $bindings = [];
private array $exclude = [];
private array $named = [];
public function __construct(DIModule ...$modules)
{
$this->reflector = new DIReflector;
foreach ((array)$modules as $module) {
$module->configure($this);
}
}
public function __clone()
{
$this->inProgress = [];
$this->singletons = [];
$this->named = [];
}
public function __destruct()
{
$this->singletons = [];
$this->bindings = [];
$this->exclude = [];
$this->named = [];
}
/**
* @throws \ReflectionException
*/
public function __invoke(callable $callable, array $arguments = [])
{
return call_user_func_array($callable, $this->reflector->processMethodArguments(
$this, $this->reflector->newMethodFromCallable($callable), $arguments
));
}
public function new(string $class, array $arguments = []): ?object
{
$binding = $this->getBinding($class);
if (isset($this->inProgress[$binding])) {
throw DIException::forCircularDependency($binding);
}
$this->inProgress[$binding] = true;
try {
return $this->newInstance($binding, $arguments);
} finally {
unset($this->inProgress[$binding]);
}
}
public function singleton(string $class, array $arguments = []): object
{
$class = $this->getBinding($class);
return $this->singletons[$class] ??= $this->new($class, $arguments);
}
public function share(object $instance, array $exclude = []): IContainer
{
$class = $instance::class;
$this->singletons[$class] = $instance;
$this->bindInterfaces($class, $class);
foreach ($exclude as $name) {
$this->exclude[$name][$class] = $class;
}
return $this;
}
public function bind(string $interface, string $class = ''): IContainer
{
assert(false === empty($interface), 'Dependency name for bind() method');
if ('$' === ($class[0] ?? null)) {
$this->bindings[$interface] = $interface;
$class && $this->bindings[$class] = $interface;
return $this;
}
$this->bindInterfaces($interface, $class);
return $this;
}
public function named(string $name, mixed $value): IContainer
{
if (1 !== preg_match('/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $name)) {
throw DIException::forInvalidParameterName($name);
}
$this->named[$name] = $value;
return $this;
}
/**
* @inheritDoc
*/
public function has($id): bool
{
assert(false === empty($id), 'Dependency name for has() method');
return isset($this->bindings[$id]) || isset($this->named[$id]);
}
/**
* @inheritDoc
*/
public function get($id): mixed
{
$this->has($id) || throw DIInstanceNotFound::for($id);
$dependency = $this->getBinding($id);
return $this->singletons[$dependency]
?? $this->named[$dependency]
?? $this->new($dependency);
}
/**
* @internal
*/
public function getFromStorage(DIStorage $type, string $dependency = ''): mixed
{
return $dependency ? match ($type) {
DIStorage::BINDINGS => $this->bindings[$dependency] ?? $dependency,
DIStorage::SINGLETONS => $this->singletons[$dependency] ?? null,
DIStorage::EXCLUDE => $this->exclude[$dependency] ?? [],
DIStorage::NAMED => $this->named['$' . $dependency] ?? $this->named[$dependency] ?? null,
} : match ($type) {
DIStorage::BINDINGS => $this->bindings,
DIStorage::SINGLETONS => $this->singletons,
DIStorage::EXCLUDE => $this->exclude,
DIStorage::NAMED => $this->named,
};
}
/**
* @internal
*/
public function getBinding(string $dependency): string
{
assert(false === empty($dependency), 'Dependency name for class/interface');
return $this->bindings[$dependency] ?? $dependency;
}
private function newInstance(string $class, array $arguments): object
{
$this->bindings[$class] = $class;
return $this->reflector->newInstance($this, $class, $arguments);
}
private function bindInterfaces(string $dependency, string $class): void
{
if (interface_exists($class)) {
throw DIException::forInterfaceBinding($dependency, $class);
}
$this->bindings[$dependency] = $class;
foreach ($this->bindings as $dependency => $class) {
$this->mapDeferred($dependency, $class);
}
}
private function mapDeferred(string $dependency, string $class): void
{
foreach (class_implements($dependency) as $interface) {
if (false === isset($this->bindings[$interface])) {
continue;
}
if (false === empty($class)) {
$this->bindings[$interface] = $class;
continue;
}
$this->bindings[$dependency] = $this->bindings[$interface];
break;
}
}
}