|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Psh\ScriptRuntime\Execution; |
|
4
|
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
|
6
|
|
|
use Dotenv\Repository\Adapter\MultiReader; |
|
7
|
|
|
use Dotenv\Repository\Adapter\PutenvAdapter; |
|
8
|
|
|
use Dotenv\Repository\Adapter\ServerConstAdapter; |
|
9
|
|
|
use Shopware\Psh\Config\DotenvFile; |
|
10
|
|
|
use Symfony\Component\Process\Process; |
|
11
|
|
|
use function array_keys; |
|
12
|
|
|
use function array_merge; |
|
13
|
|
|
use function pathinfo; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create representation of the current environment variables and constants |
|
17
|
|
|
*/ |
|
18
|
|
|
class ProcessEnvironment |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ValueProvider[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $constants; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ValueProvider[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $variables; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Template[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $templates; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $dotenvVariables; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param DotenvFile[] $dotenvPaths |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(array $constants, array $variables, array $templates, array $dotenvPaths) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->constants = $this->initializeConstants($constants); |
|
46
|
|
|
$this->variables = $this->initializeVariables($variables); |
|
47
|
|
|
$this->templates = $this->initializeTemplates($templates); |
|
48
|
|
|
$this->dotenvVariables = $this->initializeDotenvVariables($dotenvPaths); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return ValueProvider[] |
|
53
|
|
|
*/ |
|
54
|
|
|
private function initializeConstants(array $constants): array |
|
55
|
|
|
{ |
|
56
|
|
|
$resolvedValues = []; |
|
57
|
|
|
foreach ($constants as $name => $value) { |
|
58
|
|
|
$resolvedValues[$name] = new SimpleValueProvider((string) $value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $resolvedValues; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return ValueProvider[] |
|
66
|
|
|
*/ |
|
67
|
|
|
private function initializeVariables(array $variables): array |
|
68
|
|
|
{ |
|
69
|
|
|
$resolvedVariables = []; |
|
70
|
|
|
foreach ($variables as $name => $shellCommand) { |
|
71
|
|
|
$process = $this->createProcess($shellCommand); |
|
72
|
|
|
$resolvedVariables[$name] = new ProcessValueProvider($process); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $resolvedVariables; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param DotenvFile[] $dotenvPaths |
|
80
|
|
|
* @return ValueProvider[] |
|
81
|
|
|
*/ |
|
82
|
|
|
private function initializeDotenvVariables(array $dotenvPaths): array |
|
83
|
|
|
{ |
|
84
|
|
|
$variables = []; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($dotenvPaths as $dotenvPath) { |
|
87
|
|
|
$dotenvVariables = $this->loadDotenvVariables($dotenvPath); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($dotenvVariables as $variableKey => $variableValue) { |
|
90
|
|
|
$variables[$variableKey] = new SimpleValueProvider($variableValue); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $variables; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return Template[] |
|
99
|
|
|
*/ |
|
100
|
|
|
private function initializeTemplates(array $templates): array |
|
101
|
|
|
{ |
|
102
|
|
|
$resolvedVariables = []; |
|
103
|
|
|
foreach ($templates as $template) { |
|
104
|
|
|
$resolvedVariables[] = new Template($template['source'], $template['destination']); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $resolvedVariables; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return ValueProvider[] |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getAllValues(): array |
|
114
|
|
|
{ |
|
115
|
|
|
return array_merge( |
|
116
|
|
|
$this->constants, |
|
117
|
|
|
$this->dotenvVariables, |
|
118
|
|
|
$this->variables |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return Template[] |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getTemplates(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->templates; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function createProcess(string $shellCommand): Process |
|
131
|
|
|
{ |
|
132
|
|
|
return new Process($shellCommand); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function loadDotenvVariables(DotenvFile $dotenvPath): array |
|
136
|
|
|
{ |
|
137
|
|
|
$fileData = $this->loadEnvVarsFromDotenvFile($dotenvPath); |
|
138
|
|
|
|
|
139
|
|
|
return $this->diffDotenvVarsWithCurrentApplicationEnv($fileData); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function loadEnvVarsFromDotenvFile(DotenvFile $dotenvPath): array |
|
143
|
|
|
{ |
|
144
|
|
|
$filePath = $dotenvPath->getPath(); |
|
145
|
|
|
|
|
146
|
|
|
$dotenv = Dotenv::createArrayBacked( |
|
147
|
|
|
pathinfo($filePath, \PATHINFO_DIRNAME), |
|
148
|
|
|
pathinfo($filePath, \PATHINFO_BASENAME) |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
return $dotenv->load(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function diffDotenvVarsWithCurrentApplicationEnv(array $fileData): array |
|
155
|
|
|
{ |
|
156
|
|
|
$fileKeys = array_keys($fileData); |
|
157
|
|
|
$result = []; |
|
158
|
|
|
|
|
159
|
|
|
$reader = new MultiReader([ |
|
160
|
|
|
PutenvAdapter::create()->get(), |
|
161
|
|
|
ServerConstAdapter::create()->get(), |
|
162
|
|
|
]); |
|
163
|
|
|
|
|
164
|
|
|
foreach ($fileKeys as $key) { |
|
165
|
|
|
$option = $reader->read($key); |
|
166
|
|
|
|
|
167
|
|
|
if ($option->isEmpty()) { |
|
168
|
|
|
$result[$key] = $fileData[$key]; |
|
169
|
|
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$result[$key] = $option->get(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $result; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: