Passed
Push — add-extend-config ( f39ef4 )
by Chema
03:24
created

GacelaConfig::addExtendConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<string, class-string|object|callable> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string|object|callable>.
Loading history...
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>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string,list<callable>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,list<callable>>.
Loading history...
43
    private ?array $specificListeners = null;
44
45
    /** @var list<class-string>  */
46
    private ?array $extendConfig = 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
     * @param array<string,class-string|object|callable> $externalServices
56
     */
57 79
    public function __construct(array $externalServices = [])
58
    {
59 79
        $this->externalServices = $externalServices;
60 79
        $this->configBuilder = new ConfigBuilder();
61 79
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
62 79
        $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
     * @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ConfigReade...figReaderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ConfigReaderInterface>|ConfigReaderInterface|null.
Loading history...
93
     */
94 19
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
95
    {
96 19
        $this->configBuilder->add($path, $pathLocal, $reader);
97
98 19
        return $this;
99
    }
100
101
    /**
102
     * Allow overriding gacela facade suffixes.
103
     */
104 5
    public function addSuffixTypeFacade(string $suffix): self
105
    {
106 5
        $this->suffixTypesBuilder->addFacade($suffix);
107
108 5
        return $this;
109
    }
110
111
    /**
112
     * Allow overriding gacela factory suffixes.
113
     */
114 5
    public function addSuffixTypeFactory(string $suffix): self
115
    {
116 5
        $this->suffixTypesBuilder->addFactory($suffix);
117
118 5
        return $this;
119
    }
120
121
    /**
122
     * Allow overriding gacela config suffixes.
123
     */
124 5
    public function addSuffixTypeConfig(string $suffix): self
125
    {
126 5
        $this->suffixTypesBuilder->addConfig($suffix);
127
128 5
        return $this;
129
    }
130
131
    /**
132
     * Allow overriding gacela dependency provider suffixes.
133
     */
134 6
    public function addSuffixTypeDependencyProvider(string $suffix): self
135
    {
136 6
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
137
138 6
        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
     * @param class-string|object|callable $value
147
     */
148 1
    public function addMappingInterface(string $key, string|object|callable $value): self
149
    {
150 1
        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
     * @param class-string|object|callable $value
158
     */
159 10
    public function addBinding(string $key, string|object|callable $value): self
160
    {
161 10
        $this->bindingBuilder->bind($key, $value);
162
163 10
        return $this;
164
    }
165
166
    /**
167
     * Useful to pass services while bootstrapping Gacela to the gacela.php config file.
168
     *
169
     * @param class-string|object|callable $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
170
     */
171 3
    public function addExternalService(string $key, $value): self
172
    {
173 3
        $this->externalServices[$key] = $value;
174
175 3
        return $this;
176
    }
177
178
    /**
179
     * Get an external service from its defined key, previously added using `addExternalService()`.
180
     *
181
     * @return class-string|object|callable
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
182
     */
183 4
    public function getExternalService(string $key)
184
    {
185 4
        return $this->externalServices[$key];
186
    }
187
188
    /**
189
     * Enable resetting the memory cache on each setup. Useful for functional tests.
190
     */
191 47
    public function resetInMemoryCache(): self
192
    {
193 47
        $this->shouldResetInMemoryCache = true;
194
195 47
        return $this;
196
    }
197
198
    /**
199
     * Define whether the file cache flag is enabled,
200
     * and the file cache directory.
201
     */
202 16
    public function setFileCache(bool $enabled, string $dir = null): self
203
    {
204 16
        $this->fileCacheEnabled = $enabled;
205 16
        $this->fileCacheDirectory = $dir;
206
207 16
        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
     * @param list<string> $list
240
     */
241 4
    public function setProjectNamespaces(array $list): self
242
    {
243 4
        $this->projectNamespaces = $list;
0 ignored issues
show
Documentation Bug introduced by
It seems like $list of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $projectNamespaces.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
244
245 4
        return $this;
246
    }
247
248
    /**
249
     * Add/replace an existent configuration key with a specific value.
250
     *
251
     * @param mixed $value
252
     */
253 8
    public function addAppConfigKeyValue(string $key, $value): self
254
    {
255 8
        $this->configKeyValues[$key] = $value;
256
257 8
        return $this;
258
    }
259
260
    /**
261
     * Add/replace a list of existent configuration keys with a specific value.
262
     *
263
     * @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 2
        return $this;
270
    }
271
272
    /**
273
     * Do not dispatch any event in the application.
274
     */
275 1
    public function disableEventListeners(): self
276
    {
277 1
        $this->areEventListenersEnabled = false;
278
279 1
        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
     * @param callable(GacelaEventInterface):void $listener
287
     */
288 8
    public function registerGenericListener(callable $listener): self
289
    {
290 8
        if ($this->genericListeners === null) {
291 8
            $this->genericListeners = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $genericListeners.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
292
        }
293 8
        $this->genericListeners[] = $listener;
294
295 8
        return $this;
296
    }
297
298
    /**
299
     * Register a listener when some event happens.
300
     *
301
     * @param class-string $event
302
     * @param callable(GacelaEventInterface):void $listener
303
     */
304 7
    public function registerSpecificListener(string $event, callable $listener): self
305
    {
306 7
        if ($this->specificListeners === null) {
307 7
            $this->specificListeners = [];
308
        }
309 7
        $this->specificListeners[$event][] = $listener;
310
311 7
        return $this;
312
    }
313
314 3
    public function extendService(string $id, Closure $service): self
315
    {
316 3
        $this->servicesToExtend[$id] ??= [];
317 3
        $this->servicesToExtend[$id][] = $service;
318
319 3
        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
     * @param class-string $config
334
     */
335 1
    public function extendConfig(string $config): self
336
    {
337 1
        $this->extendConfig[] = $config;
338
339 1
        return $this;
340
    }
341
342
    /**
343
     * @param list<class-string> $list
344
     */
345
    public function addExtendConfig(array $list): self
346
    {
347
        $this->extendConfig = array_merge($this->extendConfig ?? [], $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->exten...nfig ?? array(), $list) of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $extendConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
348
349
        return $this;
350
    }
351
352
    /**
353
     * @param class-string $plugin
354
     */
355 3
    public function addAfterPlugin(string $plugin): self
356
    {
357 3
        $this->afterPlugins[] = $plugin;
358
359 3
        return $this;
360
    }
361
362
    /**
363
     * @param list<class-string> $list
364
     */
365 1
    public function addAfterPlugins(array $list): self
366
    {
367 1
        $this->afterPlugins = array_merge($this->afterPlugins ?? [], $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->after...gins ?? array(), $list) of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $afterPlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
368
369 1
        return $this;
370
    }
371
372
    /**
373
     * @return array{
374
     *     external-services: array<string,class-string|object|callable>,
375
     *     config-builder: ConfigBuilder,
376
     *     suffix-types-builder: SuffixTypesBuilder,
377
     *     mapping-interfaces-builder: BindingsBuilder,
378
     *     should-reset-in-memory-cache: ?bool,
379
     *     file-cache-enabled: ?bool,
380
     *     file-cache-directory: ?string,
381
     *     project-namespaces: ?list<string>,
382
     *     config-key-values: ?array<string,mixed>,
383
     *     are-event-listeners-enabled: ?bool,
384
     *     generic-listeners: ?list<callable>,
385
     *     specific-listeners: ?array<class-string,list<callable>>,
386
     *     before-config: ?list<class-string>,
387
     *     after-plugins: ?list<class-string>,
388
     *     services-to-extend: array<string,list<Closure>>,
389
     * }
390
     *
391
     * @internal
392
     */
393 78
    public function build(): array
394
    {
395 78
        return [
396 78
            'external-services' => $this->externalServices,
397 78
            'config-builder' => $this->configBuilder,
398 78
            'suffix-types-builder' => $this->suffixTypesBuilder,
399 78
            'mapping-interfaces-builder' => $this->bindingBuilder,
400 78
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
401 78
            'file-cache-enabled' => $this->fileCacheEnabled,
402 78
            'file-cache-directory' => $this->fileCacheDirectory,
403 78
            'project-namespaces' => $this->projectNamespaces,
404 78
            'config-key-values' => $this->configKeyValues,
405 78
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
406 78
            'generic-listeners' => $this->genericListeners,
407 78
            'specific-listeners' => $this->specificListeners,
408 78
            'before-config' => $this->extendConfig,
409 78
            'after-plugins' => $this->afterPlugins,
410 78
            'services-to-extend' => $this->servicesToExtend,
411 78
        ];
412
    }
413
}
414