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