|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Gacela\Container\Exception\ContainerException; |
|
9
|
|
|
use SplObjectStorage; |
|
10
|
|
|
|
|
11
|
|
|
use function count; |
|
12
|
|
|
use function is_array; |
|
13
|
|
|
use function is_callable; |
|
14
|
|
|
use function is_object; |
|
15
|
|
|
|
|
16
|
|
|
class Container implements ContainerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private ?DependencyResolver $dependencyResolver = null; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array<class-string,list<mixed>> */ |
|
21
|
|
|
private array $cachedDependencies = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<string,mixed> */ |
|
24
|
|
|
private array $instances = []; |
|
25
|
|
|
|
|
26
|
|
|
private SplObjectStorage $factoryInstances; |
|
27
|
|
|
|
|
28
|
|
|
private SplObjectStorage $protectedInstances; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array<string,bool> */ |
|
31
|
|
|
private array $frozenInstances = []; |
|
32
|
|
|
|
|
33
|
|
|
private ?string $currentlyExtending = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array<class-string, class-string|callable|object> $bindings |
|
37
|
|
|
* @param array<string, list<Closure>> $instancesToExtend |
|
38
|
|
|
*/ |
|
39
|
37 |
|
public function __construct( |
|
40
|
|
|
private array $bindings = [], |
|
41
|
|
|
private array $instancesToExtend = [], |
|
42
|
|
|
) { |
|
43
|
37 |
|
$this->factoryInstances = new SplObjectStorage(); |
|
44
|
37 |
|
$this->protectedInstances = new SplObjectStorage(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param class-string $className |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public static function create(string $className): mixed |
|
51
|
|
|
{ |
|
52
|
5 |
|
return (new self())->get($className); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
37 |
|
public function has(string $id): bool |
|
56
|
|
|
{ |
|
57
|
37 |
|
return isset($this->instances[$id]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
19 |
|
public function set(string $id, mixed $instance): void |
|
61
|
|
|
{ |
|
62
|
19 |
|
if (!empty($this->frozenInstances[$id])) { |
|
63
|
1 |
|
throw ContainerException::frozenInstanceOverride($id); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
19 |
|
$this->instances[$id] = $instance; |
|
67
|
|
|
|
|
68
|
19 |
|
if ($this->currentlyExtending === $id) { |
|
69
|
3 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
19 |
|
$this->extendService($id); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param class-string|string $id |
|
77
|
|
|
*/ |
|
78
|
33 |
|
public function get(string $id): mixed |
|
79
|
|
|
{ |
|
80
|
33 |
|
if ($this->has($id)) { |
|
81
|
15 |
|
return $this->getInstance($id); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
18 |
|
return $this->createInstance($id); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
public function factory(Closure $instance): Closure |
|
88
|
|
|
{ |
|
89
|
1 |
|
$this->factoryInstances->attach($instance); |
|
90
|
|
|
|
|
91
|
1 |
|
return $instance; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function remove(string $id): void |
|
95
|
|
|
{ |
|
96
|
1 |
|
unset( |
|
97
|
1 |
|
$this->instances[$id], |
|
98
|
1 |
|
$this->frozenInstances[$id] |
|
99
|
1 |
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @psalm-suppress MixedAssignment |
|
104
|
|
|
*/ |
|
105
|
11 |
|
public function extend(string $id, Closure $instance): Closure |
|
106
|
|
|
{ |
|
107
|
11 |
|
if (!$this->has($id)) { |
|
108
|
4 |
|
$this->extendLater($id, $instance); |
|
109
|
|
|
|
|
110
|
4 |
|
return $instance; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
10 |
|
if (isset($this->frozenInstances[$id])) { |
|
114
|
4 |
|
throw ContainerException::frozenInstanceExtend($id); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
8 |
|
if (is_object($this->instances[$id]) && isset($this->protectedInstances[$this->instances[$id]])) { |
|
118
|
1 |
|
throw ContainerException::instanceProtected($id); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
7 |
|
$factory = $this->instances[$id]; |
|
122
|
7 |
|
$extended = $this->generateExtendedInstance($instance, $factory); |
|
123
|
6 |
|
$this->set($id, $extended); |
|
124
|
|
|
|
|
125
|
6 |
|
return $extended; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
public function protect(Closure $instance): Closure |
|
129
|
|
|
{ |
|
130
|
2 |
|
$this->protectedInstances->attach($instance); |
|
131
|
|
|
|
|
132
|
2 |
|
return $instance; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
15 |
|
private function getInstance(string $id): mixed |
|
136
|
|
|
{ |
|
137
|
15 |
|
$this->frozenInstances[$id] = true; |
|
138
|
|
|
|
|
139
|
15 |
|
if (!is_object($this->instances[$id]) |
|
140
|
14 |
|
|| isset($this->protectedInstances[$this->instances[$id]]) |
|
141
|
15 |
|
|| !method_exists($this->instances[$id], '__invoke') |
|
142
|
|
|
) { |
|
143
|
8 |
|
return $this->instances[$id]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
12 |
|
if (isset($this->factoryInstances[$this->instances[$id]])) { |
|
147
|
1 |
|
return $this->instances[$id]($this); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
11 |
|
$rawService = $this->instances[$id]; |
|
151
|
|
|
|
|
152
|
|
|
/** @var mixed $resolvedService */ |
|
153
|
11 |
|
$resolvedService = $rawService($this); |
|
154
|
|
|
|
|
155
|
11 |
|
$this->instances[$id] = $resolvedService; |
|
156
|
|
|
|
|
157
|
11 |
|
return $resolvedService; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
18 |
|
private function createInstance(string $class): ?object |
|
161
|
|
|
{ |
|
162
|
18 |
|
if (isset($this->bindings[$class])) { |
|
163
|
4 |
|
$binding = $this->bindings[$class]; |
|
164
|
4 |
|
if (is_callable($binding)) { |
|
165
|
|
|
/** @var mixed $binding */ |
|
166
|
2 |
|
$binding = $binding(); |
|
167
|
|
|
} |
|
168
|
4 |
|
if (is_object($binding)) { |
|
169
|
2 |
|
return $binding; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** @var class-string $binding */ |
|
173
|
2 |
|
if (class_exists($binding)) { |
|
174
|
2 |
|
return $this->instantiateClass($binding); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
14 |
|
if (class_exists($class)) { |
|
179
|
10 |
|
return $this->instantiateClass($class); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
4 |
|
return null; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param class-string $class |
|
187
|
|
|
*/ |
|
188
|
12 |
|
private function instantiateClass(string $class): ?object |
|
189
|
|
|
{ |
|
190
|
12 |
|
if (class_exists($class)) { |
|
191
|
12 |
|
if (!isset($this->cachedDependencies[$class])) { |
|
192
|
12 |
|
$this->cachedDependencies[$class] = $this |
|
193
|
12 |
|
->getDependencyResolver() |
|
194
|
12 |
|
->resolveDependencies($class); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** @psalm-suppress MixedMethodCall */ |
|
198
|
12 |
|
return new $class(...$this->cachedDependencies[$class]); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return null; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
4 |
|
private function extendLater(string $id, Closure $instance): void |
|
205
|
|
|
{ |
|
206
|
4 |
|
$this->instancesToExtend[$id][] = $instance; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
12 |
|
private function getDependencyResolver(): DependencyResolver |
|
210
|
|
|
{ |
|
211
|
12 |
|
if ($this->dependencyResolver === null) { |
|
212
|
12 |
|
$this->dependencyResolver = new DependencyResolver( |
|
213
|
12 |
|
$this->bindings, |
|
214
|
12 |
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
12 |
|
return $this->dependencyResolver; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @psalm-suppress MissingClosureReturnType,MixedAssignment |
|
222
|
|
|
*/ |
|
223
|
7 |
|
private function generateExtendedInstance(Closure $instance, mixed $factory): Closure |
|
224
|
|
|
{ |
|
225
|
7 |
|
if (is_callable($factory)) { |
|
226
|
5 |
|
return static function (self $container) use ($instance, $factory) { |
|
227
|
5 |
|
$result = $factory($container); |
|
228
|
|
|
|
|
229
|
5 |
|
return $instance($result, $container) ?? $result; |
|
230
|
5 |
|
}; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
4 |
|
if (is_object($factory) || is_array($factory)) { |
|
234
|
3 |
|
return static fn (self $container) => $instance($factory, $container) ?? $factory; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
1 |
|
throw ContainerException::instanceNotExtendable(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
19 |
|
private function extendService(string $id): void |
|
241
|
|
|
{ |
|
242
|
19 |
|
if (!isset($this->instancesToExtend[$id]) || count($this->instancesToExtend[$id]) === 0) { |
|
243
|
16 |
|
return; |
|
244
|
|
|
} |
|
245
|
3 |
|
$this->currentlyExtending = $id; |
|
246
|
|
|
|
|
247
|
3 |
|
foreach ($this->instancesToExtend[$id] as $instance) { |
|
248
|
3 |
|
$this->extend($id, $instance); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
3 |
|
unset($this->instancesToExtend[$id]); |
|
252
|
3 |
|
$this->currentlyExtending = null; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|