Skip to content

Commit 9b839c0

Browse files
alzotao-iegorov
authored andcommitted
ACP2E-1958: avoid reloading configs for each requested key; added unit tests
1 parent 79ba795 commit 9b839c0

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

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

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

54+
/**
55+
* @var array
56+
*/
57+
private $envOverrides = [];
58+
5459
/**
5560
* Constructor
5661
*
@@ -76,6 +81,7 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
7681
*/
7782
public function get($key = null, $defaultValue = null)
7883
{
84+
//if no key is requested, then all config is returned.
7985
if ($key === null) {
8086
if (empty($this->flatData)) {
8187
$this->reloadData();
@@ -84,7 +90,7 @@ public function get($key = null, $defaultValue = null)
8490
}
8591
$result = $this->getByKey($key);
8692
if ($result === null) {
87-
if (empty($this->flatData)) {
93+
if (empty($this->flatData) || count($this->getAllEnvOverrides())) {
8894
$this->reloadData();
8995
}
9096
$result = $this->getByKey($key);
@@ -181,19 +187,29 @@ private function reloadData(): void
181187
);
182188
// flatten data for config retrieval using get()
183189
$this->flatData = $this->flattenParams($this->data);
190+
$this->flatData = $this->getAllEnvOverrides() + $this->flatData;
191+
}
184192

185-
// allow reading values from env variables by convention
186-
// MAGENTO_DC_{path}, like db/connection/default/host =>
187-
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
188-
foreach (getenv() as $key => $value) {
189-
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
190-
&& $key !== self::OVERRIDE_KEY
191-
) {
192-
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
193-
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
194-
$this->flatData[$flatKey] = $value;
193+
/**
194+
* @return array
195+
*/
196+
private function getAllEnvOverrides(): array
197+
{
198+
if (empty($this->envOverrides)) {
199+
// allow reading values from env variables by convention
200+
// MAGENTO_DC_{path}, like db/connection/default/host =>
201+
// can be overwritten by MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST
202+
foreach (getenv() as $key => $value) {
203+
if (false !== \strpos($key, self::MAGENTO_ENV_PREFIX)
204+
&& $key !== self::OVERRIDE_KEY
205+
) {
206+
// convert MAGENTO_DC_DB__CONNECTION__DEFAULT__HOST into db/connection/default/host
207+
$flatKey = strtolower(str_replace([self::MAGENTO_ENV_PREFIX, '__'], ['', '/'], $key));
208+
$this->envOverrides[$flatKey] = $value;
209+
}
195210
}
196211
}
212+
return $this->envOverrides;
197213
}
198214

199215
/**

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());

0 commit comments

Comments
 (0)