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