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