Passed
Push — remove-deprecated-file-cache-m... ( e4db37 )
by Jesús
03:42
created

GacelaConfig   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 361
Duplicated Lines 0 %

Test Coverage

Coverage 90.82%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 87
c 6
b 0
f 0
dl 0
loc 361
ccs 89
cts 98
cp 0.9082
rs 10
wmc 28

26 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultPhpConfig() 0 4 1
A __construct() 0 6 1
A setFileCache() 0 6 1
A getExternalService() 0 3 1
A addAppConfig() 0 5 1
A addExternalService() 0 5 1
A addSuffixTypeFacade() 0 5 1
A addMappingInterface() 0 3 1
A addSuffixTypeFactory() 0 5 1
A withPhpConfigDefault() 0 3 1
A resetInMemoryCache() 0 5 1
A addSuffixTypeConfig() 0 5 1
A addSuffixTypeDependencyProvider() 0 5 1
A addBinding() 0 5 1
A addAppConfigKeyValue() 0 5 1
A addExtendConfigs() 0 5 1
A build() 0 18 1
A setProjectNamespaces() 0 5 1
A addPlugins() 0 5 1
A registerGenericListener() 0 8 2
A addAppConfigKeyValues() 0 5 1
A extendService() 0 6 1
A disableEventListeners() 0 5 1
A addPlugin() 0 5 1
A addExtendConfig() 0 5 1
A registerSpecificListener() 0 8 2
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 $plugins = 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 a list of project namespaces.
212
     *
213
     * @param list<string> $list
214
     */
215 4
    public function setProjectNamespaces(array $list): self
216
    {
217 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...
218
219 4
        return $this;
220
    }
221
222
    /**
223
     * Add/replace an existent configuration key with a specific value.
224
     *
225
     * @param mixed $value
226
     */
227 8
    public function addAppConfigKeyValue(string $key, $value): self
228
    {
229 8
        $this->configKeyValues[$key] = $value;
230
231 8
        return $this;
232
    }
233
234
    /**
235
     * Add/replace a list of existent configuration keys with a specific value.
236
     *
237
     * @param array<string, mixed> $config
238
     */
239 2
    public function addAppConfigKeyValues(array $config): self
240
    {
241 2
        $this->configKeyValues = array_merge($this->configKeyValues ?? [], $config);
242
243 2
        return $this;
244
    }
245
246
    /**
247
     * Do not dispatch any event in the application.
248
     */
249 1
    public function disableEventListeners(): self
250
    {
251 1
        $this->areEventListenersEnabled = false;
252
253 1
        return $this;
254
    }
255
256
    /**
257
     * Register a generic listener when any event happens.
258
     * The callable argument must be the type `GacelaEventInterface`.
259
     *
260
     * @param callable(GacelaEventInterface):void $listener
261
     */
262 8
    public function registerGenericListener(callable $listener): self
263
    {
264 8
        if ($this->genericListeners === null) {
265 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...
266
        }
267 8
        $this->genericListeners[] = $listener;
268
269 8
        return $this;
270
    }
271
272
    /**
273
     * Register a listener when some event happens.
274
     *
275
     * @param class-string $event
276
     * @param callable(GacelaEventInterface):void $listener
277
     */
278 7
    public function registerSpecificListener(string $event, callable $listener): self
279
    {
280 7
        if ($this->specificListeners === null) {
281 7
            $this->specificListeners = [];
282
        }
283 7
        $this->specificListeners[$event][] = $listener;
284
285 7
        return $this;
286
    }
287
288 3
    public function extendService(string $id, Closure $service): self
289
    {
290 3
        $this->servicesToExtend[$id] ??= [];
291 3
        $this->servicesToExtend[$id][] = $service;
292
293 3
        return $this;
294
    }
295
296
    /**
297
     * @param class-string $config
298
     */
299 1
    public function addExtendConfig(string $config): self
300
    {
301 1
        $this->extendConfig[] = $config;
302
303 1
        return $this;
304
    }
305
306
    /**
307
     * @param list<class-string> $list
308
     */
309
    public function addExtendConfigs(array $list): self
310
    {
311
        $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...
312
313
        return $this;
314
    }
315
316
    /**
317
     * @param class-string $plugin
318
     */
319 3
    public function addPlugin(string $plugin): self
320
    {
321 3
        $this->plugins[] = $plugin;
322
323 3
        return $this;
324
    }
325
326
    /**
327
     * @param list<class-string> $list
328
     */
329 1
    public function addPlugins(array $list): self
330
    {
331 1
        $this->plugins = array_merge($this->plugins ?? [], $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->plugins ?? array(), $list) of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $plugins.

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...
332
333 1
        return $this;
334
    }
335
336
    /**
337
     * @return array{
338
     *     external-services: array<string,class-string|object|callable>,
339
     *     config-builder: ConfigBuilder,
340
     *     suffix-types-builder: SuffixTypesBuilder,
341
     *     mapping-interfaces-builder: BindingsBuilder,
342
     *     should-reset-in-memory-cache: ?bool,
343
     *     file-cache-enabled: ?bool,
344
     *     file-cache-directory: ?string,
345
     *     project-namespaces: ?list<string>,
346
     *     config-key-values: ?array<string,mixed>,
347
     *     are-event-listeners-enabled: ?bool,
348
     *     generic-listeners: ?list<callable>,
349
     *     specific-listeners: ?array<class-string,list<callable>>,
350
     *     before-config: ?list<class-string>,
351
     *     after-plugins: ?list<class-string>,
352
     *     services-to-extend: array<string,list<Closure>>,
353
     * }
354
     *
355
     * @internal
356
     */
357 78
    public function build(): array
358
    {
359 78
        return [
360 78
            'external-services' => $this->externalServices,
361 78
            'config-builder' => $this->configBuilder,
362 78
            'suffix-types-builder' => $this->suffixTypesBuilder,
363 78
            'mapping-interfaces-builder' => $this->bindingBuilder,
364 78
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
365 78
            'file-cache-enabled' => $this->fileCacheEnabled,
366 78
            'file-cache-directory' => $this->fileCacheDirectory,
367 78
            'project-namespaces' => $this->projectNamespaces,
368 78
            'config-key-values' => $this->configKeyValues,
369 78
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
370 78
            'generic-listeners' => $this->genericListeners,
371 78
            'specific-listeners' => $this->specificListeners,
372 78
            'before-config' => $this->extendConfig,
373 78
            'after-plugins' => $this->plugins,
374 78
            'services-to-extend' => $this->servicesToExtend,
375 78
        ];
376
    }
377
}
378