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