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\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 string $cacheDirectory = GacelaCache::DEFAULT_DIRECTORY_VALUE; |
28
|
|
|
|
29
|
|
|
/** @var list<string> */ |
30
|
|
|
private array $projectNamespaces = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array<string,class-string|object|callable> $externalServices |
|
|
|
|
34
|
|
|
*/ |
35
|
40 |
|
public function __construct(array $externalServices = []) |
36
|
|
|
{ |
37
|
40 |
|
$this->externalServices = $externalServices; |
38
|
40 |
|
$this->configBuilder = new ConfigBuilder(); |
39
|
40 |
|
$this->suffixTypesBuilder = new SuffixTypesBuilder(); |
40
|
40 |
|
$this->mappingInterfacesBuilder = new MappingInterfacesBuilder(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Closure(GacelaConfig):void |
45
|
|
|
*/ |
46
|
7 |
|
public static function withPhpConfigDefault(): callable |
47
|
|
|
{ |
48
|
7 |
|
return static function (self $config): void { |
49
|
7 |
|
$config->addAppConfig('config/*.php', 'config/local.php'); |
50
|
|
|
}; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $path define the path where Gacela will read all the config files |
55
|
|
|
* @param string $pathLocal define the path where Gacela will read the local config file |
56
|
|
|
* @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files |
|
|
|
|
57
|
|
|
*/ |
58
|
23 |
|
public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self |
59
|
|
|
{ |
60
|
23 |
|
$this->configBuilder->add($path, $pathLocal, $reader); |
61
|
|
|
|
62
|
23 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function addSuffixTypeFacade(string $suffix): self |
66
|
|
|
{ |
67
|
2 |
|
$this->suffixTypesBuilder->addFacade($suffix); |
68
|
|
|
|
69
|
2 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function addSuffixTypeFactory(string $suffix): self |
73
|
|
|
{ |
74
|
2 |
|
$this->suffixTypesBuilder->addFactory($suffix); |
75
|
|
|
|
76
|
2 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function addSuffixTypeConfig(string $suffix): self |
80
|
|
|
{ |
81
|
2 |
|
$this->suffixTypesBuilder->addConfig($suffix); |
82
|
|
|
|
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
public function addSuffixTypeDependencyProvider(string $suffix): self |
87
|
|
|
{ |
88
|
3 |
|
$this->suffixTypesBuilder->addDependencyProvider($suffix); |
89
|
|
|
|
90
|
3 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param class-string $key |
|
|
|
|
95
|
|
|
* @param class-string|object|callable $value |
96
|
|
|
*/ |
97
|
9 |
|
public function addMappingInterface(string $key, $value): self |
98
|
|
|
{ |
99
|
9 |
|
$this->mappingInterfacesBuilder->bind($key, $value); |
100
|
|
|
|
101
|
9 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return class-string|object|callable |
|
|
|
|
106
|
|
|
*/ |
107
|
4 |
|
public function getExternalService(string $key) |
108
|
|
|
{ |
109
|
4 |
|
return $this->externalServices[$key]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param class-string|object|callable $value |
|
|
|
|
114
|
|
|
*/ |
115
|
2 |
|
public function addExternalService(string $key, $value): self |
116
|
|
|
{ |
117
|
2 |
|
$this->externalServices[$key] = $value; |
118
|
|
|
|
119
|
2 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
8 |
|
public function setCacheEnabled(bool $flag): self |
123
|
|
|
{ |
124
|
8 |
|
$this->cacheEnabled = $flag; |
125
|
|
|
|
126
|
8 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function setCacheDirectory(string $dir): self |
130
|
|
|
{ |
131
|
1 |
|
$this->cacheDirectory = $dir; |
132
|
|
|
|
133
|
1 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param list<string> $list |
138
|
|
|
*/ |
139
|
3 |
|
public function setProjectNamespaces(array $list): self |
140
|
|
|
{ |
141
|
3 |
|
$this->projectNamespaces = $list; |
|
|
|
|
142
|
|
|
|
143
|
3 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array{ |
148
|
|
|
* external-services:array<string,class-string|object|callable>, |
149
|
|
|
* config-builder:ConfigBuilder, |
150
|
|
|
* suffix-types-builder:SuffixTypesBuilder, |
151
|
|
|
* mapping-interfaces-builder:MappingInterfacesBuilder, |
152
|
|
|
* cache-enabled:bool, |
153
|
|
|
* cache-directory:string, |
154
|
|
|
* project-namespaces:list<string>, |
155
|
|
|
* } |
156
|
|
|
* |
157
|
|
|
* @internal |
158
|
|
|
*/ |
159
|
40 |
|
public function build(): array |
160
|
|
|
{ |
161
|
|
|
return [ |
162
|
40 |
|
'external-services' => $this->externalServices, |
163
|
40 |
|
'config-builder' => $this->configBuilder, |
164
|
40 |
|
'suffix-types-builder' => $this->suffixTypesBuilder, |
165
|
40 |
|
'mapping-interfaces-builder' => $this->mappingInterfacesBuilder, |
166
|
40 |
|
'cache-enabled' => $this->cacheEnabled, |
167
|
40 |
|
'cache-directory' => $this->cacheDirectory, |
168
|
40 |
|
'project-namespaces' => $this->projectNamespaces, |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|