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