1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Config; |
4
|
|
|
|
5
|
|
|
use Shopware\Psh\Application\RuntimeParameters; |
6
|
|
|
use function array_map; |
7
|
|
|
use function array_merge; |
8
|
|
|
use function in_array; |
9
|
|
|
use function mb_strtoupper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents the global configuration consisting of multiple environments |
13
|
|
|
*/ |
14
|
|
|
class Config |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EnvironmentResolver |
18
|
|
|
*/ |
19
|
|
|
private $resolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $header; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $defaultEnvironment; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ConfigEnvironment[] |
33
|
|
|
*/ |
34
|
|
|
private $environments; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RuntimeParameters |
38
|
|
|
*/ |
39
|
|
|
private $runtimeParameters; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ConfigEnvironment[] $environments |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
EnvironmentResolver $resolver, |
46
|
|
|
string $defaultEnvironment, |
47
|
|
|
array $environments, |
48
|
|
|
RuntimeParameters $runtimeParameters, |
49
|
|
|
?string $header = null |
50
|
|
|
) { |
51
|
|
|
$this->resolver = $resolver; |
52
|
|
|
$this->defaultEnvironment = $defaultEnvironment; |
53
|
|
|
$this->environments = $environments; |
54
|
|
|
$this->runtimeParameters = $runtimeParameters; |
55
|
|
|
$this->header = $header; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ScriptsPath[] |
60
|
|
|
*/ |
61
|
|
|
public function getAllScriptsPaths(): array |
62
|
|
|
{ |
63
|
|
|
$paths = []; |
64
|
|
|
|
65
|
|
|
foreach ($this->environments as $name => $environmentConfig) { |
66
|
|
|
foreach ($environmentConfig->getAllScriptsPaths() as $path) { |
67
|
|
|
$paths[] = $path; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $paths; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getTemplates(?string $environment = null): array |
75
|
|
|
{ |
76
|
|
|
return $this->createResult( |
77
|
|
|
[$this->getEnvironment(), 'getTemplates'], |
78
|
|
|
[$this->getEnvironment($environment), 'getTemplates'] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getDynamicVariables(?string $environment = null): array |
83
|
|
|
{ |
84
|
|
|
return $this->resolver->resolveVariables($this->createUpperCaseResult( |
85
|
|
|
[$this->getEnvironment(), 'getDynamicVariables'], |
86
|
|
|
[$this->getEnvironment($environment), 'getDynamicVariables'] |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getConstants(?string $environment = null): array |
91
|
|
|
{ |
92
|
|
|
return $this->resolver->resolveConstants($this->createUpperCaseResult( |
93
|
|
|
[$this->getEnvironment(), 'getConstants'], |
94
|
|
|
[$this->getEnvironment($environment), 'getConstants'], |
95
|
|
|
[$this, 'getParams'] |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getAllPlaceholders(?string $environment = null): array |
100
|
|
|
{ |
101
|
|
|
return array_merge( |
102
|
|
|
$this->getConstants($environment), |
103
|
|
|
$this->getDotenvVariables($environment), |
104
|
|
|
$this->getDynamicVariables($environment) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return ValueProvider[] |
110
|
|
|
*/ |
111
|
|
|
public function getDotenvVariables(?string $environment = null): array |
112
|
|
|
{ |
113
|
|
|
$paths = $this->getDotenvPaths($environment); |
114
|
|
|
|
115
|
|
|
return $this->resolver->resolveDotenvVariables($paths); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return RequiredValue[] |
120
|
|
|
*/ |
121
|
|
|
public function getRequiredVariables(?string $environment = null): array |
122
|
|
|
{ |
123
|
|
|
$requiredValues = $this->createUpperCaseResult( |
124
|
|
|
[$this->getEnvironment(), 'getRequiredVariables'], |
125
|
|
|
[$this->getEnvironment($environment), 'getRequiredVariables'] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$result = []; |
129
|
|
|
foreach ($requiredValues as $name => $description) { |
130
|
|
|
$result[$name] = new RequiredValue($name, $description); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return DotenvFile[] |
138
|
|
|
*/ |
139
|
|
|
public function getDotenvPaths(?string $environment = null): array |
140
|
|
|
{ |
141
|
|
|
$paths = $this->createResult( |
142
|
|
|
[$this->getEnvironment(), 'getDotenvPaths'], |
143
|
|
|
[$this->getEnvironment($environment), 'getDotenvPaths'] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return array_map(static function (string $path): DotenvFile { |
147
|
|
|
return new DotenvFile($path); |
148
|
|
|
}, $paths); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getHeader(): ?string |
155
|
|
|
{ |
156
|
|
|
return $this->header; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return ConfigEnvironment[] |
161
|
|
|
*/ |
162
|
|
|
public function getEnvironments(): array |
163
|
|
|
{ |
164
|
|
|
return $this->environments; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getDefaultEnvironment(): string |
168
|
|
|
{ |
169
|
|
|
return $this->defaultEnvironment; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function hasOption(string $name): bool |
173
|
|
|
{ |
174
|
|
|
return in_array($name, $this->runtimeParameters->getAppParams(), true); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getImports(): array |
178
|
|
|
{ |
179
|
|
|
return $this->getEnvironment()->getImports(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getScriptNames(): array |
183
|
|
|
{ |
184
|
|
|
return $this->runtimeParameters->getCommands(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function getParams(): array |
188
|
|
|
{ |
189
|
|
|
return $this->runtimeParameters->getOverwrites(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function createResult(callable ...$valueProviders): array |
193
|
|
|
{ |
194
|
|
|
$mergedKeyValues = []; |
195
|
|
|
|
196
|
|
|
foreach ($valueProviders as $valueProvider) { |
197
|
|
|
foreach ($valueProvider() as $key => $value) { |
198
|
|
|
$mergedKeyValues[$key] = $value; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $mergedKeyValues; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function createUpperCaseResult(callable ...$valueProviders): array |
206
|
|
|
{ |
207
|
|
|
$mergedKeyValues = []; |
208
|
|
|
|
209
|
|
|
foreach ($valueProviders as $valueProvider) { |
210
|
|
|
foreach ($valueProvider() as $key => $value) { |
211
|
|
|
$mergedKeyValues[mb_strtoupper($key)] = $value; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $mergedKeyValues; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function getEnvironment(?string $name = null): ConfigEnvironment |
219
|
|
|
{ |
220
|
|
|
if ($name === null) { |
221
|
|
|
return $this->environments[$this->defaultEnvironment]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this->environments[$name]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|