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