Skip to content

Commit 6bc82de

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-1958-2.4.7-beta1-develop' into PR06022023_2.4.7-beta1-develop
2 parents 3be3fb2 + 9521e87 commit 6bc82de

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

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

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ class DeploymentConfig
5151
*/
5252
private $overrideData;
5353

54+
/**
55+
* @var array
56+
*/
57+
private $envOverrides = [];
58+
59+
/**
60+
* @var array
61+
*/
62+
private $readerLoad = [];
63+
5464
/**
5565
* Constructor
5666
*
@@ -84,7 +94,9 @@ public function get($key = null, $defaultValue = null)
8494
}
8595
$result = $this->getByKey($key);
8696
if ($result === null) {
87-
$this->reloadData();
97+
if (empty($this->flatData) || count($this->getAllEnvOverrides())) {
98+
$this->reloadData();
99+
}
88100
$result = $this->getByKey($key);
89101
}
90102
return $result ?? $defaultValue;
@@ -114,13 +126,13 @@ public function getConfigData($key = null)
114126
{
115127
if ($key === null) {
116128
if (empty($this->data)) {
117-
$this->reloadData();
129+
$this->reloadInitialData();
118130
}
119131
return $this->data;
120132
}
121133
$result = $this->getConfigDataByKey($key);
122134
if ($result === null) {
123-
$this->reloadData();
135+
$this->reloadInitialData();
124136
$result = $this->getConfigDataByKey($key);
125137
}
126138
return $result;
@@ -170,28 +182,55 @@ private function getEnvOverride() : array
170182
* @throws FileSystemException
171183
* @throws RuntimeException
172184
*/
173-
private function reloadData(): void
185+
private function reloadInitialData(): void
174186
{
187+
if (empty($this->readerLoad) || empty($this->data) || empty($this->flatData)) {
188+
$this->readerLoad = $this->reader->load();
189+
}
175190
$this->data = array_replace(
176-
$this->reader->load(),
191+
$this->readerLoad,
177192
$this->overrideData ?? [],
178193
$this->getEnvOverride()
179194
);
195+
}
196+
197+
/**
198+
* Loads the configuration data
199+
*
200+
* @return void
201+
* @throws FileSystemException
202+
* @throws RuntimeException
203+
*/
204+
private function reloadData(): void
205+
{
206+
$this->reloadInitialData();
180207
// flatten data for config retrieval using get()
181208
$this->flatData = $this->flattenParams($this->data);
209+
$this->flatData = $this->getAllEnvOverrides() + $this->flatData;
210+
}
182211

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;
212+
/**
213+
* Load all getenv() configs once
214+
*
215+
* @return array
216+
*/
217+
private function getAllEnvOverrides(): array
218+
{
219+
if (empty($this->envOverrides)) {
220+
// allow reading values from env variables by convention
221+
// MAGENTO_DC_{path}, like db/connection/default/host =>
222+
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
223+
foreach (getenv() as $key => $value) {
224+
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
225+
&& $key !== self::OVERRIDE_KEY
226+
) {
227+
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
228+
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
229+
$this->envOverrides[$flatKey] = $value;
230+
}
193231
}
194232
}
233+
return $this->envOverrides;
195234
}
196235

197236
/**

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfigTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ class DeploymentConfigTest extends TestCase
5050
'test_override' => 'overridden',
5151
];
5252

53+
/**
54+
* @var array
55+
*/
56+
private static $flattenedFixtureSecond
57+
= [
58+
'test_override' => 'overridden2'
59+
];
60+
5361
/**
5462
* @var array
5563
*/
@@ -112,6 +120,25 @@ public function testGetters(): void
112120
$this->assertSame('overridden', $this->deploymentConfig->get('test_override'));
113121
}
114122

123+
/**
124+
* @return void
125+
* @throws FileSystemException
126+
* @throws RuntimeException
127+
*/
128+
public function testGettersReloadConfig(): void
129+
{
130+
$this->readerMock->expects($this->any())->method('load')->willReturn(self::$flattenedFixtureSecond);
131+
$this->deploymentConfig = new DeploymentConfig(
132+
$this->readerMock,
133+
['test_override' => 'overridden2']
134+
);
135+
$this->assertNull($this->deploymentConfig->get('invalid_key'));
136+
$this->assertNull($this->deploymentConfig->getConfigData('invalid_key'));
137+
putenv('MAGENTO_DC_A=abc');
138+
$this->assertSame('abc', $this->deploymentConfig->get('a'));
139+
$this->assertSame('overridden2', $this->deploymentConfig->get('test_override'));
140+
}
141+
115142
/**
116143
* @return void
117144
* @throws FileSystemException
@@ -149,7 +176,7 @@ public function testNotAvailable(): void
149176
*/
150177
public function testNotAvailableThenAvailable(): void
151178
{
152-
$this->readerMock->expects($this->exactly(2))->method('load')->willReturn(['Test']);
179+
$this->readerMock->expects($this->exactly(1))->method('load')->willReturn(['Test']);
153180
$object = new DeploymentConfig($this->readerMock);
154181
$this->assertFalse($object->isAvailable());
155182
$this->assertFalse($object->isAvailable());

lib/internal/Magento/Framework/Module/ModuleList.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ public function isModuleInfoAvailable()
140140
*/
141141
private function loadConfigData()
142142
{
143-
if (null === $this->configData && null !== $this->config->get(ConfigOptionsListConstants::KEY_MODULES)) {
144-
$this->configData = $this->config->get(ConfigOptionsListConstants::KEY_MODULES);
143+
if (null === $this->configData) {
144+
$config = $this->config->get(ConfigOptionsListConstants::KEY_MODULES);
145+
if (null !== $config) {
146+
$this->configData = $config;
147+
}
145148
}
146149
}
147150

lib/internal/Magento/Framework/Module/Test/Unit/ModuleListTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testIsModuleInfoAvailableNoConfig(): void
129129
{
130130
$this->config
131131
->method('get')
132-
->willReturnOnConsecutiveCalls(['modules' => 'testModule'], null);
132+
->willReturnOnConsecutiveCalls(null);
133133
$this->assertFalse($this->model->isModuleInfoAvailable());
134134
}
135135

@@ -144,7 +144,7 @@ public function testIsModuleInfoAvailableNoConfig(): void
144144
private function setLoadConfigExpectation($isExpected = true): void
145145
{
146146
if ($isExpected) {
147-
$this->config->expects($this->exactly(2))->method('get')->willReturn(self::$enabledFixture);
147+
$this->config->expects($this->once())->method('get')->willReturn(self::$enabledFixture);
148148
} else {
149149
$this->config->expects($this->never())->method('get');
150150
}

0 commit comments

Comments
 (0)