Repository navigation
Expand file tree
/
Copy pathContainerAdapter.php
More file actions
313 lines (276 loc) · 8.13 KB
/
Copy pathContainerAdapter.php
File metadata and controls
313 lines (276 loc) · 8.13 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php declare(strict_types=1);
namespace StellarWP\Foundation\Container;
use Closure;
use lucatume\DI52\Container as DI52Container;
use lucatume\DI52\ContainerException as DI52ContainerException;
use lucatume\DI52\NotFoundException as DI52NotFoundException;
use StellarWP\Foundation\Container\Contracts\Container;
use StellarWP\Foundation\Container\Contracts\Providable;
use StellarWP\Foundation\Container\Exceptions\ContainerException;
use StellarWP\Foundation\Container\Exceptions\NotFoundException;
use Throwable;
/**
* Adapts DI52 to Foundation's stable container and resolver contracts.
*
* Prefer {@see ContainerFactory} for application setup. A custom composition
* root that constructs this adapter directly must register Foundation's core
* contracts, including Configuration, before it registers any Provider.
*/
final class ContainerAdapter implements Container
{
/**
* @var array<string, Closure>
*/
private array $callbacks = [];
/**
* Wrap the underlying DI52 container without exposing it to application services.
*/
public function __construct(
private readonly DI52Container $container
) {
}
/**
* Register a transient service binding with the underlying container.
*
* @param string[]|null $afterBuildMethods
*
* @throws ContainerException When the binding cannot be registered.
*/
public function bind(string $id, mixed $implementation = null, ?array $afterBuildMethods = null): void {
$this->call(fn () => $this->container->bind($id, $this->adaptImplementation($implementation), $afterBuildMethods));
}
/**
* {@inheritDoc}
*/
public function get(string $id): mixed {
return $this->call(fn (): mixed => $this->container->get($id));
}
/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public function has(string $id): bool {
return $this->container->has($id);
}
/**
* Register a shared service binding with the underlying container.
*
* @param string[]|null $afterBuildMethods
*
* @throws ContainerException When the singleton cannot be registered.
*/
public function singleton(string $id, mixed $implementation = null, ?array $afterBuildMethods = null): void {
$this->call(fn () => $this->container->singleton($id, $this->adaptImplementation($implementation), $afterBuildMethods));
}
/**
* Validate, resolve, and register a Foundation provider with optional aliases.
*
* @param class-string $serviceProviderClass
*
* @throws ContainerException When the class is not a Foundation provider or cannot be registered.
*/
public function register(string $serviceProviderClass, ...$alias): void {
if (! is_a($serviceProviderClass, Providable::class, true)) {
throw new ContainerException(sprintf(
'%s must implement %s to be registered as a provider.',
$serviceProviderClass,
Providable::class
));
}
$provider = $this->resolveProvider($serviceProviderClass);
$provider->register();
$this->singleton($serviceProviderClass, $provider);
foreach ($alias as $providerAlias) {
$this->singleton($providerAlias, $provider);
}
}
/**
* Check the resolved binding as applications can replace a provider class with a factory.
*/
private function resolveProvider(string $id): Providable {
$provider = $this->get($id);
if (! $provider instanceof Providable) {
throw new ContainerException(sprintf(
'The binding for %s must resolve to %s; received %s.',
$id,
Providable::class,
get_debug_type($provider)
));
}
return $provider;
}
/**
* {@inheritDoc}
*/
public function when(string $class): Container {
$this->call(fn () => $this->container->when($class));
return $this;
}
/**
* {@inheritDoc}
*/
public function needs(string $id): Container {
$this->call(fn () => $this->container->needs($id));
return $this;
}
/**
* {@inheritDoc}
*/
public function give(mixed $implementation): void {
$this->call(fn () => $this->container->give($this->adaptImplementation($implementation)));
}
/**
* {@inheritDoc}
*
* @throws ContainerException When the additive binding cannot be registered.
*/
public function mergeArrayVar(string $id, mixed $implementation): void {
$this->call(fn () => $this->container->mergeArrayVar($id, $this->adaptImplementation($implementation)));
}
/**
* {@inheritDoc}
*/
public function instance(mixed $id, array $buildArgs = [], ?array $afterBuildMethods = null): Closure {
$implementation = $this->adaptImplementation($id);
/**
* @var Closure $factory
*/
$factory = $this->call(
// @phpstan-ignore-next-line invalid DocBlock comments in DI52
fn (): Closure => $this->container->instance($implementation, $buildArgs, $afterBuildMethods)
);
return fn (): mixed => $this->resolveDeferred(static fn (): mixed => $factory());
}
/**
* Create a stable callable that resolves its target service when invoked.
*
* @param class-string|string|object $id
*
* @throws ContainerException
*/
public function callback(object|string $id, string $method): callable {
$callbackId = (is_object($id) ? 'object:' . spl_object_id($id) : 'service:' . $id)
. '::' . $method;
return $this->callbacks[$callbackId] ??= function (mixed ...$args) use ($id, $method): mixed {
$instance = is_object($id) ? $id : $this->get($id);
if (! is_callable([$instance, $method])) {
throw new ContainerException(sprintf(
'%s::%s is not a callable container callback.',
is_object($instance) ? $instance::class : get_debug_type($instance),
$method
));
}
return $instance->{$method}(...$args);
};
}
/**
* {@inheritDoc}
*/
public function singletonDecorators(
string $id,
array $decorators,
?array $afterBuildMethods = null,
bool $afterBuildAll = false
): void {
$this->call(
fn () => $this->container->singletonDecorators(
$id,
array_map($this->adaptImplementation(...), $decorators),
$afterBuildMethods,
$afterBuildAll
)
);
}
/**
* {@inheritDoc}
*/
public function bindDecorators(
string $id,
array $decorators,
?array $afterBuildMethods = null,
bool $afterBuildAll = false
): void {
$this->call(
fn () => $this->container->bindDecorators(
$id,
array_map($this->adaptImplementation(...), $decorators),
$afterBuildMethods,
$afterBuildAll
)
);
}
/**
* Adapt a factory closure so it receives Foundation's resolver rather than DI52.
*/
private function adaptImplementation(mixed $implementation): mixed {
if (! $implementation instanceof Closure) {
return $implementation;
}
$resolver = $this;
return static fn (): mixed => $implementation($resolver);
}
/**
* Invoke a DI52 operation while keeping backend exceptions behind Foundation's API.
*
* @template T
*
* @param Closure(): T $operation
*
* @throws ContainerException When the underlying container operation fails.
* @throws NotFoundException When a requested entry does not exist.
*
* @return T
*/
private function call(Closure $operation): mixed {
try {
return $operation();
} catch (DI52NotFoundException $exception) {
throw new NotFoundException(
$exception->getMessage(),
$exception->getCode(),
$this->previous($exception)
);
} catch (DI52ContainerException $exception) {
throw new ContainerException(
$exception->getMessage(),
$exception->getCode(),
$this->previous($exception)
);
}
}
/**
* Resolve a deferred factory while normalizing failures that bypass DI52's get() boundary.
*
* @template T
*
* @param Closure(): T $operation
*
* @throws ContainerException When the deferred service cannot be resolved.
*
* @return T
*/
private function resolveDeferred(Closure $operation): mixed {
try {
return $this->call($operation);
} catch (ContainerException $exception) {
throw $exception;
} catch (Throwable $exception) {
throw new ContainerException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
/**
* Preserve the application failure wrapped by DI52 when one is available.
*/
private function previous(DI52ContainerException $exception): ?Throwable {
$previous = $exception->getPrevious();
while ($previous instanceof DI52ContainerException) {
$previous = $previous->getPrevious();
}
return $previous;
}
}