1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents the global configuration consisting of multiple environments |
7
|
|
|
*/ |
8
|
|
|
class Config |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $header; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $defaultEnvironment; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ConfigEnvironment[] |
22
|
|
|
*/ |
23
|
|
|
private $environments; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string|null $header |
27
|
|
|
* @param string $defaultEnvironment |
28
|
|
|
* @param ConfigEnvironment[] $environments |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
string $header = null, |
32
|
|
|
string $defaultEnvironment, |
33
|
|
|
array $environments |
34
|
|
|
) { |
35
|
|
|
$this->header = $header; |
36
|
|
|
$this->defaultEnvironment = $defaultEnvironment; |
37
|
|
|
$this->environments = $environments; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return ScriptPath[] |
42
|
|
|
*/ |
43
|
|
|
public function getAllScriptPaths(): array |
44
|
|
|
{ |
45
|
|
|
$paths = []; |
46
|
|
|
|
47
|
|
|
foreach ($this->environments as $name => $environmentConfig) { |
48
|
|
|
foreach ($environmentConfig->getAllScriptPaths() as $path) { |
49
|
|
|
if ($name !== $this->defaultEnvironment) { |
|
|
|
|
50
|
|
|
$paths[] = new ScriptPath($path, $name); |
51
|
|
|
} else { |
52
|
|
|
$paths[] = new ScriptPath($path); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $paths; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|null $environment |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getTemplates(string $environment = null): array |
65
|
|
|
{ |
66
|
|
|
return $this->createResult( |
67
|
|
|
[$this->getEnvironment(), 'getTemplates'], |
68
|
|
|
[$this->getEnvironment($environment), 'getTemplates'] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|null $environment |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getDynamicVariables(string $environment = null): array |
77
|
|
|
{ |
78
|
|
|
return $this->createResult( |
79
|
|
|
[$this->getEnvironment(), 'getDynamicVariables'], |
80
|
|
|
[$this->getEnvironment($environment), 'getDynamicVariables'] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string|null $environment |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getConstants(string $environment = null): array |
90
|
|
|
{ |
91
|
|
|
return $this->createResult( |
92
|
|
|
[$this->getEnvironment(), 'getConstants'], |
93
|
|
|
[$this->getEnvironment($environment), 'getConstants'] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getHeader() |
101
|
|
|
{ |
102
|
|
|
return $this->header; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getDefaultEnvironment(): string |
109
|
|
|
{ |
110
|
|
|
return $this->defaultEnvironment; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param callable $defaultValues |
115
|
|
|
* @param callable $specificValues |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
private function createResult(callable $defaultValues, callable $specificValues): array |
119
|
|
|
{ |
120
|
|
|
$mergedKeyValues = []; |
121
|
|
|
|
122
|
|
|
foreach ($defaultValues() as $key => $value) { |
123
|
|
|
$mergedKeyValues[$key] = $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($specificValues() as $key => $value) { |
127
|
|
|
$mergedKeyValues[$key] = $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $mergedKeyValues; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string|null $name |
135
|
|
|
* @return ConfigEnvironment |
136
|
|
|
*/ |
137
|
|
|
private function getEnvironment(string $name = null): ConfigEnvironment |
138
|
|
|
{ |
139
|
|
|
if (!$name) { |
|
|
|
|
140
|
|
|
return $this->environments[$this->defaultEnvironment]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->environments[$name]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|