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