Passed
Push — add-enableFileCache ( 55cb5f )
by Chema
03:35
created

GacelaConfig::enableFileCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
     * @return Closure(GacelaConfig):void
67
     *
68
     * @deprecated use `defaultPhpConfig()` instead
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
     * @param class-string $key
143
     * @param class-string|object|callable $value
144
     *
145
     * @deprecated in favor of `$this->addBinding(key, value)`
146
     * It will be removed in the next release
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
     * Shortcut to setFileCache(true)
200
     */
201 1
    public function enableFileCache(string $dir = null): self
202
    {
203 1
        return $this->setFileCache(true, $dir);
204
    }
205
206
    /**
207
     * Define whether the file cache flag is enabled,
208
     * and the file cache directory.
209
     */
210 16
    public function setFileCache(bool $enabled, string $dir = null): self
211
    {
212 16
        $this->fileCacheEnabled = $enabled;
213 16
        $this->fileCacheDirectory = $dir;
214
215 16
        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;
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...
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
        $this->genericListeners ??= [];
273 8
        $this->genericListeners[] = $listener;
274
275 8
        return $this;
276
    }
277
278
    /**
279
     * Register a listener when some event happens.
280
     *
281
     * @param class-string $event
282
     * @param callable(GacelaEventInterface):void $listener
283
     */
284 7
    public function registerSpecificListener(string $event, callable $listener): self
285
    {
286 7
        $this->specificListeners[$event] ??= [];
287 7
        $this->specificListeners[$event][] = $listener;
288
289 7
        return $this;
290
    }
291
292 3
    public function extendService(string $id, Closure $service): self
293
    {
294 3
        $this->servicesToExtend[$id] ??= [];
295 3
        $this->servicesToExtend[$id][] = $service;
296
297 3
        return $this;
298
    }
299
300
    /**
301
     * Add a new invokable class that can extend the GacelaConfig object.
302
     *
303
     * This configClass will receive the GacelaConfig object as argument to the __invoke() method.
304
     * ```
305
     * __invoke(GacelaConfig $config): void
306
     * ```
307
     *
308
     * @param class-string $className
309
     */
310 1
    public function extendGacelaConfig(string $className): self
311
    {
312 1
        $this->gacelaConfigsToExtend[] = $className;
313
314 1
        return $this;
315
    }
316
317
    /**
318
     * @param list<class-string> $list
319
     */
320
    public function extendGacelaConfigs(array $list): self
321
    {
322
        $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...
323
324
        return $this;
325
    }
326
327
    /**
328
     * @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...
329
     */
330 4
    public function addPlugin(string|callable $plugin): self
331
    {
332 4
        $this->plugins[] = $plugin;
333
334 4
        return $this;
335
    }
336
337
    /**
338
     * @param list<class-string|callable> $list
339
     */
340 1
    public function addPlugins(array $list): self
341
    {
342 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...
343
344 1
        return $this;
345
    }
346
347
    /**
348
     * @return array{
349
     *     external-services: array<string,class-string|object|callable>,
350
     *     app-config-builder: AppConfigBuilder,
351
     *     suffix-types-builder: SuffixTypesBuilder,
352
     *     bindings-builder: BindingsBuilder,
353
     *     should-reset-in-memory-cache: ?bool,
354
     *     file-cache-enabled: ?bool,
355
     *     file-cache-directory: ?string,
356
     *     project-namespaces: ?list<string>,
357
     *     config-key-values: ?array<string,mixed>,
358
     *     are-event-listeners-enabled: ?bool,
359
     *     generic-listeners: ?list<callable>,
360
     *     specific-listeners: ?array<class-string,list<callable>>,
361
     *     gacela-configs-to-extend: ?list<class-string>,
362
     *     plugins: ?list<class-string|callable>,
363
     *     instances-to-extend: array<string,list<Closure>>,
364
     * }
365
     *
366
     * @internal
367
     */
368 80
    public function build(): array
369
    {
370 80
        return [
371 80
            'external-services' => $this->externalServices,
372 80
            'app-config-builder' => $this->appConfigBuilder,
373 80
            'suffix-types-builder' => $this->suffixTypesBuilder,
374 80
            'bindings-builder' => $this->bindingsBuilder,
375 80
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
376 80
            'file-cache-enabled' => $this->fileCacheEnabled,
377 80
            'file-cache-directory' => $this->fileCacheDirectory,
378 80
            'project-namespaces' => $this->projectNamespaces,
379 80
            'config-key-values' => $this->configKeyValues,
380 80
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
381 80
            'generic-listeners' => $this->genericListeners,
382 80
            'specific-listeners' => $this->specificListeners,
383 80
            'gacela-configs-to-extend' => $this->gacelaConfigsToExtend,
384 80
            'plugins' => $this->plugins,
385 80
            'instances-to-extend' => $this->servicesToExtend,
386 80
        ];
387
    }
388
}
389