1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\ScriptRuntime; |
4
|
|
|
|
5
|
|
|
use function array_merge; |
6
|
|
|
use function dirname; |
7
|
|
|
use Dotenv\Dotenv; |
8
|
|
|
use Dotenv\Environment\DotenvFactory; |
9
|
|
|
use function pathinfo; |
10
|
|
|
use const PATHINFO_BASENAME; |
11
|
|
|
use const PATHINFO_DIRNAME; |
12
|
|
|
use const PATHINFO_EXTENSION; |
13
|
|
|
use const PATHINFO_FILENAME; |
14
|
|
|
use Shopware\Psh\Config\ScriptPath; |
15
|
|
|
use Symfony\Component\Process\Process; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create representation of the current environment variables and constants |
19
|
|
|
*/ |
20
|
|
|
class ProcessEnvironment |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ValueProvider[] |
24
|
|
|
*/ |
25
|
|
|
private $constants; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ValueProvider[] |
29
|
|
|
*/ |
30
|
|
|
private $variables; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Template[] |
34
|
|
|
*/ |
35
|
|
|
private $templates; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $dotenvVariables; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $constants |
44
|
|
|
* @param array $variables |
45
|
|
|
* @param array $templates |
46
|
|
|
* @param array $dotenvPaths |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $constants, array $variables, array $templates, array $dotenvPaths) |
49
|
|
|
{ |
50
|
|
|
$this->constants = $this->initializeConstants($constants); |
51
|
|
|
$this->variables = $this->initializeVariables($variables); |
52
|
|
|
$this->templates = $this->initializeTemplates($templates); |
|
|
|
|
53
|
|
|
$this->dotenvVariables = $this->initializeDotenvVariables($dotenvPaths); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $constants |
58
|
|
|
* @return ValueProvider[] |
59
|
|
|
*/ |
60
|
|
|
private function initializeConstants(array $constants): array |
61
|
|
|
{ |
62
|
|
|
$resolvedValues = []; |
63
|
|
|
foreach ($constants as $name => $value) { |
64
|
|
|
$resolvedValues[$name] = new SimpleValueProvider((string) $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $resolvedValues; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $variables |
72
|
|
|
* @return ValueProvider[] |
73
|
|
|
*/ |
74
|
|
|
private function initializeVariables(array $variables): array |
75
|
|
|
{ |
76
|
|
|
$resolvedVariables = []; |
77
|
|
|
foreach ($variables as $name => $shellCommand) { |
78
|
|
|
$process = $this->createProcess($shellCommand); |
79
|
|
|
$resolvedVariables[$name] = new ProcessValueProvider($process); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $resolvedVariables; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ScriptPath[] $dotenvPaths |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function initializeDotenvVariables(array $dotenvPaths): array |
91
|
|
|
{ |
92
|
|
|
$variables = []; |
93
|
|
|
|
94
|
|
|
foreach ($dotenvPaths as $dotenvPath) { |
95
|
|
|
$dotenvVariables = $this->getDotenvVariables($dotenvPath); |
96
|
|
|
|
97
|
|
|
foreach ($dotenvVariables as $variableKey => $variableValue) { |
98
|
|
|
$variables[$variableKey] = new SimpleValueProvider($variableValue); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $variables; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $templates |
107
|
|
|
* @return ValueProvider[] |
108
|
|
|
*/ |
109
|
|
|
private function initializeTemplates(array $templates): array |
110
|
|
|
{ |
111
|
|
|
$resolvedVariables = []; |
112
|
|
|
foreach ($templates as $template) { |
113
|
|
|
$resolvedVariables[] = new Template($template['source'], $template['destination']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $resolvedVariables; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return ValueProvider[] |
121
|
|
|
*/ |
122
|
|
|
public function getAllValues(): array |
123
|
|
|
{ |
124
|
|
|
return array_merge( |
125
|
|
|
$this->constants, |
126
|
|
|
$this->dotenvVariables, |
127
|
|
|
$this->variables |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Template[] |
133
|
|
|
*/ |
134
|
|
|
public function getTemplates(): array |
135
|
|
|
{ |
136
|
|
|
return $this->templates; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $shellCommand |
141
|
|
|
* @return Process |
142
|
|
|
*/ |
143
|
|
|
public function createProcess(string $shellCommand): Process |
144
|
|
|
{ |
145
|
|
|
return new Process($shellCommand); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param ScriptPath $dotenvPath |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
private function getDotenvVariables(ScriptPath $dotenvPath): array |
154
|
|
|
{ |
155
|
|
|
$filePath = $dotenvPath->getPath(); |
156
|
|
|
|
157
|
|
|
$dotenv = Dotenv::create( |
158
|
|
|
pathinfo($filePath, PATHINFO_DIRNAME), |
159
|
|
|
pathinfo($filePath, PATHINFO_BASENAME), |
160
|
|
|
new DotenvFactory() |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
return $dotenv->load(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..