Skip to content

Commit e35f352

Browse files
committed
Merge branch '33802' of github.com:sergeynezbritskiy/magento2 into 2.4-develop-prs
2 parents 093ddc6 + 212b6cf commit e35f352

File tree

2 files changed

+179
-104
lines changed

2 files changed

+179
-104
lines changed

lib/internal/Magento/Framework/App/DeploymentConfig.php

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class DeploymentConfig
3535
*
3636
* @var array
3737
*/
38-
private $data;
38+
private $data = [];
3939

4040
/**
4141
* Flattened data
4242
*
4343
* @var array
4444
*/
45-
private $flatData;
45+
private $flatData = [];
4646

4747
/**
4848
* Injected configuration data
@@ -76,16 +76,18 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
7676
*/
7777
public function get($key = null, $defaultValue = null)
7878
{
79-
$this->load();
8079
if ($key === null) {
80+
if (empty($this->flatData)) {
81+
$this->reloadData();
82+
}
8183
return $this->flatData;
8284
}
83-
84-
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
85-
return '';
85+
$result = $this->getByKey($key);
86+
if ($result === null) {
87+
$this->reloadData();
88+
$result = $this->getByKey($key);
8689
}
87-
88-
return $this->flatData[$key] ?? $defaultValue;
90+
return $result ?? $defaultValue;
8991
}
9092

9193
/**
@@ -97,27 +99,31 @@ public function get($key = null, $defaultValue = null)
9799
*/
98100
public function isAvailable()
99101
{
100-
$this->load();
101-
return isset($this->flatData[ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE]);
102+
return $this->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) !== null;
102103
}
103104

104105
/**
105106
* Gets a value specified key from config data
106107
*
107-
* @param string $key
108+
* @param string|null $key
108109
* @return null|mixed
109110
* @throws FileSystemException
110111
* @throws RuntimeException
111112
*/
112113
public function getConfigData($key = null)
113114
{
114-
$this->load();
115-
116-
if ($key !== null && !isset($this->data[$key])) {
117-
return null;
115+
if ($key === null) {
116+
if (empty($this->data)) {
117+
$this->reloadData();
118+
}
119+
return $this->data;
118120
}
119-
120-
return $this->data[$key] ?? $this->data;
121+
$result = $this->getConfigDataByKey($key);
122+
if ($result === null) {
123+
$this->reloadData();
124+
$result = $this->getConfigDataByKey($key);
125+
}
126+
return $result;
121127
}
122128

123129
/**
@@ -127,7 +133,8 @@ public function getConfigData($key = null)
127133
*/
128134
public function resetData()
129135
{
130-
$this->data = null;
136+
$this->data = [];
137+
$this->flatData = [];
131138
}
132139

133140
/**
@@ -140,8 +147,7 @@ public function resetData()
140147
*/
141148
public function isDbAvailable()
142149
{
143-
$this->load();
144-
return isset($this->data['db']);
150+
return $this->getConfigData('db') !== null;
145151
}
146152

147153
/**
@@ -164,28 +170,26 @@ private function getEnvOverride() : array
164170
* @throws FileSystemException
165171
* @throws RuntimeException
166172
*/
167-
private function load()
173+
private function reloadData(): void
168174
{
169-
if (empty($this->data)) {
170-
$this->data = array_replace(
171-
$this->reader->load(),
172-
$this->overrideData ?? [],
173-
$this->getEnvOverride()
174-
);
175-
// flatten data for config retrieval using get()
176-
$this->flatData = $this->flattenParams($this->data);
177-
178-
// allow reading values from env variables by convention
179-
// MAGENTO_DC_{path}, like db/connection/default/host =>
180-
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
181-
foreach (getenv() as $key => $value) {
182-
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
183-
&& $key !== self::OVERRIDE_KEY
184-
) {
185-
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
186-
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
187-
$this->flatData[$flatKey] = $value;
188-
}
175+
$this->data = array_replace(
176+
$this->reader->load(),
177+
$this->overrideData ?? [],
178+
$this->getEnvOverride()
179+
);
180+
// flatten data for config retrieval using get()
181+
$this->flatData = $this->flattenParams($this->data);
182+
183+
// allow reading values from env variables by convention
184+
// MAGENTO_DC_{path}, like db/connection/default/host =>
185+
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
186+
foreach (getenv() as $key => $value) {
187+
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
188+
&& $key !== self::OVERRIDE_KEY
189+
) {
190+
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
191+
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
192+
$this->flatData[$flatKey] = $value;
189193
}
190194
}
191195
}
@@ -197,12 +201,12 @@ private function load()
197201
* each level of array is accessible by path key
198202
*
199203
* @param array $params
200-
* @param string $path
201-
* @param array $flattenResult
204+
* @param string|null $path
205+
* @param array|null $flattenResult
202206
* @return array
203207
* @throws RuntimeException
204208
*/
205-
private function flattenParams(array $params, $path = null, array &$flattenResult = null) : array
209+
private function flattenParams(array $params, ?string $path = null, array &$flattenResult = null): array
206210
{
207211
if (null === $flattenResult) {
208212
$flattenResult = [];
@@ -236,4 +240,30 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
236240

237241
return $flattenResult;
238242
}
243+
244+
/**
245+
* Returns flat data by key
246+
*
247+
* @param string|null $key
248+
* @return mixed|null
249+
*/
250+
private function getByKey(?string $key)
251+
{
252+
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
253+
return '';
254+
}
255+
256+
return $this->flatData[$key] ?? null;
257+
}
258+
259+
/**
260+
* Returns data by key
261+
*
262+
* @param string|null $key
263+
* @return mixed|null
264+
*/
265+
private function getConfigDataByKey(?string $key)
266+
{
267+
return $this->data[$key] ?? null;
268+
}
239269
}

0 commit comments

Comments
 (0)