-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
DeploymentConfig.php
319 lines (291 loc) · 8.25 KB
/
DeploymentConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\Phrase;
/**
* Application deployment configuration
*
* @api
* @since 100.0.2
*/
class DeploymentConfig
{
private const MAGENTO_ENV_PREFIX = 'MAGENTO_DC_';
private const ENV_NAME_PATTERN = '~^#env\(\s*(?<name>\w+)\s*(,\s*"(?<default>[^"]+)")?\)$~';
private const OVERRIDE_KEY = self::MAGENTO_ENV_PREFIX . '_OVERRIDE';
/**
* Configuration reader
*
* @var DeploymentConfig\Reader
*/
private $reader;
/**
* Configuration data
*
* @var array
*/
private $data = [];
/**
* Flattened data
*
* @var array
*/
private $flatData = [];
/**
* Injected configuration data
*
* @var array
*/
private $overrideData;
/**
* @var array
*/
private $envOverrides = [];
/**
* @var array
*/
private $readerLoad = [];
/**
* Constructor
*
* Data can be optionally injected in the constructor. This object's public interface is intentionally immutable
*
* @param DeploymentConfig\Reader $reader
* @param array $overrideData
*/
public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
{
$this->reader = $reader;
$this->overrideData = $overrideData;
}
/**
* Gets data from flattened data
*
* @param string $key
* @param mixed $defaultValue
* @return mixed|null
* @throws FileSystemException
* @throws RuntimeException
*/
public function get($key = null, $defaultValue = null)
{
if ($key === null) {
if (empty($this->flatData)) {
$this->reloadData();
}
return $this->flatData;
}
$result = $this->getByKey($key);
if ($result === null) {
if (empty($this->flatData) || count($this->getAllEnvOverrides())) {
$this->reloadData();
}
$result = $this->getByKey($key);
}
return $result ?? $defaultValue;
}
/**
* Checks if data available
*
* @return bool
* @throws FileSystemException
* @throws RuntimeException
*/
public function isAvailable()
{
return $this->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) !== null;
}
/**
* Gets a value specified key from config data
*
* @param string|null $key
* @return null|mixed
* @throws FileSystemException
* @throws RuntimeException
*/
public function getConfigData($key = null)
{
if ($key === null) {
if (empty($this->data)) {
$this->reloadInitialData();
}
return $this->data;
}
$result = $this->getConfigDataByKey($key);
if ($result === null) {
$this->reloadInitialData();
$result = $this->getConfigDataByKey($key);
}
return $result;
}
/**
* Resets config data
*
* @return void
*/
public function resetData()
{
$this->data = [];
$this->flatData = [];
}
/**
* Check if data from deploy files is available
*
* @return bool
* @throws FileSystemException
* @throws RuntimeException
* @since 100.1.3
*/
public function isDbAvailable()
{
return $this->getConfigData('db') !== null;
}
/**
* Get additional configuration from env variable MAGENTO_DC__OVERRIDE
*
* Data should be JSON encoded
*
* @return array
*/
private function getEnvOverride() : array
{
$env = getenv(self::OVERRIDE_KEY);
return !empty($env) ? (json_decode($env, true) ?? []) : [];
}
/**
* Loads the configuration data
*
* @return void
* @throws FileSystemException
* @throws RuntimeException
*/
private function reloadInitialData(): void
{
if (empty($this->readerLoad) || empty($this->data) || empty($this->flatData)) {
$this->readerLoad = $this->reader->load();
}
$this->data = array_replace(
$this->readerLoad,
$this->overrideData ?? [],
$this->getEnvOverride()
);
}
/**
* Loads the configuration data
*
* @return void
* @throws FileSystemException
* @throws RuntimeException
*/
private function reloadData(): void
{
$this->reloadInitialData();
// flatten data for config retrieval using get()
$this->flatData = $this->flattenParams($this->data);
$this->flatData = $this->getAllEnvOverrides() + $this->flatData;
}
/**
* Load all getenv() configs once
*
* @return array
*/
private function getAllEnvOverrides(): array
{
if (empty($this->envOverrides)) {
// allow reading values from env variables by convention
// MAGENTO_DC_{path}, like db/connection/default/host =>
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
foreach (getenv() as $key => $value) {
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
&& $key !== self::OVERRIDE_KEY
) {
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
$this->envOverrides[$flatKey] = $value;
}
}
}
return $this->envOverrides;
}
/**
* Array keys conversion
*
* Convert associative array of arbitrary depth to a flat associative array with concatenated key path as keys
* each level of array is accessible by path key
*
* @param array $params
* @param string|null $path
* @param array|null $flattenResult
* @return array
* @throws RuntimeException
*/
private function flattenParams(array $params, ?string $path = null, array &$flattenResult = null): array
{
if (null === $flattenResult) {
$flattenResult = [];
}
foreach ($params as $key => $param) {
if ($path) {
$newPath = $path . '/' . $key;
} else {
$newPath = $key;
}
if (isset($flattenResult[$newPath])) {
//phpcs:ignore Magento2.Exceptions.DirectThrow
throw new RuntimeException(new Phrase("Key collision '%1' is already defined.", [$newPath]));
}
if (is_array($param)) {
$flattenResult[$newPath] = $param;
$this->flattenParams($param, $newPath, $flattenResult);
} else {
// allow reading values from env variables
// value need to be specified in %env(NAME, "default value")% format
// like #env(DB_PASSWORD), #env(DB_NAME, "test")
if ($param !== null && preg_match(self::ENV_NAME_PATTERN, $param, $matches)) {
$param = getenv($matches['name']) ?: ($matches['default'] ?? null);
}
$flattenResult[$newPath] = $param;
}
}
return $flattenResult;
}
/**
* Returns flat data by key
*
* @param string|null $key
* @return mixed|null
*/
private function getByKey(?string $key)
{
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
return '';
}
return $this->flatData[$key] ?? null;
}
/**
* Returns data by key
*
* @param string|null $key
* @return mixed|null
*/
private function getConfigDataByKey(?string $key)
{
return $this->data[$key] ?? null;
}
/**
* Disable show internals with var_dump
*
* @see https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo
* @return array
*/
public function __debugInfo()
{
return [];
}
}