1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Bootstrap; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\Profiler\GacelaProfiler; |
8
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder; |
9
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder; |
10
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
use function is_callable; |
14
|
|
|
|
15
|
|
|
final class SetupGacela extends AbstractSetupGacela |
16
|
|
|
{ |
17
|
|
|
/** @var callable(ConfigBuilder):void */ |
18
|
|
|
private $configFn; |
19
|
|
|
|
20
|
|
|
/** @var callable(MappingInterfacesBuilder,array<string,mixed>):void */ |
21
|
|
|
private $mappingInterfacesFn; |
22
|
|
|
|
23
|
|
|
/** @var callable(SuffixTypesBuilder):void */ |
24
|
|
|
private $suffixTypesFn; |
25
|
|
|
|
26
|
|
|
/** @var array<string,class-string|object|callable> */ |
|
|
|
|
27
|
|
|
private array $externalServices = []; |
28
|
|
|
|
29
|
|
|
private ?ConfigBuilder $configBuilder = null; |
30
|
|
|
|
31
|
|
|
private ?SuffixTypesBuilder $suffixTypesBuilder = null; |
32
|
|
|
|
33
|
|
|
private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null; |
34
|
|
|
|
35
|
|
|
private bool $cacheEnabled = true; |
36
|
|
|
|
37
|
|
|
private bool $profilerEnabled = false; |
38
|
|
|
|
39
|
|
|
private string $profilerDirectory = GacelaProfiler::DEFAULT_DIRECTORY_VALUE; |
40
|
|
|
|
41
|
|
|
/** @var list<string> */ |
42
|
|
|
private array $projectNamespaces = []; |
43
|
|
|
|
44
|
|
|
/** @var array<string,mixed> */ |
45
|
|
|
private array $configKeyValues = []; |
46
|
|
|
|
47
|
64 |
|
public function __construct() |
48
|
|
|
{ |
49
|
64 |
|
$this->configFn = static function (): void { |
50
|
|
|
}; |
51
|
64 |
|
$this->mappingInterfacesFn = static function (): void { |
52
|
|
|
}; |
53
|
64 |
|
$this->suffixTypesFn = static function (): void { |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
10 |
|
public static function fromFile(string $gacelaFilePath): self |
58
|
|
|
{ |
59
|
10 |
|
if (!is_file($gacelaFilePath)) { |
60
|
|
|
throw new RuntimeException("Invalid file path: '{$gacelaFilePath}'"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @var callable(GacelaConfig):void|null $setupGacelaFileFn */ |
64
|
10 |
|
$setupGacelaFileFn = include $gacelaFilePath; |
65
|
10 |
|
if (!is_callable($setupGacelaFileFn)) { |
66
|
|
|
return new self(); |
67
|
|
|
} |
68
|
|
|
|
69
|
10 |
|
return self::fromCallable($setupGacelaFileFn); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param callable(GacelaConfig):void $setupGacelaFileFn |
74
|
|
|
*/ |
75
|
40 |
|
public static function fromCallable(callable $setupGacelaFileFn): self |
76
|
|
|
{ |
77
|
40 |
|
$gacelaConfig = new GacelaConfig(); |
78
|
40 |
|
$setupGacelaFileFn($gacelaConfig); |
79
|
|
|
|
80
|
40 |
|
return self::fromGacelaConfig($gacelaConfig); |
81
|
|
|
} |
82
|
|
|
|
83
|
46 |
|
public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self |
84
|
|
|
{ |
85
|
46 |
|
$build = $gacelaConfig->build(); |
86
|
|
|
|
87
|
46 |
|
return (new self()) |
88
|
46 |
|
->setConfigBuilder($build['config-builder']) |
89
|
46 |
|
->setSuffixTypesBuilder($build['suffix-types-builder']) |
90
|
46 |
|
->setMappingInterfacesBuilder($build['mapping-interfaces-builder']) |
91
|
46 |
|
->setExternalServices($build['external-services']) |
92
|
46 |
|
->setCacheEnabled($build['cache-enabled']) |
93
|
46 |
|
->setProfilerEnabled($build['profiler-enabled']) |
94
|
46 |
|
->setProfilerDirectory($build['profiler-directory']) |
95
|
46 |
|
->setProjectNamespaces($build['project-namespaces']) |
96
|
46 |
|
->setConfigKeyValues($build['config-key-values']); |
97
|
|
|
} |
98
|
|
|
|
99
|
46 |
|
public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self |
100
|
|
|
{ |
101
|
46 |
|
$this->mappingInterfacesBuilder = $builder; |
102
|
|
|
|
103
|
46 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
46 |
|
public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self |
107
|
|
|
{ |
108
|
46 |
|
$this->suffixTypesBuilder = $builder; |
109
|
|
|
|
110
|
46 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
46 |
|
public function setConfigBuilder(ConfigBuilder $builder): self |
114
|
|
|
{ |
115
|
46 |
|
$this->configBuilder = $builder; |
116
|
|
|
|
117
|
46 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param callable(ConfigBuilder):void $callable |
122
|
|
|
*/ |
123
|
3 |
|
public function setConfigFn(callable $callable): self |
124
|
|
|
{ |
125
|
3 |
|
$this->configFn = $callable; |
126
|
|
|
|
127
|
3 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
64 |
|
public function buildConfig(ConfigBuilder $configBuilder): ConfigBuilder |
131
|
|
|
{ |
132
|
64 |
|
if ($this->configBuilder) { |
133
|
46 |
|
$configBuilder = $this->configBuilder; |
134
|
|
|
} |
135
|
|
|
|
136
|
64 |
|
($this->configFn)($configBuilder); |
137
|
|
|
|
138
|
64 |
|
return $configBuilder; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable |
143
|
|
|
*/ |
144
|
3 |
|
public function setMappingInterfacesFn(callable $callable): self |
145
|
|
|
{ |
146
|
3 |
|
$this->mappingInterfacesFn = $callable; |
147
|
|
|
|
148
|
3 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically. |
153
|
|
|
* |
154
|
|
|
* @param array<string,class-string|object|callable> $externalServices |
|
|
|
|
155
|
|
|
*/ |
156
|
64 |
|
public function buildMappingInterfaces( |
157
|
|
|
MappingInterfacesBuilder $mappingInterfacesBuilder, |
158
|
|
|
array $externalServices |
159
|
|
|
): MappingInterfacesBuilder { |
160
|
64 |
|
if ($this->mappingInterfacesBuilder) { |
161
|
46 |
|
$mappingInterfacesBuilder = $this->mappingInterfacesBuilder; |
162
|
|
|
} |
163
|
|
|
|
164
|
64 |
|
($this->mappingInterfacesFn)( |
165
|
|
|
$mappingInterfacesBuilder, |
166
|
64 |
|
array_merge($this->externalServices, $externalServices) |
167
|
|
|
); |
168
|
|
|
|
169
|
64 |
|
return $mappingInterfacesBuilder; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param callable(SuffixTypesBuilder):void $callable |
174
|
|
|
*/ |
175
|
3 |
|
public function setSuffixTypesFn(callable $callable): self |
176
|
|
|
{ |
177
|
3 |
|
$this->suffixTypesFn = $callable; |
178
|
|
|
|
179
|
3 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Allow overriding gacela resolvable types. |
184
|
|
|
*/ |
185
|
64 |
|
public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): SuffixTypesBuilder |
186
|
|
|
{ |
187
|
64 |
|
if ($this->suffixTypesBuilder) { |
188
|
46 |
|
$suffixTypesBuilder = $this->suffixTypesBuilder; |
189
|
|
|
} |
190
|
|
|
|
191
|
64 |
|
($this->suffixTypesFn)($suffixTypesBuilder); |
192
|
|
|
|
193
|
64 |
|
return $suffixTypesBuilder; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array<string,class-string|object|callable> $array |
|
|
|
|
198
|
|
|
*/ |
199
|
49 |
|
public function setExternalServices(array $array): self |
200
|
|
|
{ |
201
|
49 |
|
$this->externalServices = $array; |
202
|
|
|
|
203
|
49 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array<string,class-string|object|callable> |
|
|
|
|
208
|
|
|
*/ |
209
|
16 |
|
public function externalServices(): array |
210
|
|
|
{ |
211
|
16 |
|
return $this->externalServices; |
212
|
|
|
} |
213
|
|
|
|
214
|
46 |
|
public function setCacheEnabled(bool $flag): self |
215
|
|
|
{ |
216
|
46 |
|
$this->cacheEnabled = $flag; |
217
|
|
|
|
218
|
46 |
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
51 |
|
public function isCacheEnabled(): bool |
222
|
|
|
{ |
223
|
51 |
|
return $this->cacheEnabled; |
224
|
|
|
} |
225
|
|
|
|
226
|
46 |
|
public function setProfilerEnabled(bool $flag): self |
227
|
|
|
{ |
228
|
46 |
|
$this->profilerEnabled = $flag; |
229
|
|
|
|
230
|
46 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
28 |
|
public function isProfilerEnabled(): bool |
234
|
|
|
{ |
235
|
28 |
|
return $this->profilerEnabled; |
236
|
|
|
} |
237
|
|
|
|
238
|
46 |
|
public function setProfilerDirectory(string $dir): self |
239
|
|
|
{ |
240
|
46 |
|
$this->profilerDirectory = $dir; |
241
|
|
|
|
242
|
46 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
4 |
|
public function getProfilerDirectory(): string |
246
|
|
|
{ |
247
|
4 |
|
return $this->profilerDirectory; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param list<string> $list |
252
|
|
|
*/ |
253
|
46 |
|
public function setProjectNamespaces(array $list): self |
254
|
|
|
{ |
255
|
46 |
|
$this->projectNamespaces = $list; |
|
|
|
|
256
|
|
|
|
257
|
46 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return list<string> |
262
|
|
|
*/ |
263
|
30 |
|
public function getProjectNamespaces(): array |
264
|
|
|
{ |
265
|
30 |
|
return $this->projectNamespaces; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return array<string,mixed> |
270
|
|
|
*/ |
271
|
51 |
|
public function getConfigKeyValues(): array |
272
|
|
|
{ |
273
|
51 |
|
return $this->configKeyValues; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param array<string,mixed> $configKeyValues |
278
|
|
|
*/ |
279
|
46 |
|
private function setConfigKeyValues(array $configKeyValues): self |
280
|
|
|
{ |
281
|
46 |
|
$this->configKeyValues = $configKeyValues; |
282
|
|
|
|
283
|
46 |
|
return $this; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|