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 $currentDynamicVariables; |
23
|
|
|
|
24
|
|
|
private $templates; |
25
|
|
|
|
26
|
|
|
private $currentConstants; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string|null $header |
30
|
|
|
* @return ConfigBuilder |
31
|
|
|
*/ |
32
|
|
|
public function setHeader(string $header = null): ConfigBuilder |
33
|
|
|
{ |
34
|
|
|
$this->header = $header; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string|null $environment |
40
|
|
|
* @return ConfigBuilder |
41
|
|
|
*/ |
42
|
|
|
public function start(string $environment = null): ConfigBuilder |
43
|
|
|
{ |
44
|
|
|
$this->reset(); |
45
|
|
|
if (!$environment) { |
|
|
|
|
46
|
|
|
$environment = self::DEFAULT_ENV; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->currentEnvironment = $environment; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $commandPaths |
55
|
|
|
* @return ConfigBuilder |
56
|
|
|
*/ |
57
|
|
|
public function setCommandPaths(array $commandPaths): ConfigBuilder |
58
|
|
|
{ |
59
|
|
|
$this->currentCommandPaths = $commandPaths; |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $dynamicVariables |
65
|
|
|
* @return ConfigBuilder |
66
|
|
|
*/ |
67
|
|
|
public function setDynamicVariables(array $dynamicVariables): ConfigBuilder |
68
|
|
|
{ |
69
|
|
|
$this->currentDynamicVariables = $dynamicVariables; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setDynamicVariable($key, $value): ConfigBuilder |
74
|
|
|
{ |
75
|
|
|
$this->currentDynamicVariables[$key] = $value; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $constants |
81
|
|
|
* @return ConfigBuilder |
82
|
|
|
*/ |
83
|
|
|
public function setConstants(array $constants): ConfigBuilder |
84
|
|
|
{ |
85
|
|
|
$this->currentConstants = $constants; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setConstVariable($key, $value): ConfigBuilder |
90
|
|
|
{ |
91
|
|
|
$this->currentConstants[$key] = $value; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $templates |
97
|
|
|
* @return ConfigBuilder |
98
|
|
|
*/ |
99
|
|
|
public function setTemplates(array $templates): ConfigBuilder |
100
|
|
|
{ |
101
|
|
|
$this->templates = $templates; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Config |
107
|
|
|
*/ |
108
|
|
|
public function create(array $params): Config |
109
|
|
|
{ |
110
|
|
|
$this->reset(); |
111
|
|
|
|
112
|
|
|
return new Config($this->header, self::DEFAULT_ENV, $this->environments, $params); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function reset() |
116
|
|
|
{ |
117
|
|
|
if ($this->currentEnvironment) { |
118
|
|
|
$this->environments[$this->currentEnvironment] = new ConfigEnvironment( |
119
|
|
|
$this->currentCommandPaths, |
120
|
|
|
$this->currentDynamicVariables, |
121
|
|
|
$this->currentConstants, |
122
|
|
|
$this->templates |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->currentCommandPaths = []; |
127
|
|
|
$this->currentDynamicVariables = []; |
128
|
|
|
$this->currentConstants = []; |
129
|
|
|
$this->templates = []; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: