|
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\ClassResolver\Profiler\GacelaProfiler; |
|
10
|
|
|
use Gacela\Framework\Config\ConfigReaderInterface; |
|
11
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder; |
|
12
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder; |
|
13
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
|
14
|
|
|
|
|
15
|
|
|
final class GacelaConfig |
|
16
|
|
|
{ |
|
17
|
|
|
private ConfigBuilder $configBuilder; |
|
18
|
|
|
|
|
19
|
|
|
private SuffixTypesBuilder $suffixTypesBuilder; |
|
20
|
|
|
|
|
21
|
|
|
private MappingInterfacesBuilder $mappingInterfacesBuilder; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<string, class-string|object|callable> */ |
|
|
|
|
|
|
24
|
|
|
private array $externalServices; |
|
25
|
|
|
|
|
26
|
|
|
private bool $shouldResetInMemoryCache = false; |
|
27
|
|
|
|
|
28
|
|
|
private bool $fileCacheEnabled = GacelaFileCache::DEFAULT_ENABLED_VALUE; |
|
29
|
|
|
|
|
30
|
|
|
private string $fileCacheDirectory = GacelaFileCache::DEFAULT_DIRECTORY_VALUE; |
|
31
|
|
|
|
|
32
|
|
|
private bool $profilerEnabled = GacelaProfiler::DEFAULT_ENABLED_VALUE; |
|
33
|
|
|
|
|
34
|
|
|
private string $profilerDirectory = GacelaProfiler::DEFAULT_DIRECTORY_VALUE; |
|
35
|
|
|
|
|
36
|
|
|
/** @var list<string> */ |
|
37
|
|
|
private array $projectNamespaces = []; |
|
38
|
|
|
/** @var array<string,mixed> */ |
|
39
|
|
|
private array $configKeyValues = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array<string,class-string|object|callable> $externalServices |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
50 |
|
public function __construct(array $externalServices = []) |
|
45
|
|
|
{ |
|
46
|
50 |
|
$this->externalServices = $externalServices; |
|
47
|
50 |
|
$this->configBuilder = new ConfigBuilder(); |
|
48
|
50 |
|
$this->suffixTypesBuilder = new SuffixTypesBuilder(); |
|
49
|
50 |
|
$this->mappingInterfacesBuilder = new MappingInterfacesBuilder(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return Closure(GacelaConfig):void |
|
54
|
|
|
*/ |
|
55
|
7 |
|
public static function withPhpConfigDefault(): callable |
|
56
|
|
|
{ |
|
57
|
7 |
|
return static function (self $config): void { |
|
58
|
7 |
|
$config->addAppConfig('config/*.php', 'config/local.php'); |
|
59
|
|
|
}; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $path define the path where Gacela will read all the config files |
|
64
|
|
|
* @param string $pathLocal define the path where Gacela will read the local config file |
|
65
|
|
|
* @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
23 |
|
public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self |
|
68
|
|
|
{ |
|
69
|
23 |
|
$this->configBuilder->add($path, $pathLocal, $reader); |
|
70
|
|
|
|
|
71
|
23 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
public function addSuffixTypeFacade(string $suffix): self |
|
75
|
|
|
{ |
|
76
|
5 |
|
$this->suffixTypesBuilder->addFacade($suffix); |
|
77
|
|
|
|
|
78
|
5 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
public function addSuffixTypeFactory(string $suffix): self |
|
82
|
|
|
{ |
|
83
|
5 |
|
$this->suffixTypesBuilder->addFactory($suffix); |
|
84
|
|
|
|
|
85
|
5 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
public function addSuffixTypeConfig(string $suffix): self |
|
89
|
|
|
{ |
|
90
|
5 |
|
$this->suffixTypesBuilder->addConfig($suffix); |
|
91
|
|
|
|
|
92
|
5 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
public function addSuffixTypeDependencyProvider(string $suffix): self |
|
96
|
|
|
{ |
|
97
|
6 |
|
$this->suffixTypesBuilder->addDependencyProvider($suffix); |
|
98
|
|
|
|
|
99
|
6 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param class-string $key |
|
104
|
|
|
* @param class-string|object|callable $value |
|
105
|
|
|
*/ |
|
106
|
9 |
|
public function addMappingInterface(string $key, $value): self |
|
107
|
|
|
{ |
|
108
|
9 |
|
$this->mappingInterfacesBuilder->bind($key, $value); |
|
109
|
|
|
|
|
110
|
9 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return class-string|object|callable |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
4 |
|
public function getExternalService(string $key) |
|
117
|
|
|
{ |
|
118
|
4 |
|
return $this->externalServices[$key]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param class-string|object|callable $value |
|
|
|
|
|
|
123
|
|
|
*/ |
|
124
|
2 |
|
public function addExternalService(string $key, $value): self |
|
125
|
|
|
{ |
|
126
|
2 |
|
$this->externalServices[$key] = $value; |
|
127
|
|
|
|
|
128
|
2 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
10 |
|
public function resetInMemoryCache(): self |
|
132
|
|
|
{ |
|
133
|
10 |
|
$this->shouldResetInMemoryCache = true; |
|
134
|
|
|
|
|
135
|
10 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
14 |
|
public function setFileCacheEnabled(bool $flag): self |
|
139
|
|
|
{ |
|
140
|
14 |
|
$this->fileCacheEnabled = $flag; |
|
141
|
|
|
|
|
142
|
14 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
public function setFileCacheDirectory(string $dir): self |
|
146
|
|
|
{ |
|
147
|
2 |
|
$this->fileCacheDirectory = $dir; |
|
148
|
|
|
|
|
149
|
2 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
public function setProfilerEnabled(bool $flag): self |
|
153
|
|
|
{ |
|
154
|
2 |
|
$this->profilerEnabled = $flag; |
|
155
|
|
|
|
|
156
|
2 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
public function setProfilerDirectory(string $dir): self |
|
160
|
|
|
{ |
|
161
|
2 |
|
$this->profilerDirectory = $dir; |
|
162
|
|
|
|
|
163
|
2 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param list<string> $list |
|
168
|
|
|
*/ |
|
169
|
3 |
|
public function setProjectNamespaces(array $list): self |
|
170
|
|
|
{ |
|
171
|
3 |
|
$this->projectNamespaces = $list; |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
3 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param mixed $value |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function addAppConfigKeyValue(string $key, $value): self |
|
180
|
|
|
{ |
|
181
|
1 |
|
$this->configKeyValues[$key] = $value; |
|
182
|
|
|
|
|
183
|
1 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param array<string, mixed> $config |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function addAppConfigKeyValues(array $config): self |
|
190
|
|
|
{ |
|
191
|
1 |
|
$this->configKeyValues = array_merge($this->configKeyValues, $config); |
|
192
|
|
|
|
|
193
|
1 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return array{ |
|
198
|
|
|
* external-services: array<string,class-string|object|callable>, |
|
199
|
|
|
* config-builder: ConfigBuilder, |
|
200
|
|
|
* suffix-types-builder: SuffixTypesBuilder, |
|
201
|
|
|
* mapping-interfaces-builder: MappingInterfacesBuilder, |
|
202
|
|
|
* should-reset-in-memory-cache: bool, |
|
203
|
|
|
* file-cache-enabled: bool, |
|
204
|
|
|
* file-cache-directory: string, |
|
205
|
|
|
* profiler-enabled: bool, |
|
206
|
|
|
* profiler-directory: string, |
|
207
|
|
|
* project-namespaces: list<string>, |
|
208
|
|
|
* config-key-values: array<string,mixed>, |
|
209
|
|
|
* } |
|
210
|
|
|
* |
|
211
|
|
|
* @internal |
|
212
|
|
|
*/ |
|
213
|
49 |
|
public function build(): array |
|
214
|
|
|
{ |
|
215
|
|
|
return [ |
|
216
|
49 |
|
'external-services' => $this->externalServices, |
|
217
|
49 |
|
'config-builder' => $this->configBuilder, |
|
218
|
49 |
|
'suffix-types-builder' => $this->suffixTypesBuilder, |
|
219
|
49 |
|
'mapping-interfaces-builder' => $this->mappingInterfacesBuilder, |
|
220
|
49 |
|
'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache, |
|
221
|
49 |
|
'file-cache-enabled' => $this->fileCacheEnabled, |
|
222
|
49 |
|
'file-cache-directory' => $this->fileCacheDirectory, |
|
223
|
49 |
|
'profiler-enabled' => $this->profilerEnabled, |
|
224
|
49 |
|
'profiler-directory' => $this->profilerDirectory, |
|
225
|
49 |
|
'project-namespaces' => $this->projectNamespaces, |
|
226
|
49 |
|
'config-key-values' => $this->configKeyValues, |
|
227
|
|
|
]; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|