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