Passed
Push — rename-add-extend-config ( 82b093 )
by Chema
03:31
created

GacelaConfig::extendGacelaConfigs()   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\AppConfigBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\BindingsBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
12
use Gacela\Framework\Event\GacelaEventInterface;
13
14
final class GacelaConfig
15
{
16
    private AppConfigBuilder $appConfigBuilder;
17
18
    private SuffixTypesBuilder $suffixTypesBuilder;
19
20
    private BindingsBuilder $bindingsBuilder;
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>> */
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 $gacelaConfigsToExtend = null;
47
48
    /** @var list<class-string|callable>  */
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 81
    public function __construct(array $externalServices = [])
58
    {
59 81
        $this->externalServices = $externalServices;
60 81
        $this->appConfigBuilder = new AppConfigBuilder();
61 81
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
62 81
        $this->bindingsBuilder = 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->appConfigBuilder->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->bindingsBuilder->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 48
    public function resetInMemoryCache(): self
192
    {
193 48
        $this->shouldResetInMemoryCache = true;
194
195 48
        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
        $this->genericListeners ??= [];
265 8
        $this->genericListeners[] = $listener;
266
267 8
        return $this;
268
    }
269
270
    /**
271
     * Register a listener when some event happens.
272
     *
273
     * @param class-string $event
274
     * @param callable(GacelaEventInterface):void $listener
275
     */
276 7
    public function registerSpecificListener(string $event, callable $listener): self
277
    {
278 7
        $this->specificListeners[$event] ??= [];
279 7
        $this->specificListeners[$event][] = $listener;
280
281 7
        return $this;
282
    }
283
284 3
    public function extendService(string $id, Closure $service): self
285
    {
286 3
        $this->servicesToExtend[$id] ??= [];
287 3
        $this->servicesToExtend[$id][] = $service;
288
289 3
        return $this;
290
    }
291
292
    /**
293
     * Add a new invokable class that can extend the GacelaConfig object.
294
     *
295
     * This configClass will receive the GacelaConfig object as argument to the __invoke() method.
296
     * ```
297
     * __invoke(GacelaConfig $config): void
298
     * ```
299
     *
300
     * @param class-string $className
301
     */
302 1
    public function extendGacelaConfig(string $className): self
303
    {
304 1
        $this->gacelaConfigsToExtend[] = $className;
305
306 1
        return $this;
307
    }
308
309
    /**
310
     * @param list<class-string> $list
311
     */
312
    public function extendGacelaConfigs(array $list): self
313
    {
314
        $this->gacelaConfigsToExtend = array_merge($this->gacelaConfigsToExtend ?? [], $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->gacel...tend ?? array(), $list) of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $gacelaConfigsToExtend.

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...
315
316
        return $this;
317
    }
318
319
    /**
320
     * @param class-string|callable $plugin
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|callable.
Loading history...
321
     */
322 4
    public function addPlugin(string|callable $plugin): self
323
    {
324 4
        $this->plugins[] = $plugin;
325
326 4
        return $this;
327
    }
328
329
    /**
330
     * @param list<class-string|callable> $list
331
     */
332 1
    public function addPlugins(array $list): self
333
    {
334 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...
335
336 1
        return $this;
337
    }
338
339
    /**
340
     * @return array{
341
     *     external-services: array<string,class-string|object|callable>,
342
     *     app-config-builder: AppConfigBuilder,
343
     *     suffix-types-builder: SuffixTypesBuilder,
344
     *     bindings-builder: BindingsBuilder,
345
     *     should-reset-in-memory-cache: ?bool,
346
     *     file-cache-enabled: ?bool,
347
     *     file-cache-directory: ?string,
348
     *     project-namespaces: ?list<string>,
349
     *     config-key-values: ?array<string,mixed>,
350
     *     are-event-listeners-enabled: ?bool,
351
     *     generic-listeners: ?list<callable>,
352
     *     specific-listeners: ?array<class-string,list<callable>>,
353
     *     gacela-configs-to-extend: ?list<class-string>,
354
     *     plugins: ?list<class-string|callable>,
355
     *     instances-to-extend: array<string,list<Closure>>,
356
     * }
357
     *
358
     * @internal
359
     */
360 80
    public function build(): array
361
    {
362 80
        return [
363 80
            'external-services' => $this->externalServices,
364 80
            'app-config-builder' => $this->appConfigBuilder,
365 80
            'suffix-types-builder' => $this->suffixTypesBuilder,
366 80
            'bindings-builder' => $this->bindingsBuilder,
367 80
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
368 80
            'file-cache-enabled' => $this->fileCacheEnabled,
369 80
            'file-cache-directory' => $this->fileCacheDirectory,
370 80
            'project-namespaces' => $this->projectNamespaces,
371 80
            'config-key-values' => $this->configKeyValues,
372 80
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
373 80
            'generic-listeners' => $this->genericListeners,
374 80
            'specific-listeners' => $this->specificListeners,
375 80
            'gacela-configs-to-extend' => $this->gacelaConfigsToExtend,
376 80
            'plugins' => $this->plugins,
377 80
            'instances-to-extend' => $this->servicesToExtend,
378 80
        ];
379
    }
380
}
381