|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config\GacelaFileConfig; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder; |
|
8
|
|
|
|
|
9
|
|
|
final class GacelaConfigFile implements GacelaConfigFileInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var list<GacelaConfigItem> */ |
|
12
|
|
|
private array $configItems = []; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array<class-string,class-string|callable|object> */ |
|
|
|
|
|
|
15
|
|
|
private array $mappingInterfaces = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array{ |
|
19
|
|
|
* Factory:list<string>, |
|
20
|
|
|
* Config:list<string>, |
|
21
|
|
|
* DependencyProvider:list<string>, |
|
22
|
|
|
* } |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $suffixTypes = SuffixTypesBuilder::DEFAULT_SUFFIX_TYPES; |
|
25
|
|
|
|
|
26
|
15 |
|
public static function withDefaults(): self |
|
27
|
|
|
{ |
|
28
|
15 |
|
return (new self()) |
|
29
|
15 |
|
->setConfigItems([new GacelaConfigItem()]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param list<GacelaConfigItem> $configItems |
|
34
|
|
|
*/ |
|
35
|
47 |
|
public function setConfigItems(array $configItems): self |
|
36
|
|
|
{ |
|
37
|
47 |
|
$this->configItems = $configItems; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
47 |
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return list<GacelaConfigItem> |
|
44
|
|
|
*/ |
|
45
|
37 |
|
public function getConfigItems(): array |
|
46
|
|
|
{ |
|
47
|
37 |
|
return $this->configItems; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array<class-string,class-string|callable|object> $mappingInterfaces |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
41 |
|
public function setMappingInterfaces(array $mappingInterfaces): self |
|
54
|
|
|
{ |
|
55
|
41 |
|
$this->mappingInterfaces = $mappingInterfaces; |
|
56
|
|
|
|
|
57
|
41 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Map interfaces to concrete classes or callable (which will be resolved on runtime). |
|
62
|
|
|
* This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getMappingInterface(string $key) |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->mappingInterfaces[$key] ?? null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array<class-string,class-string|callable|object> |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
15 |
|
public function getMappingInterfaces(): array |
|
75
|
|
|
{ |
|
76
|
15 |
|
return $this->mappingInterfaces; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param array{ |
|
81
|
|
|
* Factory:list<string>, |
|
82
|
|
|
* Config:list<string>, |
|
83
|
|
|
* DependencyProvider:list<string> |
|
84
|
|
|
* } $suffixTypes |
|
85
|
|
|
*/ |
|
86
|
41 |
|
public function setSuffixTypes(array $suffixTypes): self |
|
87
|
|
|
{ |
|
88
|
41 |
|
$this->suffixTypes = $suffixTypes; |
|
89
|
|
|
|
|
90
|
41 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array{ |
|
95
|
|
|
* Factory:list<string>, |
|
96
|
|
|
* Config:list<string>, |
|
97
|
|
|
* DependencyProvider:list<string>, |
|
98
|
|
|
* } |
|
99
|
|
|
*/ |
|
100
|
27 |
|
public function getSuffixTypes(): array |
|
101
|
|
|
{ |
|
102
|
27 |
|
return $this->suffixTypes; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
15 |
|
public function combine(GacelaConfigFileInterface $other): GacelaConfigFileInterface |
|
106
|
|
|
{ |
|
107
|
15 |
|
$new = clone $this; |
|
108
|
15 |
|
$new->configItems = array_merge($this->configItems, $other->getConfigItems()); |
|
|
|
|
|
|
109
|
15 |
|
$new->mappingInterfaces = array_merge($this->mappingInterfaces, $other->getMappingInterfaces()); |
|
110
|
15 |
|
$new->suffixTypes = [ |
|
111
|
15 |
|
'Factory' => $this->filterList($other, 'Factory'), |
|
112
|
15 |
|
'Config' => $this->filterList($other, 'Config'), |
|
113
|
15 |
|
'DependencyProvider' => $this->filterList($other, 'DependencyProvider'), |
|
114
|
|
|
]; |
|
115
|
15 |
|
return $new; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return list<string> |
|
120
|
|
|
*/ |
|
121
|
15 |
|
private function filterList(GacelaConfigFileInterface $other, string $key): array |
|
122
|
|
|
{ |
|
123
|
15 |
|
$merged = array_merge($this->suffixTypes[$key], $other->getSuffixTypes()[$key]); // @phpstan-ignore-line |
|
124
|
15 |
|
$filtered = array_filter(array_unique($merged)); |
|
125
|
|
|
/** @var list<string> $values */ |
|
126
|
15 |
|
$values = array_values($filtered); |
|
127
|
|
|
|
|
128
|
15 |
|
return $values; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|