1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Config; |
4
|
|
|
|
5
|
|
|
use Shopware\Psh\Application\RuntimeParameters; |
6
|
|
|
use function pathinfo; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Builder pattern |
10
|
|
|
* |
11
|
|
|
* Create a config from a more complicated proto format by representing a stateful representation of config read so far. |
12
|
|
|
*/ |
13
|
|
|
class ConfigBuilder |
14
|
|
|
{ |
15
|
|
|
const DEFAULT_ENV = '##default##'; |
16
|
|
|
|
17
|
|
|
private $header = ''; |
18
|
|
|
|
19
|
|
|
private $environments = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
private $currentEnvironment; |
25
|
|
|
|
26
|
|
|
private $currentCommandPaths; |
27
|
|
|
|
28
|
|
|
private $currentDotenvPaths; |
29
|
|
|
|
30
|
|
|
private $currentDynamicVariables; |
31
|
|
|
|
32
|
|
|
private $templates; |
33
|
|
|
|
34
|
|
|
private $currentConstants; |
35
|
|
|
|
36
|
|
|
private $hidden; |
37
|
|
|
|
38
|
|
|
private $currentRequiredVariables; |
39
|
|
|
|
40
|
|
|
private $imports; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $workingDirectory; |
46
|
|
|
|
47
|
|
|
public function setHeader(?string $header = null): ConfigBuilder |
48
|
|
|
{ |
49
|
|
|
$this->header = $header; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setWorkingDirectory(string $workingDirectory): ConfigBuilder |
55
|
|
|
{ |
56
|
|
|
$this->workingDirectory = $workingDirectory; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function start(?string $environment = null): ConfigBuilder |
62
|
|
|
{ |
63
|
|
|
$this->reset(); |
64
|
|
|
if ($environment === null) { |
65
|
|
|
$environment = self::DEFAULT_ENV; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->currentEnvironment = $environment; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setHidden(bool $set): ConfigBuilder |
74
|
|
|
{ |
75
|
|
|
$this->hidden = $set; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setCommandPaths(array $commandPaths): ConfigBuilder |
81
|
|
|
{ |
82
|
|
|
$this->currentCommandPaths = []; |
83
|
|
|
|
84
|
|
|
foreach ($commandPaths as $path) { |
85
|
|
|
$env = $this->currentEnvironment; |
86
|
|
|
|
87
|
|
|
if ($env === self::DEFAULT_ENV) { |
88
|
|
|
$env = null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->currentCommandPaths[] = new ScriptsPath($path, $this->workingDirectory, $this->hidden, $env); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @deprecated only used by yaml builder |
99
|
|
|
*/ |
100
|
|
|
public function setDotenvPaths(array $dotenvPaths): ConfigBuilder |
101
|
|
|
{ |
102
|
|
|
$this->currentDotenvPaths = []; |
103
|
|
|
|
104
|
|
|
foreach ($dotenvPaths as $dotenvPath) { |
105
|
|
|
$this->addDotenvPath($dotenvPath); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function addDotenvPath(string $dotenvPath): ConfigBuilder |
112
|
|
|
{ |
113
|
|
|
$this->currentDotenvPaths[pathinfo($dotenvPath, PATHINFO_BASENAME)] = $dotenvPath; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function addRequirePlaceholder(string $name, ?string $description = null): ConfigBuilder |
119
|
|
|
{ |
120
|
|
|
$this->currentRequiredVariables[$name] = $description; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @deprecated only used by yaml builder |
127
|
|
|
*/ |
128
|
|
|
public function setDynamicVariables(array $dynamicVariables): ConfigBuilder |
129
|
|
|
{ |
130
|
|
|
$this->currentDynamicVariables = $dynamicVariables; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function addDynamicVariable(string $key, string $value): ConfigBuilder |
136
|
|
|
{ |
137
|
|
|
$this->currentDynamicVariables[$key] = $value; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @deprecated only used by yaml builder |
144
|
|
|
*/ |
145
|
|
|
public function setConstants(array $constants): ConfigBuilder |
146
|
|
|
{ |
147
|
|
|
$this->currentConstants = $constants; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function addConstVariable(string $key, string $value): ConfigBuilder |
153
|
|
|
{ |
154
|
|
|
$this->currentConstants[$key] = $value; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function addImport(string $path): self |
160
|
|
|
{ |
161
|
|
|
$this->imports[] = $path; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setTemplates(array $templates, string $baseFile): ConfigBuilder |
167
|
|
|
{ |
168
|
|
|
$this->templates = []; |
169
|
|
|
|
170
|
|
|
foreach ($templates as $template) { |
171
|
|
|
$this->templates[] = new Template($template['source'], $template['destination'], $baseFile); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function create(RuntimeParameters $runtimeParameters): Config |
178
|
|
|
{ |
179
|
|
|
$this->reset(); |
180
|
|
|
|
181
|
|
|
return new Config(new EnvironmentResolver(), self::DEFAULT_ENV, $this->environments, $runtimeParameters, $this->header); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function reset(): void |
185
|
|
|
{ |
186
|
|
|
if ($this->currentEnvironment) { |
|
|
|
|
187
|
|
|
$this->environments[$this->currentEnvironment] = new ConfigEnvironment( |
188
|
|
|
$this->hidden, |
189
|
|
|
$this->currentCommandPaths, |
190
|
|
|
$this->currentDynamicVariables, |
191
|
|
|
$this->currentConstants, |
192
|
|
|
$this->templates, |
193
|
|
|
$this->currentDotenvPaths, |
194
|
|
|
$this->currentRequiredVariables, |
195
|
|
|
$this->imports |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->currentCommandPaths = []; |
200
|
|
|
$this->currentDotenvPaths = []; |
201
|
|
|
$this->currentDynamicVariables = []; |
202
|
|
|
$this->currentConstants = []; |
203
|
|
|
$this->templates = []; |
204
|
|
|
$this->hidden = false; |
205
|
|
|
$this->currentRequiredVariables = []; |
206
|
|
|
$this->imports = []; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: