|
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\ConfigBuilder; |
|
10
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder; |
|
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 MappingInterfacesBuilder $mappingInterfacesBuilder; |
|
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 $plugins = 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
|
75 |
|
public function __construct(array $externalServices = []) |
|
56
|
|
|
{ |
|
57
|
75 |
|
$this->externalServices = $externalServices; |
|
58
|
75 |
|
$this->configBuilder = new ConfigBuilder(); |
|
59
|
75 |
|
$this->suffixTypesBuilder = new SuffixTypesBuilder(); |
|
60
|
75 |
|
$this->mappingInterfacesBuilder = new MappingInterfacesBuilder(); |
|
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
|
|
|
* Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically. |
|
141
|
|
|
* |
|
142
|
|
|
* @param class-string $key |
|
143
|
|
|
* @param class-string|object|callable $value |
|
144
|
|
|
*/ |
|
145
|
9 |
|
public function addMappingInterface(string $key, $value): self |
|
146
|
|
|
{ |
|
147
|
9 |
|
$this->mappingInterfacesBuilder->bind($key, $value); |
|
148
|
|
|
|
|
149
|
9 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Useful to pass services while bootstrapping Gacela to the gacela.php config file. |
|
154
|
|
|
* |
|
155
|
|
|
* @param class-string|object|callable $value |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
3 |
|
public function addExternalService(string $key, $value): self |
|
158
|
|
|
{ |
|
159
|
3 |
|
$this->externalServices[$key] = $value; |
|
160
|
|
|
|
|
161
|
3 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get an external service from its defined key, previously added using `addExternalService()`. |
|
166
|
|
|
* |
|
167
|
|
|
* @return class-string|object|callable |
|
|
|
|
|
|
168
|
|
|
*/ |
|
169
|
4 |
|
public function getExternalService(string $key) |
|
170
|
|
|
{ |
|
171
|
4 |
|
return $this->externalServices[$key]; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Enable resetting the memory cache on each setup. Useful for functional tests. |
|
176
|
|
|
*/ |
|
177
|
46 |
|
public function resetInMemoryCache(): self |
|
178
|
|
|
{ |
|
179
|
46 |
|
$this->shouldResetInMemoryCache = true; |
|
180
|
|
|
|
|
181
|
46 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Define whether the file cache flag is enabled, |
|
186
|
|
|
* and the file cache directory. |
|
187
|
|
|
*/ |
|
188
|
16 |
|
public function setFileCache(bool $enabled, string $dir = null): self |
|
189
|
|
|
{ |
|
190
|
16 |
|
$this->fileCacheEnabled = $enabled; |
|
191
|
16 |
|
$this->fileCacheDirectory = $dir; |
|
192
|
|
|
|
|
193
|
16 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Define whether the file cache flag is enabled. |
|
198
|
|
|
* |
|
199
|
|
|
* @deprecated in favor of setFileCache() |
|
200
|
|
|
* It will be removed in the next release |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setFileCacheEnabled(bool $flag): self |
|
203
|
|
|
{ |
|
204
|
|
|
$this->fileCacheEnabled = $flag; |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Define the file cache directory. |
|
211
|
|
|
* |
|
212
|
|
|
* @deprecated in favor of setFileCache() |
|
213
|
|
|
* It will be removed in the next release |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setFileCacheDirectory(string $dir): self |
|
216
|
|
|
{ |
|
217
|
|
|
$this->fileCacheDirectory = $dir; |
|
218
|
|
|
|
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Define a list of project namespaces. |
|
224
|
|
|
* |
|
225
|
|
|
* @param list<string> $list |
|
226
|
|
|
*/ |
|
227
|
4 |
|
public function setProjectNamespaces(array $list): self |
|
228
|
|
|
{ |
|
229
|
4 |
|
$this->projectNamespaces = $list; |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
4 |
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Add/replace an existent configuration key with a specific value. |
|
236
|
|
|
* |
|
237
|
|
|
* @param mixed $value |
|
238
|
|
|
*/ |
|
239
|
8 |
|
public function addAppConfigKeyValue(string $key, $value): self |
|
240
|
|
|
{ |
|
241
|
8 |
|
$this->configKeyValues[$key] = $value; |
|
242
|
|
|
|
|
243
|
8 |
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Add/replace a list of existent configuration keys with a specific value. |
|
248
|
|
|
* |
|
249
|
|
|
* @param array<string, mixed> $config |
|
250
|
|
|
*/ |
|
251
|
2 |
|
public function addAppConfigKeyValues(array $config): self |
|
252
|
|
|
{ |
|
253
|
2 |
|
$this->configKeyValues = array_merge($this->configKeyValues ?? [], $config); |
|
254
|
|
|
|
|
255
|
2 |
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Do not dispatch any event in the application. |
|
260
|
|
|
*/ |
|
261
|
1 |
|
public function disableEventListeners(): self |
|
262
|
|
|
{ |
|
263
|
1 |
|
$this->areEventListenersEnabled = false; |
|
264
|
|
|
|
|
265
|
1 |
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Register a generic listener when any event happens. |
|
270
|
|
|
* The callable argument must be the type `GacelaEventInterface`. |
|
271
|
|
|
* |
|
272
|
|
|
* @param callable(GacelaEventInterface):void $listener |
|
273
|
|
|
*/ |
|
274
|
8 |
|
public function registerGenericListener(callable $listener): self |
|
275
|
|
|
{ |
|
276
|
8 |
|
if ($this->genericListeners === null) { |
|
277
|
8 |
|
$this->genericListeners = []; |
|
|
|
|
|
|
278
|
|
|
} |
|
279
|
8 |
|
$this->genericListeners[] = $listener; |
|
280
|
|
|
|
|
281
|
8 |
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Register a listener when some event happens. |
|
286
|
|
|
* |
|
287
|
|
|
* @param class-string $event |
|
288
|
|
|
* @param callable(GacelaEventInterface):void $listener |
|
289
|
|
|
*/ |
|
290
|
7 |
|
public function registerSpecificListener(string $event, callable $listener): self |
|
291
|
|
|
{ |
|
292
|
7 |
|
if ($this->specificListeners === null) { |
|
293
|
7 |
|
$this->specificListeners = []; |
|
294
|
|
|
} |
|
295
|
7 |
|
$this->specificListeners[$event][] = $listener; |
|
296
|
|
|
|
|
297
|
7 |
|
return $this; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
3 |
|
public function extendService(string $id, Closure $service): self |
|
301
|
|
|
{ |
|
302
|
3 |
|
$this->servicesToExtend[$id] ??= []; |
|
303
|
3 |
|
$this->servicesToExtend[$id][] = $service; |
|
304
|
|
|
|
|
305
|
3 |
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @param class-string<PluginInterface> $plugin |
|
|
|
|
|
|
310
|
|
|
*/ |
|
311
|
1 |
|
public function addPlugin(string $plugin): self |
|
312
|
|
|
{ |
|
313
|
1 |
|
$this->plugins[] = $plugin; |
|
314
|
|
|
|
|
315
|
1 |
|
return $this; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @return array{ |
|
320
|
|
|
* external-services: array<string,class-string|object|callable>, |
|
321
|
|
|
* config-builder: ConfigBuilder, |
|
322
|
|
|
* suffix-types-builder: SuffixTypesBuilder, |
|
323
|
|
|
* mapping-interfaces-builder: MappingInterfacesBuilder, |
|
324
|
|
|
* should-reset-in-memory-cache: ?bool, |
|
325
|
|
|
* file-cache-enabled: ?bool, |
|
326
|
|
|
* file-cache-directory: ?string, |
|
327
|
|
|
* project-namespaces: ?list<string>, |
|
328
|
|
|
* config-key-values: ?array<string,mixed>, |
|
329
|
|
|
* are-event-listeners-enabled: ?bool, |
|
330
|
|
|
* generic-listeners: ?list<callable>, |
|
331
|
|
|
* specific-listeners: ?array<class-string,list<callable>>, |
|
332
|
|
|
* plugins: ?list<class-string<PluginInterface>>, |
|
333
|
|
|
* services-to-extend: array<string,list<Closure>>, |
|
334
|
|
|
* } |
|
335
|
|
|
* |
|
336
|
|
|
* @internal |
|
337
|
|
|
*/ |
|
338
|
74 |
|
public function build(): array |
|
339
|
|
|
{ |
|
340
|
74 |
|
return [ |
|
341
|
74 |
|
'external-services' => $this->externalServices, |
|
342
|
74 |
|
'config-builder' => $this->configBuilder, |
|
343
|
74 |
|
'suffix-types-builder' => $this->suffixTypesBuilder, |
|
344
|
74 |
|
'mapping-interfaces-builder' => $this->mappingInterfacesBuilder, |
|
345
|
74 |
|
'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache, |
|
346
|
74 |
|
'file-cache-enabled' => $this->fileCacheEnabled, |
|
347
|
74 |
|
'file-cache-directory' => $this->fileCacheDirectory, |
|
348
|
74 |
|
'project-namespaces' => $this->projectNamespaces, |
|
349
|
74 |
|
'config-key-values' => $this->configKeyValues, |
|
350
|
74 |
|
'are-event-listeners-enabled' => $this->areEventListenersEnabled, |
|
351
|
74 |
|
'generic-listeners' => $this->genericListeners, |
|
352
|
74 |
|
'specific-listeners' => $this->specificListeners, |
|
353
|
74 |
|
'plugins' => $this->plugins, |
|
354
|
74 |
|
'services-to-extend' => $this->servicesToExtend, |
|
355
|
74 |
|
]; |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|