|
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
|
|
|
$templates = $this->getEnvironment()->getTemplates(); |
|
77
|
|
|
|
|
78
|
|
|
if ($environment === null) { |
|
79
|
|
|
return $templates; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
foreach ($this->getEnvironment($environment)->getTemplates() as $template) { |
|
83
|
|
|
$templates[] = $template; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $templates; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getDynamicVariables(?string $environment = null): array |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->resolver->resolveVariables($this->createMergedUpperCaseResult( |
|
92
|
|
|
[$this->getEnvironment(), 'getDynamicVariables'], |
|
93
|
|
|
[$this->getEnvironment($environment), 'getDynamicVariables'] |
|
94
|
|
|
)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getConstants(?string $environment = null): array |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->resolver->resolveConstants($this->createMergedUpperCaseResult( |
|
100
|
|
|
[$this->getEnvironment(), 'getConstants'], |
|
101
|
|
|
[$this->getEnvironment($environment), 'getConstants'], |
|
102
|
|
|
[$this, 'getParams'] |
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getAllPlaceholders(?string $environment = null): array |
|
107
|
|
|
{ |
|
108
|
|
|
return array_merge( |
|
109
|
|
|
$this->getConstants($environment), |
|
110
|
|
|
$this->getDotenvVariables($environment), |
|
111
|
|
|
$this->getDynamicVariables($environment) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return ValueProvider[] |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getDotenvVariables(?string $environment = null): array |
|
119
|
|
|
{ |
|
120
|
|
|
$paths = $this->getDotenvPaths($environment); |
|
121
|
|
|
|
|
122
|
|
|
return $this->resolver->resolveDotenvVariables($paths); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return RequiredValue[] |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getRequiredVariables(?string $environment = null): array |
|
129
|
|
|
{ |
|
130
|
|
|
$requiredValues = $this->createMergedUpperCaseResult( |
|
131
|
|
|
[$this->getEnvironment(), 'getRequiredVariables'], |
|
132
|
|
|
[$this->getEnvironment($environment), 'getRequiredVariables'] |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
$result = []; |
|
136
|
|
|
foreach ($requiredValues as $name => $description) { |
|
137
|
|
|
$result[$name] = new RequiredValue($name, $description); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return DotenvFile[] |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getDotenvPaths(?string $environment = null): array |
|
147
|
|
|
{ |
|
148
|
|
|
$paths = $this->createMergedResult( |
|
149
|
|
|
[$this->getEnvironment(), 'getDotenvPaths'], |
|
150
|
|
|
[$this->getEnvironment($environment), 'getDotenvPaths'] |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
return array_map(static function (string $path): DotenvFile { |
|
154
|
|
|
return new DotenvFile($path); |
|
155
|
|
|
}, $paths); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getHeader(): ?string |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->header; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return ConfigEnvironment[] |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getEnvironments(): array |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->environments; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function getDefaultEnvironment(): string |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->defaultEnvironment; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function hasOption(string $name): bool |
|
180
|
|
|
{ |
|
181
|
|
|
return in_array($name, $this->runtimeParameters->getAppParams(), true); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function getImports(): array |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->getEnvironment()->getImports(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getScriptNames(): array |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->runtimeParameters->getCommands(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private function getParams(): array |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->runtimeParameters->getOverwrites(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
private function createMergedResult(callable ...$valueProviders): array |
|
200
|
|
|
{ |
|
201
|
|
|
$mergedKeyValues = []; |
|
202
|
|
|
|
|
203
|
|
|
foreach ($valueProviders as $valueProvider) { |
|
204
|
|
|
foreach ($valueProvider() as $key => $value) { |
|
205
|
|
|
$mergedKeyValues[$key] = $value; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $mergedKeyValues; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function createMergedUpperCaseResult(callable ...$valueProviders): array |
|
213
|
|
|
{ |
|
214
|
|
|
$mergedKeyValues = []; |
|
215
|
|
|
|
|
216
|
|
|
foreach ($valueProviders as $valueProvider) { |
|
217
|
|
|
foreach ($valueProvider() as $key => $value) { |
|
218
|
|
|
$mergedKeyValues[mb_strtoupper($key)] = $value; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $mergedKeyValues; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
private function getEnvironment(?string $name = null): ConfigEnvironment |
|
226
|
|
|
{ |
|
227
|
|
|
if ($name === null) { |
|
228
|
|
|
return $this->environments[$this->defaultEnvironment]; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this->environments[$name]; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|