Test Failed
Push — add-default-php-config ( 54c75f )
by Chema
12:10
created

GacelaConfig::defaultPhpConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\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> */
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 array<string,list<Closure>> */
46
    private array $servicesToExtend = [];
47
48
    /**
49
     * @param array<string,class-string|object|callable> $externalServices
50
     */
51
    public function __construct(array $externalServices = [])
52
    {
53
        $this->externalServices = $externalServices;
54
        $this->configBuilder = new ConfigBuilder();
55
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
56
        $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
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...
87
     */
88
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
89
    {
90
        $this->configBuilder->add($path, $pathLocal, $reader);
91
92
        return $this;
93
    }
94
95
    /**
96
     * Allow overriding gacela facade suffixes.
97
     */
98
    public function addSuffixTypeFacade(string $suffix): self
99
    {
100
        $this->suffixTypesBuilder->addFacade($suffix);
101
102
        return $this;
103
    }
104
105
    /**
106
     * Allow overriding gacela factory suffixes.
107
     */
108
    public function addSuffixTypeFactory(string $suffix): self
109
    {
110
        $this->suffixTypesBuilder->addFactory($suffix);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Allow overriding gacela config suffixes.
117
     */
118
    public function addSuffixTypeConfig(string $suffix): self
119
    {
120
        $this->suffixTypesBuilder->addConfig($suffix);
121
122
        return $this;
123
    }
124
125
    /**
126
     * Allow overriding gacela dependency provider suffixes.
127
     */
128
    public function addSuffixTypeDependencyProvider(string $suffix): self
129
    {
130
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
131
132
        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
    public function addMappingInterface(string $key, $value): self
142
    {
143
        $this->mappingInterfacesBuilder->bind($key, $value);
144
145
        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
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...
152
     */
153
    public function addExternalService(string $key, $value): self
154
    {
155
        $this->externalServices[$key] = $value;
156
157
        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
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...
164
     */
165
    public function getExternalService(string $key)
166
    {
167
        return $this->externalServices[$key];
168
    }
169
170
    /**
171
     * Enable resetting the memory cache on each setup. Useful for functional tests.
172
     */
173
    public function resetInMemoryCache(): self
174
    {
175
        $this->shouldResetInMemoryCache = true;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Define whether the file cache flag is enabled.
182
     */
183
    public function setFileCacheEnabled(bool $flag): self
184
    {
185
        $this->fileCacheEnabled = $flag;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Define the file cache directory.
192
     */
193
    public function setFileCacheDirectory(string $dir): self
194
    {
195
        $this->fileCacheDirectory = $dir;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Define a list of project namespaces.
202
     *
203
     * @param list<string> $list
204
     */
205
    public function setProjectNamespaces(array $list): self
206
    {
207
        $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...
208
209
        return $this;
210
    }
211
212
    /**
213
     * Add/replace an existent configuration key with a specific value.
214
     *
215
     * @param mixed $value
216
     */
217
    public function addAppConfigKeyValue(string $key, $value): self
218
    {
219
        $this->configKeyValues[$key] = $value;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Add/replace a list of existent configuration keys with a specific value.
226
     *
227
     * @param array<string, mixed> $config
228
     */
229
    public function addAppConfigKeyValues(array $config): self
230
    {
231
        $this->configKeyValues = array_merge($this->configKeyValues ?? [], $config);
232
233
        return $this;
234
    }
235
236
    /**
237
     * Do not dispatch any event in the application.
238
     */
239
    public function disableEventListeners(): self
240
    {
241
        $this->areEventListenersEnabled = false;
242
243
        return $this;
244
    }
245
246
    /**
247
     * Register a generic listener when any event happens.
248
     * The callable argument must be the type `GacelaEventInterface`.
249
     *
250
     * @param callable(GacelaEventInterface):void $listener
251
     */
252
    public function registerGenericListener(callable $listener): self
253
    {
254
        if ($this->genericListeners === null) {
255
            $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...
256
        }
257
        $this->genericListeners[] = $listener;
258
259
        return $this;
260
    }
261
262
    /**
263
     * Register a listener when some event happens.
264
     *
265
     * @param class-string $event
266
     * @param callable(GacelaEventInterface):void $listener
267
     */
268
    public function registerSpecificListener(string $event, callable $listener): self
269
    {
270
        if ($this->specificListeners === null) {
271
            $this->specificListeners = [];
272
        }
273
        $this->specificListeners[$event][] = $listener;
274
275
        return $this;
276
    }
277
278
    public function extendService(string $id, Closure $service): self
279
    {
280
        $this->servicesToExtend[$id] ??= [];
281
        $this->servicesToExtend[$id][] = $service;
282
283
        return $this;
284
    }
285
286
    /**
287
     * @return array{
288
     *     external-services: array<string,class-string|object|callable>,
289
     *     config-builder: ConfigBuilder,
290
     *     suffix-types-builder: SuffixTypesBuilder,
291
     *     mapping-interfaces-builder: MappingInterfacesBuilder,
292
     *     should-reset-in-memory-cache: ?bool,
293
     *     file-cache-enabled: ?bool,
294
     *     file-cache-directory: ?string,
295
     *     project-namespaces: ?list<string>,
296
     *     config-key-values: ?array<string,mixed>,
297
     *     are-event-listeners-enabled: ?bool,
298
     *     generic-listeners: ?list<callable>,
299
     *     specific-listeners: ?array<class-string,list<callable>>,
300
     *     services-to-extend: array<string,list<Closure>>,
301
     * }
302
     *
303
     * @internal
304
     */
305
    public function build(): array
306
    {
307
        return [
308
            'external-services' => $this->externalServices,
309
            'config-builder' => $this->configBuilder,
310
            'suffix-types-builder' => $this->suffixTypesBuilder,
311
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
312
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
313
            'file-cache-enabled' => $this->fileCacheEnabled,
314
            'file-cache-directory' => $this->fileCacheDirectory,
315
            'project-namespaces' => $this->projectNamespaces,
316
            'config-key-values' => $this->configKeyValues,
317
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
318
            'generic-listeners' => $this->genericListeners,
319
            'specific-listeners' => $this->specificListeners,
320
            'services-to-extend' => $this->servicesToExtend,
321
        ];
322
    }
323
}
324