Passed
Push — feature/container-extend ( b81097...93c762 )
by Chema
03:26
created

GacelaConfig::extendService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
ccs 4
cts 4
cp 1
crap 1
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>> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<string,list<Closure>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
46
    private array $servicesToExtend = [];
47
48
    /**
49
     * @param array<string,class-string|object|callable> $externalServices
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...
50
     */
51 72
    public function __construct(array $externalServices = [])
52
    {
53 72
        $this->externalServices = $externalServices;
54 72
        $this->configBuilder = new ConfigBuilder();
55 72
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
56 72
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
57
    }
58
59
    /**
60
     * Define 'config/*.php' as path, and 'config/local.php' as local path for the configuration.
61
     *
62
     * @return Closure(GacelaConfig):void
63
     */
64 4
    public static function withPhpConfigDefault(): callable
65
    {
66 4
        return static function (self $config): void {
67 4
            $config->addAppConfig('config/*.php', 'config/local.php');
68
        };
69
    }
70
71
    /**
72
     * Define the path where the configuration will be stored.
73
     *
74
     * @param string $path define the path where Gacela will read all the config files
75
     * @param string $pathLocal define the path where Gacela will read the local config file
76
     * @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...
77
     */
78 25
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
79
    {
80 25
        $this->configBuilder->add($path, $pathLocal, $reader);
81
82 25
        return $this;
83
    }
84
85
    /**
86
     * Allow overriding gacela facade suffixes.
87
     */
88 5
    public function addSuffixTypeFacade(string $suffix): self
89
    {
90 5
        $this->suffixTypesBuilder->addFacade($suffix);
91
92 5
        return $this;
93
    }
94
95
    /**
96
     * Allow overriding gacela factory suffixes.
97
     */
98 5
    public function addSuffixTypeFactory(string $suffix): self
99
    {
100 5
        $this->suffixTypesBuilder->addFactory($suffix);
101
102 5
        return $this;
103
    }
104
105
    /**
106
     * Allow overriding gacela config suffixes.
107
     */
108 5
    public function addSuffixTypeConfig(string $suffix): self
109
    {
110 5
        $this->suffixTypesBuilder->addConfig($suffix);
111
112 5
        return $this;
113
    }
114
115
    /**
116
     * Allow overriding gacela dependency provider suffixes.
117
     */
118 6
    public function addSuffixTypeDependencyProvider(string $suffix): self
119
    {
120 6
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
121
122 6
        return $this;
123
    }
124
125
    /**
126
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
127
     *
128
     * @param class-string $key
129
     * @param class-string|object|callable $value
130
     */
131 9
    public function addMappingInterface(string $key, $value): self
132
    {
133 9
        $this->mappingInterfacesBuilder->bind($key, $value);
134
135 9
        return $this;
136
    }
137
138
    /**
139
     * Useful to pass services while bootstrapping Gacela to the gacela.php config file.
140
     *
141
     * @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...
142
     */
143 3
    public function addExternalService(string $key, $value): self
144
    {
145 3
        $this->externalServices[$key] = $value;
146
147 3
        return $this;
148
    }
149
150
    /**
151
     * Get an external service from its defined key, previously added using `addExternalService()`.
152
     *
153
     * @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...
154
     */
155 4
    public function getExternalService(string $key)
156
    {
157 4
        return $this->externalServices[$key];
158
    }
159
160
    /**
161
     * Enable resetting the memory cache on each setup. Useful for functional tests.
162
     */
163 29
    public function resetInMemoryCache(): self
164
    {
165 29
        $this->shouldResetInMemoryCache = true;
166
167 29
        return $this;
168
    }
169
170
    /**
171
     * Define whether the file cache flag is enabled.
172
     */
173 16
    public function setFileCacheEnabled(bool $flag): self
174
    {
175 16
        $this->fileCacheEnabled = $flag;
176
177 16
        return $this;
178
    }
179
180
    /**
181
     * Define the file cache directory.
182
     */
183 4
    public function setFileCacheDirectory(string $dir): self
