-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDic.php
More file actions
313 lines (276 loc) · 8.94 KB
/
Copy pathDic.php
File metadata and controls
313 lines (276 loc) · 8.94 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 Thesis;
use Thesis\Dic\Configuration\FunctionAutoconfig;
use Thesis\Dic\Configuration\FunctionConfig;
use Thesis\Dic\Configuration\ObjectAutoconfig;
use Thesis\Dic\Configuration\ObjectConfig;
use Thesis\Dic\Configuration\ProviderConfig;
use Thesis\Dic\Configuration\ScopedConfig;
use Thesis\Dic\Configuration\TaggedListConfig;
use Thesis\Dic\Configuration\ValueConfig;
use Thesis\Dic\DisposalFailed;
use Thesis\Dic\Internal\Autowiring;
use Thesis\Dic\Internal\Builder;
use Thesis\Dic\Internal\Builder\Autoconfiguration;
use Thesis\Dic\Internal\Factory\ValueFactory;
use Thesis\Dic\Internal\NonCopyable;
use Thesis\Dic\Location;
use Thesis\Dic\Module;
use Thesis\Dic\Ref;
use Thesis\Dic\Tag;
use Thesis\Dic\TaggedRef;
use Thesis\Dic\TaggedRefs;
use function Thesis\Dic\Internal\caller;
/**
* @api
*/
final readonly class Dic
{
use NonCopyable;
/**
* The recommended entry point. Runs $function with the service the module
* returns, then disposes the scope and the container (even on throw).
*
* @template T
* @template R
* @param Module<T|Ref<T>> $module
* @param callable(T): R $main
* @return R
*/
public static function run(Module $module, callable $main): mixed
{
$builder = new Builder();
$result = new self($builder)->import($module);
$container = $builder->build();
// a scope is used to resolve a service of any lifetime: singleton or scoped
$scope = $container->startScope();
$error = null;
try {
$value = ValueFactory::from($result)->create($scope);
return $main($value);
} catch (\Throwable $error) {
throw $error;
} finally {
// Always dispose both the scope and the container, even if a
// disposer fails; surface any disposer errors without masking $error.
$disposalErrors = [
...$scope->dispose($error),
...$container->dispose($error),
];
if ($disposalErrors !== []) {
throw new DisposalFailed($disposalErrors, $error);
}
}
}
/**
* Returns the service the module returns without disposing anything.
* Meant for tests and debugging modules; otherwise prefer {@see self::run()}.
*
* @template T
* @param Module<T|Ref<T>> $module
* @return T
*/
public static function build(Module $module): mixed
{
$builder = new Builder();
$result = new self($builder)->import($module);
// a scope is used to resolve a service of any lifetime: singleton or scoped
$scope = $builder->build()->startScope();
return ValueFactory::from($result)->create($scope);
}
private Autoconfiguration $autoconfiguration;
private Autowiring $autowiring;
private function __construct(
private Builder $builder,
) {
$this->autoconfiguration = new Autoconfiguration($builder);
$this->autowiring = new Autowiring();
}
/**
* Runs $module in a fresh isolated Dic scope and returns whatever it returns.
*
* @template T
* @param Module<T> $module
* @return T
*/
public function import(Module $module): mixed
{
$dic = new self($this->builder);
$result = $module->configure($dic);
$dic->autoconfiguration->start();
return $result;
}
/**
* Calls $configurator on this Dic and returns whatever it returns; its bindings stay visible in the current scope.
*
* @template T
* @param callable(self): T $configurator
* @return T
*/
public function apply(callable $configurator): mixed
{
return $configurator($this);
}
/**
* Declares $value as a service.
*
* @template T
* @param T|Ref<T> $value
* @return ValueConfig<T>
*/
public function value(mixed $value): ValueConfig
{
/** @var ValueConfig<T> */
return new ValueConfig(
builder: $this->builder,
autowiring: $this->autowiring,
value: $value,
declaredAt: caller(),
);
}
/**
* Declares a callable as a service; chain {@see FunctionConfig::closure()} to expose it as a typed \Closure.
*
* @param callable|array{Ref<object>, string}|Ref<callable> $function
* @return FunctionConfig<callable>
*/
public function function(callable|array|Ref $function): FunctionConfig
{
$declaredAt = caller();
/** @var FunctionConfig<callable> */
return new FunctionConfig(
builder: $this->builder,
autoconfiguration: $this->autoconfiguration,
autowiring: $this->autowiring,
/** @phpstan-ignore argument.type */
function: $this->toRef($function, $declaredAt),
declaredAt: $declaredAt,
);
}
/**
* Declares $class as an object service; constructor parameters are autowired unless a $factory is provided.
*
* @template T of object
* @param class-string<T> $class
* @param null|callable(): T|array{Ref<class-string|object>, string}|Ref<callable(): T> $factory
* @return ObjectConfig<T>
*/
public function object(string $class, null|callable|array|Ref $factory = null): ObjectConfig
{
$declaredAt = caller();
if (!($factory === null || $factory instanceof Ref)) {
/** @var Ref<callable(): T> */
$factory = $this->toRef($factory, $declaredAt);
}
return new ObjectConfig(
builder: $this->builder,
autoconfiguration: $this->autoconfiguration,
autowiring: $this->autowiring,
reflection: new \ReflectionClass($class),
factory: $factory,
declaredAt: $declaredAt,
);
}
/**
* Declares a Scoped<T> handle that opens a fresh scope, resolves $ref inside it, and disposes on exit.
*
* @template T
* @param T|Ref<T> $value
* @return ScopedConfig<T>
*/
public function scoped(mixed $value): ScopedConfig
{
$declaredAt = caller();
/** @var ValueConfig<T> $value */
return new ScopedConfig(
builder: $this->builder,
autowiring: $this->autowiring,
ref: $this->toRef($value, $declaredAt),
declaredAt: $declaredAt,
);
}
/**
* Declares a lazy \Closure(): T that resolves $value on each call without opening a scope.
*
* @template T
* @param T|Ref<T> $value
* @return ProviderConfig<T>
*/
public function provider(mixed $value): ProviderConfig
{
$declaredAt = caller();
/** @var Ref<T> $value */
return new ProviderConfig(
builder: $this->builder,
autowiring: $this->autowiring,
ref: $this->toRef($value, $declaredAt),
declaredAt: $declaredAt,
);
}
/**
* @template T
* @param T|Ref<T> $value
* @return Ref<T>
*/
private function toRef(mixed $value, Location $declaredAt): Ref
{
if ($value instanceof Ref) {
return $value;
}
/** @var ValueConfig<T> */
return new ValueConfig(
builder: $this->builder,
autowiring: $this->autowiring,
value: $value,
declaredAt: $declaredAt,
);
}
/**
* Declares a list of every service tagged with $tag, resolved once after all modules have run.
*
* @template T
* @template TTag of Tag<T>
* @param class-string<TTag>|TTag $tag
* @param ?callable(TaggedRef<T, TTag>, TaggedRef<T, TTag>): int $sort
* @return TaggedListConfig<T, TTag>
*/
public function taggedList(string|Tag $tag, ?callable $sort = null): TaggedListConfig
{
return new TaggedListConfig(
builder: $this->builder,
autowiring: $this->autowiring,
tag: $tag,
sort: $sort,
declaredAt: caller(),
);
}
/**
* Registers a listener called for every object() service declared on this Dic, before the container is built.
*
* @param callable(ObjectAutoconfig<object>): void $listener
*/
public function onObject(callable $listener): void
{
$this->autoconfiguration->onObject($listener);
}
/**
* Registers a listener called for every function() / method() service declared on this Dic, before the container
* is built.
*
* @param callable(FunctionAutoconfig): void $listener
*/
public function onFunction(callable $listener): void
{
$this->autoconfiguration->onFunction($listener);
}
/**
* Registers a listener called once after all modules run, receiving the complete set of tagged services.
*
* @param callable(TaggedRefs): void $listener
*/
public function onTagResolution(callable $listener): void
{
$this->builder->onTagResolution($listener);
}
}