Passed
Push — feature/remove-profiler ( 1b185f )
by Chema
04:28
created

GacelaConfig::setProfilerDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 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\ClassResolver\Cache\GacelaFileCache;
9
use Gacela\Framework\Config\ConfigReaderInterface;
10
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
12
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
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 = false;
26
27
    private bool $fileCacheEnabled = GacelaFileCache::DEFAULT_ENABLED_VALUE;
28
29
    private string $fileCacheDirectory = GacelaFileCache::DEFAULT_DIRECTORY_VALUE;
30
31
    /** @var list<string> */
32
    private array $projectNamespaces = [];
33
    /** @var array<string,mixed> */
34
    private array $configKeyValues = [];
35
36
    /**
37
     * @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...
38
     */
39 48
    public function __construct(array $externalServices = [])
40
    {
41 48
        $this->externalServices = $externalServices;
42 48
        $this->configBuilder = new ConfigBuilder();
43 48
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
44 48
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
45
    }
46
47
    /**
48
     * Define 'config/*.php' as path, and 'config/local.php' as local path for the configuration.
49
     *
50
     * @return Closure(GacelaConfig):void
51
     */
52 7
    public static function withPhpConfigDefault(): callable
53
    {
54 7
        return static function (self $config): void {
55 7
            $config->addAppConfig('config/*.php', 'config/local.php');
56
        };
57
    }
58
59
    /**
60
     * Define the path where the configuration will be stored.
61
     *
62
     * @param string $path define the path where Gacela will read all the config files
63
     * @param string $pathLocal define the path where Gacela will read the local config file
64
     * @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...
65
     */
66 23
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
67
    {
68 23
        $this->configBuilder->add($path, $pathLocal, $reader);
69
70 23
        return $this;
71
    }
72
73
    /**
74
     * Allow overriding gacela facade suffixes.
75
     */
76 5
    public function addSuffixTypeFacade(string $suffix): self
77
    {
78 5
        $this->suffixTypesBuilder->addFacade($suffix);
79
80 5
        return $this;
81
    }
82
83
    /**
84
     * Allow overriding gacela factory suffixes.
85
     */
86 5
    public function addSuffixTypeFactory(string $suffix): self
87
    {
88 5
        $this->suffixTypesBuilder->addFactory($suffix);
89
90 5
        return $this;
91
    }
92
93
    /**
94
     * Allow overriding gacela config suffixes.
95
     */
96 5
    public function addSuffixTypeConfig(string $suffix): self
97
    {
98 5
        $this->suffixTypesBuilder->addConfig($suffix);
99
100 5
        return $this;
101
    }
102
103
    /**
104
     * Allow overriding gacela dependency provider suffixes.
105
     */
106 6
    public function addSuffixTypeDependencyProvider(string $suffix): self
107
    {
108 6
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
109
110 6
        return $this;
111
    }
112
113
    /**
114
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
115
     *
116
     * @param class-string $key
117
     * @param class-string|object|callable $value
118
     */
119 9
    public function addMappingInterface(string $key, $value): self
120
    {
121 9
        $this->mappingInterfacesBuilder->bind($key, $value);
122
123 9
        return $this;
124
    }
125
126
    /**
127
     * Useful to pass services while bootstrapping Gacela to the gacela.php config file.
128
     *
129
     * @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...
130
     */
131 2
    public function addExternalService(string $key, $value): self
132
    {
133 2
        $this->externalServices[$key] = $value;
134
135 2
        return $this;
136
    }
137
138
    /**
139
     * Get an external service from its defined key, previously added using `addExternalService()`.
140
     *
141
     * @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...
142
     */
143 4
    public function getExternalService(string $key)
144
    {
145 4
        return $this->externalServices[$key];
146
    }
147
148
    /**
149
     * Enable resetting the memory cache on each setup. Useful for functional tests.
150
     */
151 8
    public function resetInMemoryCache(): self
152
    {
153 8
        $this->shouldResetInMemoryCache = true;
154
155 8
        return $this;
156
    }
157
158
    /**
159
     * Define whether the file cache flag is enabled.
160
     */
161 14
    public function setFileCacheEnabled(bool $flag): self
162
    {
163 14
        $this->fileCacheEnabled = $flag;
164
165 14
        return $this;
166
    }
167
168
    /**
169
     * Define the file cache directory.
170
     */
171 2
    public function setFileCacheDirectory(string $dir): self
172
    {
173 2
        $this->fileCacheDirectory = $dir;
174
175 2
        return $this;
176
    }
177
178
    /**
179
     * Define a list of project namespaces.
180
     *
181
     * @param list<string> $list
182
     */
183 3
    public function setProjectNamespaces(array $list): self
184
    {
185 3
        $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...
186
187 3
        return $this;
188
    }
189
190
    /**
191
     * Add/replace an existent configuration key with a specific value.
192
     *
193
     * @param mixed $value
194
     */
195 1
    public function addAppConfigKeyValue(string $key, $value): self
196
    {
197 1
        $this->configKeyValues[$key] = $value;
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * Add/replace a list of existent configuration keys with a specific value.
204
     *
205
     * @param array<string, mixed> $config
206
     */
207 1
    public function addAppConfigKeyValues(array $config): self
208
    {
209 1
        $this->configKeyValues = array_merge($this->configKeyValues, $config);
210
211 1
        return $this;
212
    }
213
214
    /**
215
     * @internal
216
     *
217
     * @return array{
218
     *     external-services: array<string,class-string|object|callable>,
219
     *     config-builder: ConfigBuilder,
220
     *     suffix-types-builder: SuffixTypesBuilder,
221
     *     mapping-interfaces-builder: MappingInterfacesBuilder,
222
     *     should-reset-in-memory-cache: bool,
223
     *     file-cache-enabled: bool,
224
     *     file-cache-directory: string,
225
     *     project-namespaces: list<string>,
226
     *     config-key-values: array<string,mixed>,
227
     * }
228
     */
229 47
    public function build(): array
230
    {
231
        return [
232 47
            'external-services' => $this->externalServices,
233 47
            'config-builder' => $this->configBuilder,
234 47
            'suffix-types-builder' => $this->suffixTypesBuilder,
235 47
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
236 47
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
237 47
            'file-cache-enabled' => $this->fileCacheEnabled,
238 47
            'file-cache-directory' => $this->fileCacheDirectory,
239 47
            'project-namespaces' => $this->projectNamespaces,
240 47
            'config-key-values' => $this->configKeyValues,
241
        ];
242
    }
243
}
244