184
    {
185 4
        $this->fileCacheDirectory = $dir;
186
187 4
        return $this;
188
    }
189
190
    /**
191
     * Define a list of project namespaces.
192
     *
193
     * @param list<string> $list
194
     */
195 4
    public function setProjectNamespaces(array $list): self
196
    {
197 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...
198
199 4
        return $this;
200
    }
201
202
    /**
203
     * Add/replace an existent configuration key with a specific value.
204
     *
205
     * @param mixed $value
206
     */
207 2
    public function addAppConfigKeyValue(string $key, $value): self
208
    {
209 2
        $this->configKeyValues[$key] = $value;
210
211 2
        return $this;
212
    }
213
214
    /**
215
     * Add/replace a list of existent configuration keys with a specific value.
216
     *
217
     * @param array<string, mixed> $config
218
     */
219 2
    public function addAppConfigKeyValues(array $config): self
220
    {
221 2
        $this->configKeyValues = array_merge($this->configKeyValues ?? [], $config);
222
223 2
        return $this;
224
    }
225
226
    /**
227
     * Do not dispatch any event in the application.
228
     */
229
    public function disableEventListeners(): self
230
    {
231
        $this->areEventListenersEnabled = false;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Register a generic listener when any event happens.
238
     * The callable argument must be the type `GacelaEventInterface`.
239
     *
240
     * @param callable(GacelaEventInterface):void $listener
241
     */
242 7
    public function registerGenericListener(callable $listener): self
243
    {
244 7
        if ($this->genericListeners === null) {
245 7
            $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...
246
        }
247 7
        $this->genericListeners[] = $listener;
248
249 7
        return $this;
250
    }
251
252
    /**
253
     * Register a listener when some event happens.
254
     *
255
     * @param class-string $event
256
     * @param callable(GacelaEventInterface):void $listener
257
     */
258 7
    public function registerSpecificListener(string $event, callable $listener): self
259
    {
260 7
        if ($this->specificListeners === null) {
261 7
            $this->specificListeners = [];
262
        }
263 7
        $this->specificListeners[$event][] = $listener;
264
265 7
        return $this;
266
    }
267
268 2
    public function extendService(string $id, Closure $service): self
269
    {
270 2
        $this->servicesToExtend[$id] ??= [];
271 2
        $this->servicesToExtend[$id][] = $service;
272
273 2
        return $this;
274
    }
275
276
    /**
277
     * @return array{
278
     *     external-services: array<string,class-string|object|callable>,
279
     *     config-builder: ConfigBuilder,
280
     *     suffix-types-builder: SuffixTypesBuilder,
281
     *     mapping-interfaces-builder: MappingInterfacesBuilder,
282
     *     should-reset-in-memory-cache: ?bool,
283
     *     file-cache-enabled: ?bool,
284
     *     file-cache-directory: ?string,
285
     *     project-namespaces: ?list<string>,
286
     *     config-key-values: ?array<string,mixed>,
287
     *     are-event-listeners-enabled: ?bool,
288
     *     generic-listeners: ?list<callable>,
289
     *     specific-listeners: ?array<class-string,list<callable>>,
290
     *     services-to-extend: array<string,list<Closure>>,
291
     * }
292
     *
293
     * @internal
294
     */
295 71
    public function build(): array
296
    {
297
        return [
298 71
            'external-services' => $this->externalServices,
299 71
            'config-builder' => $this->configBuilder,
300 71
            'suffix-types-builder' => $this->suffixTypesBuilder,
301 71
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
302 71
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
303 71
            'file-cache-enabled' => $this->fileCacheEnabled,
304 71
            'file-cache-directory' => $this->fileCacheDirectory,
305 71
            'project-namespaces' => $this->projectNamespaces,
306 71
            'config-key-values' => $this->configKeyValues,
307 71
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
308 71
            'generic-listeners' => $this->genericListeners,
309 71
            'specific-listeners' => $this->specificListeners,
310 71
            'services-to-extend' => $this->servicesToExtend,
311
        ];
312
    }
313
}
314