1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Bootstrap; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Gacela\Framework\Config\ConfigReaderInterface; |
9
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\BindingsBuilder; |
10
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder; |
11
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
12
|
|
|
use Gacela\Framework\Event\GacelaEventInterface; |
13
|
|
|
|
14
|
|
|
final class GacelaConfig |
15
|
|
|
{ |
16
|
|
|
private ConfigBuilder $configBuilder; |
17
|
|
|
|
18
|
|
|
private SuffixTypesBuilder $suffixTypesBuilder; |
19
|
|
|
|
20
|
|
|
private BindingsBuilder $bindingBuilder; |
21
|
|
|
|
22
|
|
|
/** @var array<string, class-string|object|callable> */ |
|
|
|
|
23
|
|
|
private array $externalServices; |
24
|
|
|
|
25
|
|
|
private ?bool $shouldResetInMemoryCache = null; |
26
|
|
|
|
27
|
|
|
private ?bool $fileCacheEnabled = null; |
28
|
|
|
|
29
|
|
|
private ?string $fileCacheDirectory = null; |
30
|
|
|
|
31
|
|
|
/** @var list<string> */ |
32
|
|
|
private ?array $projectNamespaces = null; |
33
|
|
|
|
34
|
|
|
/** @var array<string,mixed> */ |
35
|
|
|
private ?array $configKeyValues = null; |
36
|
|
|
|
37
|
|
|
private ?bool $areEventListenersEnabled = null; |
38
|
|
|
|
39
|
|
|
/** @var list<callable> */ |
40
|
|
|
private ?array $genericListeners = null; |
41
|
|
|
|
42
|
|
|
/** @var array<class-string,list<callable>> */ |
|
|
|
|
43
|
|
|
private ?array $specificListeners = null; |
44
|
|
|
|
45
|
|
|
/** @var list<class-string> */ |
46
|
|
|
private ?array $beforePlugins = null; |
47
|
|
|
|
48
|
|
|
/** @var list<class-string> */ |
49
|
|
|
private ?array $afterPlugins = null; |
50
|
|
|
|
51
|
|
|
/** @var array<string,list<Closure>> */ |
52
|
|
|
private array $servicesToExtend = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
78 |
|
* @param array<string,class-string|object|callable> $externalServices |
56
|
|
|
*/ |
57
|
78 |
|
public function __construct(array $externalServices = []) |
58
|
78 |
|
{ |
59
|
78 |
|
$this->externalServices = $externalServices; |
60
|
78 |
|
$this->configBuilder = new ConfigBuilder(); |
61
|
|
|
$this->suffixTypesBuilder = new SuffixTypesBuilder(); |
62
|
|
|
$this->bindingBuilder = new BindingsBuilder(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @deprecated use `defaultPhpConfig()` instead |
67
|
|
|
* |
68
|
|
|
* @return Closure(GacelaConfig):void |
69
|
|
|
*/ |
70
|
|
|
public static function withPhpConfigDefault(): callable |
71
|
|
|
{ |
72
|
|
|
return self::defaultPhpConfig(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Define 'config/*.php' as path, and 'config/local.php' as local path for the configuration. |
77
|
|
|
* |
78
|
|
|
* @return Closure(GacelaConfig):void |
79
|
|
|
*/ |
80
|
|
|
public static function defaultPhpConfig(): callable |
81
|
|
|
{ |
82
|
|
|
return static function (self $config): void { |
83
|
|
|
$config->addAppConfig('config/*.php', 'config/local.php'); |
84
|
|
|
}; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Define the path where the configuration will be stored. |
89
|
|
|
* |
90
|
|
|
* @param string $path define the path where Gacela will read all the config files |
91
|
|
|
* @param string $pathLocal define the path where Gacela will read the local config file |
92
|
19 |
|
* @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files |
|
|
|
|
93
|
|
|
*/ |
94
|
19 |
|
public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self |
95
|
|
|
{ |
96
|
19 |
|
$this->configBuilder->add($path, $pathLocal, $reader); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
5 |
|
* Allow overriding gacela facade suffixes. |
103
|
|
|
*/ |
104
|
5 |
|
public function addSuffixTypeFacade(string $suffix): self |
105
|
|
|
{ |
106
|
5 |
|
$this->suffixTypesBuilder->addFacade($suffix); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
5 |
|
* Allow overriding gacela factory suffixes. |
113
|
|
|
*/ |
114
|
5 |
|
public function addSuffixTypeFactory(string $suffix): self |
115
|
|
|
{ |
116
|
5 |
|
$this->suffixTypesBuilder->addFactory($suffix); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
5 |
|
* Allow overriding gacela config suffixes. |
123
|
|
|
*/ |
124
|
5 |
|
public function addSuffixTypeConfig(string $suffix): self |
125
|
|
|
{ |
126
|
5 |
|
$this->suffixTypesBuilder->addConfig($suffix); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
6 |
|
* Allow overriding gacela dependency provider suffixes. |
133
|
|
|
*/ |
134
|
6 |
|
public function addSuffixTypeDependencyProvider(string $suffix): self |
135
|
|
|
{ |
136
|
6 |
|
$this->suffixTypesBuilder->addDependencyProvider($suffix); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @deprecated in favor of `$this->addBinding(key, value)` |
143
|
|
|
* It will be removed in the next release |
144
|
|
|
* |
145
|
|
|
* @param class-string $key |
146
|
1 |
|
* @param class-string|object|callable $value |
147
|
|
|
*/ |
148
|
1 |
|
public function addMappingInterface(string $key, string|object|callable $value): self |
149
|
|
|
{ |
150
|
|
|
return $this->addBinding($key, $value); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Bind a key class or interface name to be resolved by Gacela automatically. |
155
|
|
|
* |
156
|
|
|
* @param class-string $key |
157
|
9 |
|
* @param class-string|object|callable $value |
158
|
|
|
*/ |
159
|
9 |
|
public function addBinding(string $key, string|object|callable $value): self |
160
|
|
|
{ |
161
|
9 |
|
$this->bindingBuilder->bind($key, $value); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Useful to pass services while bootstrapping Gacela to the gacela.php config file. |
168
|
|
|
* |
169
|
3 |
|
* @param class-string|object|callable $value |
|
|
|
|
170
|
|
|
*/ |
171
|
3 |
|
public function addExternalService(string $key, $value): self |
172
|
|
|
{ |
173
|
3 |
|
$this->externalServices[$key] = $value; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get an external service from its defined key, previously added using `addExternalService()`. |
180
|
|
|
* |
181
|
4 |
|
* @return class-string|object|callable |
|
|
|
|
182
|
|
|
*/ |
183
|
4 |
|
public function getExternalService(string $key) |
184
|
|
|
{ |
185
|
|
|
return $this->externalServices[$key]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
46 |
|
* Enable resetting the memory cache on each setup. Useful for functional tests. |
190
|
|
|
*/ |
191
|
46 |
|
public function resetInMemoryCache(): self |
192
|
|
|
{ |
193
|
46 |
|
$this->shouldResetInMemoryCache = true; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Define whether the file cache flag is enabled, |
200
|
16 |
|
* and the file cache directory. |
201
|
|
|
*/ |
202
|
16 |
|
public function setFileCache(bool $enabled, string $dir = null): self |
203
|
16 |
|
{ |
204
|
|
|
$this->fileCacheEnabled = $enabled; |
205
|
16 |
|
$this->fileCacheDirectory = $dir; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Define whether the file cache flag is enabled. |
212
|
|
|
* |
213
|
|
|
* @deprecated in favor of setFileCache() |
214
|
|
|
* It will be removed in the next release |
215
|
|
|
*/ |
216
|
|
|
public function setFileCacheEnabled(bool $flag): self |
217
|
|
|
{ |
218
|
|
|
$this->fileCacheEnabled = $flag; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Define the file cache directory. |
225
|
|
|
* |
226
|
|
|
* @deprecated in favor of setFileCache() |
227
|
|
|
* It will be removed in the next release |
228
|
|
|
*/ |
229
|
|
|
public function setFileCacheDirectory(string $dir): self |
230
|
|
|
{ |
231
|
|
|
$this->fileCacheDirectory = $dir; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Define a list of project namespaces. |
238
|
|
|
* |
239
|
4 |
|
* @param list<string> $list |
240
|
|
|
*/ |
241
|
4 |
|
public function setProjectNamespaces(array $list): self |
242
|
|
|
{ |
243
|
4 |
|
$this->projectNamespaces = $list; |
|
|
|
|
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Add/replace an existent configuration key with a specific value. |
250
|
|
|
* |
251
|
8 |
|
* @param mixed $value |
252
|
|
|
*/ |
253
|
8 |
|
public function addAppConfigKeyValue(string $key, $value): self |
254
|
|
|
{ |
255
|
8 |
|
$this->configKeyValues[$key] = $value; |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Add/replace a list of existent configuration keys with a specific value. |
262
|
|
|
* |
263
|
2 |
|
* @param array<string, mixed> $config |
264
|
|
|
*/ |
265
|
2 |
|
public function addAppConfigKeyValues(array $config): self |
266
|
|
|
{ |
267
|
2 |
|
$this->configKeyValues = array_merge($this->configKeyValues ?? [], $config); |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
1 |
|
* Do not dispatch any event in the application. |
274
|
|
|
*/ |
275
|
1 |
|
public function disableEventListeners(): self |
276
|
|
|
{ |
277
|
1 |
|
$this->areEventListenersEnabled = false; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Register a generic listener when any event happens. |
284
|
|
|
* The callable argument must be the type `GacelaEventInterface`. |
285
|
|
|
* |
286
|
8 |
|
* @param callable(GacelaEventInterface):void $listener |
287
|
|
|
*/ |
288
|
8 |
|
public function registerGenericListener(callable $listener): self |
289
|
8 |
|
{ |
290
|
|
|
if ($this->genericListeners === null) { |
291
|
8 |
|
$this->genericListeners = []; |
|
|
|
|
292
|
|
|
} |
293
|
8 |
|
$this->genericListeners[] = $listener; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Register a listener when some event happens. |
300
|
|
|
* |
301
|
|
|
* @param class-string $event |
302
|
7 |
|
* @param callable(GacelaEventInterface):void $listener |
303
|
|
|
*/ |
304
|
7 |
|
public function registerSpecificListener(string $event, callable $listener): self |
305
|
7 |
|
{ |
306
|
|
|
if ($this->specificListeners === null) { |
307
|
7 |
|
$this->specificListeners = []; |
308
|
|
|
} |
309
|
7 |
|
$this->specificListeners[$event][] = $listener; |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
3 |
|
} |
313
|
|
|
|
314
|
3 |
|
public function extendService(string $id, Closure $service): self |
315
|
3 |
|
{ |
316
|
|
|
$this->servicesToExtend[$id] ??= []; |
317
|
3 |
|
$this->servicesToExtend[$id][] = $service; |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
/** |
322
|
|
|
* @deprecated in favor of `addAfterPlugin()` |
323
|
|
|
* It will be removed in the next release |
324
|
|
|
* |
325
|
|
|
* @param class-string $plugin |
326
|
|
|
*/ |
327
|
|
|
public function addPlugin(string $plugin): self |
328
|
|
|
{ |
329
|
|
|
return $this->addAfterPlugin($plugin); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
3 |
|
* @param class-string $plugin |
334
|
|
|
*/ |
335
|
3 |
|
public function addBeforePlugin(string $plugin): self |
336
|
|
|
{ |
337
|
3 |
|
$this->beforePlugins[] = $plugin; |
338
|
|
|
|
339
|
|
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
1 |
|
* @param list<class-string> $list |
344
|
|
|
*/ |
345
|
1 |
|
public function addBeforePlugins(array $list): self |
346
|
|
|
{ |
347
|
1 |
|
$this->beforePlugins = array_merge($this->beforePlugins ?? [], $list); |
|
|
|
|
348
|
|
|
|
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param class-string $plugin |
354
|
|
|
*/ |
355
|
|
|
public function addAfterPlugin(string $plugin): self |
356
|
|
|
{ |
357
|
|
|
$this->afterPlugins[] = $plugin; |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param list<class-string> $list |
364
|
|
|
*/ |
365
|
|
|
public function addAfterPlugins(array $list): self |
366
|
|
|
{ |
367
|
|
|
$this->afterPlugins = array_merge($this->afterPlugins ?? [], $list); |
|
|
|
|
368
|
|
|
|
369
|
|
|
return $this; |
370
|
77 |
|
} |
371
|
|
|
|
372
|
77 |
|
/** |
373
|
77 |
|
* @return array{ |
374
|
77 |
|
* external-services: array<string,class-string|object|callable>, |
375
|
77 |
|
* config-builder: ConfigBuilder, |
376
|
77 |
|
* suffix-types-builder: SuffixTypesBuilder, |
377
|
77 |
|
* mapping-interfaces-builder: BindingsBuilder, |
378
|
77 |
|
* should-reset-in-memory-cache: ?bool, |
379
|
77 |
|
* file-cache-enabled: ?bool, |
380
|
77 |
|
* file-cache-directory: ?string, |
381
|
77 |
|
* project-namespaces: ?list<string>, |
382
|
77 |
|
* config-key-values: ?array<string,mixed>, |
383
|
77 |
|
* are-event-listeners-enabled: ?bool, |
384
|
77 |
|
* generic-listeners: ?list<callable>, |
385
|
77 |
|
* specific-listeners: ?array<class-string,list<callable>>, |
386
|
77 |
|
* before-plugins: ?list<class-string>, |
387
|
77 |
|
* after-plugins: ?list<class-string>, |
388
|
|
|
* services-to-extend: array<string,list<Closure>>, |
389
|
|
|
* } |
390
|
|
|
* |
391
|
|
|
* @internal |
392
|
|
|
*/ |
393
|
|
|
public function build(): array |
394
|
|
|
{ |
395
|
|
|
return [ |
396
|
|
|
'external-services' => $this->externalServices, |
397
|
|
|
'config-builder' => $this->configBuilder, |
398
|
|
|
'suffix-types-builder' => $this->suffixTypesBuilder, |
399
|
|
|
'mapping-interfaces-builder' => $this->bindingBuilder, |
400
|
|
|
'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache, |
401
|
|
|
'file-cache-enabled' => $this->fileCacheEnabled, |
402
|
|
|
'file-cache-directory' => $this->fileCacheDirectory, |
403
|
|
|
'project-namespaces' => $this->projectNamespaces, |
404
|
|
|
'config-key-values' => $this->configKeyValues, |
405
|
|
|
'are-event-listeners-enabled' => $this->areEventListenersEnabled, |
406
|
|
|
'generic-listeners' => $this->genericListeners, |
407
|
|
|
'specific-listeners' => $this->specificListeners, |
408
|
|
|
'before-plugins' => $this->beforePlugins, |
409
|
|
|
'after-plugins' => $this->afterPlugins, |
410
|
|
|
'services-to-extend' => $this->servicesToExtend, |
411
|
|
|
]; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|