Passed
Push — rename-mapping-to-bindings ( 2a1c78...2a17a9 )
by Chema
03:01
created

GacelaConfig::addMappingInterface()   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 2
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\BindingsBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
12
use Gacela\Framework\Event\GacelaEventInterface;
13
use Gacela\Framework\Plugin\PluginInterface;
14
15
final class GacelaConfig
16
{
17
    private ConfigBuilder $configBuilder;
18
19
    private SuffixTypesBuilder $suffixTypesBuilder;
20
21
    private BindingsBuilder $bindingBuilder;
22
23
    /** @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...
24
    private array $externalServices;
25
26
    private ?bool $shouldResetInMemoryCache = null;
27
28
    private ?bool $fileCacheEnabled = null;
29
30
    private ?string $fileCacheDirectory = null;
31
32
    /** @var list<string> */
33
    private ?array $projectNamespaces = null;
34
35
    /** @var array<string,mixed> */
36
    private ?array $configKeyValues = null;
37
38
    private ?bool $areEventListenersEnabled = null;
39
40
    /** @var list<callable> */
41
    private ?array $genericListeners = null;
42
43
    /** @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...
44
    private ?array $specificListeners = null;
45
46
    /** @var list<class-string<PluginInterface>>  */
47
    private ?array $plugins = null;
48
49
    /** @var array<string,list<Closure>> */
50
    private array $servicesToExtend = [];
51
52
    /**
53
     * @param array<string,class-string|object|callable> $externalServices
54
     */
55 78
    public function __construct(array $externalServices = [])
56
    {
57 78
        $this->externalServices = $externalServices;
58 78
        $this->configBuilder = new ConfigBuilder();
59 78
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
60 78
        $this->bindingBuilder = new BindingsBuilder();
61
    }
62
63
    /**
64
     * @deprecated use `defaultPhpConfig()` instead
65
     *
66
     * @return Closure(GacelaConfig):void
67
     */
68
    public static function withPhpConfigDefault(): callable
69
    {
70
        return self::defaultPhpConfig();
71
    }
72
73
    /**
74
     * Define 'config/*.php' as path, and 'config/local.php' as local path for the configuration.
75
     *
76
     * @return Closure(GacelaConfig):void
77
     */
78
    public static function defaultPhpConfig(): callable
79
    {
80
        return static function (self $config): void {
81
            $config->addAppConfig('config/*.php', 'config/local.php');
82
        };
83
    }
84
85
    /**
86
     * Define the path where the configuration will be stored.
87
     *
88
     * @param string $path define the path where Gacela will read all the config files
89
     * @param string $pathLocal define the path where Gacela will read the local config file
90
     * @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...
91
     */
92 19
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
93
    {
94 19
        $this->configBuilder->add($path, $pathLocal, $reader);
95
96 19
        return $this;
97
    }
98
99
    /**
100
     * Allow overriding gacela facade suffixes.
101
     */
102 5
    public function addSuffixTypeFacade(string $suffix): self
103
    {
104 5
        $this->suffixTypesBuilder->addFacade($suffix);
105
106 5
        return $this;
107
    }
108
109
    /**
110
     * Allow overriding gacela factory suffixes.
111
     */
112 5
    public function addSuffixTypeFactory(string $suffix): self
113
    {
114 5
        $this->suffixTypesBuilder->addFactory($suffix);
115
116 5
        return $this;
117
    }
118
119
    /**
120
     * Allow overriding gacela config suffixes.
121
     */
122 5
    public function addSuffixTypeConfig(string $suffix): self
123
    {
124 5
        $this->suffixTypesBuilder->addConfig($suffix);
125
126 5
        return $this;
127
    }
128
129
    /**
130
     * Allow overriding gacela dependency provider suffixes.
131
     */
132 6
    public function addSuffixTypeDependencyProvider(string $suffix): self
133
    {
134 6
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
135
136 6
        return $this;
137
    }
138
139
    /**
140
     * @deprecated in favor of `$this->addBinding()`
141
     * It will be removed in the next release
142
     *
143
     * @param class-string $key
144
     * @param class-string|object|callable $value
145
     */
146 1
    public function addMappingInterface(string $key, string|object|callable $value): self
147
    {
148 1
        return $this->addBinding($key, $value);
149
    }
150
151
    /**
152
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
153
     *
154
     * @param class-string $key
155
     * @param class-string|object|callable $value
156
     */
157 9
    public function addBinding(string $key, string|object|callable $value): self
158
    {
159 9
        $this->bindingBuilder->bind($key, $value);
160
161 9
        return $this;
162
    }
163
164
    /**
165
     * Useful to pass services while bootstrapping Gacela to the gacela.php config file.
166
     *
167
     * @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...
168
     */
169 3
    public function addExternalService(string $key, $value): self
170
    {
171 3
        $this->externalServices[$key] = $value;
172
173 3
        return $this;
174
    }
175
176
    /**
177
     * Get an external service from its defined key, previously added using `addExternalService()`.
178
     *
179
     * @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...
180
     */
181 4
    public function getExternalService(string $key)
182
    {
183 4
        return $this->externalServices[$key];
184
    }
185
186
    /**
187
     * Enable resetting the memory cache on each setup. Useful for functional tests.
188
     */
189 46
    public function resetInMemoryCache(): self
190
    {
191 46
        $this->shouldResetInMemoryCache = true;
192
193 46
        return $this;
194
    }
195
196
    /**
197
     * Define whether the file cache flag is enabled,
198
     * and the file cache directory.
199
     */
200 16
    public function setFileCache(bool $enabled, string $dir = null): self
201
    {
202 16
        $this->fileCacheEnabled = $enabled;
203 16
        $this->fileCacheDirectory = $dir;
204
205 16
        return $this;
206
    }
207
208
    /**
209
     * Define whether the file cache flag is enabled.
210
     *
211
     * @deprecated in favor of setFileCache()
212
     * It will be removed in the next release
213
     */
214
    public function setFileCacheEnabled(bool $flag): self
215
    {
216
        $this->fileCacheEnabled = $flag;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Define the file cache directory.
223
     *
224
     * @deprecated in favor of setFileCache()
225
     * It will be removed in the next release
226
     */
227
    public function setFileCacheDirectory(string $dir): self
228
    {
229
        $this->fileCacheDirectory = $dir;
230
231
        return $this;
232
    }
233
234
    /**
235
     * Define a list of project namespaces.
236
     *
237
     * @param list<string> $list
238
     */
239 4
    public function setProjectNamespaces(array $list): self
240
    {
241 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...
242
243 4
        return $this;
244
    }
245
246
    /**
247
     * Add/replace an existent configuration key with a specific value.
248
     *
249
     * @param mixed $value
250
     */
251 8
    public function addAppConfigKeyValue(string $key, $value): self
252
    {
253 8
        $this->configKeyValues[$key] = $value;
254
255 8
        return $this;
256
    }
257
258
    /**
259
     * Add/replace a list of existent configuration keys with a specific value.
260
     *
261
     * @param array<string, mixed> $config
262
     */
263 2
    public function addAppConfigKeyValues(array $config): self
264
    {
265 2
        $this->configKeyValues = array_merge($this->configKeyValues ?? [], $config);
266
267 2
        return $this;
268
    }
269
270
    /**
271
     * Do not dispatch any event in the application.
272
     */
273 1
    public function disableEventListeners(): self
274
    {
275 1
        $this->areEventListenersEnabled = false;
276
277 1
        return $this;
278
    }
279
280
    /**
281
     * Register a generic listener when any event happens.
282
     * The callable argument must be the type `GacelaEventInterface`.
283
     *
284
     * @param callable(GacelaEventInterface):void $listener
285
     */
286 8
    public function registerGenericListener(callable $listener): self
287
    {
288 8
        if ($this->genericListeners === null) {
289 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...
290
        }
291 8
        $this->genericListeners[] = $listener;
292
293 8
        return $this;
294
    }
295
296
    /**
297
     * Register a listener when some event happens.
298
     *
299
     * @param class-string $event
300
     * @param callable(GacelaEventInterface):void $listener
301
     */
302 7
    public function registerSpecificListener(string $event, callable $listener): self
303
    {
304 7
        if ($this->specificListeners === null) {
305 7
            $this->specificListeners = [];
306
        }
307 7
        $this->specificListeners[$event][] = $listener;
308
309 7
        return $this;
310
    }
311
312 3
    public function extendService(string $id, Closure $service): self
313
    {
314 3
        $this->servicesToExtend[$id] ??= [];
315 3
        $this->servicesToExtend[$id][] = $service;
316
317 3
        return $this;
318
    }
319
320
    /**
321
     * @param class-string<PluginInterface> $plugin
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<PluginInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<PluginInterface>.
Loading history...
322
     */
323 3
    public function addPlugin(string $plugin): self
324
    {
325 3
        $this->plugins[] = $plugin;
326
327 3
        return $this;
328
    }
329
330
    /**
331
     * @param list<class-string<PluginInterface>> $list
332
     */
333 1
    public function addPlugins(array $list): self
334
    {
335 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...
336
337 1
        return $this;
338
    }
339
340
    /**
341
     * @return array{
342
     *     external-services: array<string,class-string|object|callable>,
343
     *     config-builder: ConfigBuilder,
344
     *     suffix-types-builder: SuffixTypesBuilder,
345
     *     mapping-interfaces-builder: BindingsBuilder,
346
     *     should-reset-in-memory-cache: ?bool,
347
     *     file-cache-enabled: ?bool,
348
     *     file-cache-directory: ?string,
349
     *     project-namespaces: ?list<string>,
350
     *     config-key-values: ?array<string,mixed>,
351
     *     are-event-listeners-enabled: ?bool,
352
     *     generic-listeners: ?list<callable>,
353
     *     specific-listeners: ?array<class-string,list<callable>>,
354
     *     plugins: ?list<class-string<PluginInterface>>,
355
     *     services-to-extend: array<string,list<Closure>>,
356
     * }
357
     *
358
     * @internal
359
     */
360 77
    public function build(): array
361
    {
362 77
        return [
363 77
            'external-services' => $this->externalServices,
364 77
            'config-builder' => $this->configBuilder,
365 77
            'suffix-types-builder' => $this->suffixTypesBuilder,
366 77
            'mapping-interfaces-builder' => $this->bindingBuilder,
367 77
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
368 77
            'file-cache-enabled' => $this->fileCacheEnabled,
369 77
            'file-cache-directory' => $this->fileCacheDirectory,
370 77
            'project-namespaces' => $this->projectNamespaces,
371 77
            'config-key-values' => $this->configKeyValues,
372 77
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
373 77
            'generic-listeners' => $this->genericListeners,
374 77
            'specific-listeners' => $this->specificListeners,
375 77
            'plugins' => $this->plugins,
376 77
            'services-to-extend' => $this->servicesToExtend,
377 77
        ];
378
    }
379
}
380