1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Bootstrap; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\ConfigReaderInterface; |
8
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder; |
9
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder; |
10
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
11
|
|
|
|
12
|
|
|
final class GacelaConfig |
13
|
|
|
{ |
14
|
|
|
private ConfigBuilder $configBuilder; |
15
|
|
|
|
16
|
|
|
private SuffixTypesBuilder $suffixTypesBuilder; |
17
|
|
|
|
18
|
|
|
private MappingInterfacesBuilder $mappingInterfacesBuilder; |
19
|
|
|
|
20
|
|
|
/** @var array<string,class-string|object|callable> */ |
|
|
|
|
21
|
|
|
private array $externalServices; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array<string,class-string|object|callable> $externalServices |
|
|
|
|
25
|
|
|
*/ |
26
|
15 |
|
public function __construct(array $externalServices = []) |
27
|
|
|
{ |
28
|
15 |
|
$this->externalServices = $externalServices; |
29
|
15 |
|
$this->configBuilder = new ConfigBuilder(); |
30
|
15 |
|
$this->suffixTypesBuilder = new SuffixTypesBuilder(); |
31
|
15 |
|
$this->mappingInterfacesBuilder = new MappingInterfacesBuilder(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return callable(GacelaConfig):void |
36
|
|
|
*/ |
37
|
1 |
|
public static function withDefaults(): callable |
38
|
|
|
{ |
39
|
1 |
|
return static function (self $config): void { |
40
|
1 |
|
$config->addAppConfig('config/*.php'); |
41
|
|
|
}; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $path define the path where Gacela will read all the config files |
46
|
|
|
* @param string $pathLocal define the path where Gacela will read the local config file |
47
|
|
|
* @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files |
|
|
|
|
48
|
|
|
*/ |
49
|
6 |
|
public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self |
50
|
|
|
{ |
51
|
6 |
|
$this->configBuilder->add($path, $pathLocal, $reader); |
52
|
|
|
|
53
|
6 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function addSuffixTypeFacade(string $suffix): self |
57
|
|
|
{ |
58
|
2 |
|
$this->suffixTypesBuilder->addFacade($suffix); |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function addSuffixTypeFactory(string $suffix): self |
64
|
|
|
{ |
65
|
2 |
|
$this->suffixTypesBuilder->addFactory($suffix); |
66
|
|
|
|
67
|
2 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function addSuffixTypeConfig(string $suffix): self |
71
|
|
|
{ |
72
|
2 |
|
$this->suffixTypesBuilder->addConfig($suffix); |
73
|
|
|
|
74
|
2 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function addSuffixTypeDependencyProvider(string $suffix): self |
78
|
|
|
{ |
79
|
3 |
|
$this->suffixTypesBuilder->addDependencyProvider($suffix); |
80
|
|
|
|
81
|
3 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param class-string $key |
|
|
|
|
86
|
|
|
* @param class-string|object|callable $value |
87
|
|
|
*/ |
88
|
9 |
|
public function addMappingInterface(string $key, $value): self |
89
|
|
|
{ |
90
|
9 |
|
$this->mappingInterfacesBuilder->bind($key, $value); |
91
|
|
|
|
92
|
9 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return class-string|object|callable |
|
|
|
|
97
|
|
|
*/ |
98
|
4 |
|
public function getExternalService(string $key) |
99
|
|
|
{ |
100
|
4 |
|
return $this->externalServices[$key]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param class-string|object|callable $value |
|
|
|
|
105
|
|
|
*/ |
106
|
2 |
|
public function addExternalService(string $key, $value): self |
107
|
|
|
{ |
108
|
2 |
|
$this->externalServices[$key] = $value; |
109
|
|
|
|
110
|
2 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @internal |
115
|
|
|
* |
116
|
|
|
* @return array{ |
117
|
|
|
* external-services:array<string,class-string|object|callable>, |
118
|
|
|
* config-builder:ConfigBuilder, |
119
|
|
|
* suffix-types-builder:SuffixTypesBuilder, |
120
|
|
|
* mapping-interfaces-builder:MappingInterfacesBuilder, |
121
|
|
|
* } |
122
|
|
|
*/ |
123
|
15 |
|
public function build(): array |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
15 |
|
'external-services' => $this->externalServices, |
127
|
15 |
|
'config-builder' => $this->configBuilder, |
128
|
15 |
|
'suffix-types-builder' => $this->suffixTypesBuilder, |
129
|
15 |
|
'mapping-interfaces-builder' => $this->mappingInterfacesBuilder, |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|