From a9fe11c8a75a3611fd8f1b9aaab9be65e87fe0d3 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sun, 29 Dec 2024 13:16:50 +0100
Subject: [PATCH 01/29] fix: set cache when env loader overrides data
---
.../Core/Helper/EnvironmentConfigLoader.php | 31 +++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 33d78ea3d2b..675dabca7d5 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -63,15 +63,32 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
case static::CONFIG_KEY_DEFAULT:
[$unused1, $unused2, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
- $xmlConfig->setNode($this->buildNodePath($scope, $path), $value);
+ $nodePath = $this->buildNodePath($scope, $path);
+ $xmlConfig->setNode($nodePath, $value);
+ try {
+ $store = Mage::app()->getStore(0);
+ $this->setCache($store, $value, $path);
+ } catch (Throwable $exception) {
+ Mage::logException($exception);
+ }
break;
case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
[$unused1, $unused2, $code, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
- $nodePath = sprintf('%s/%s/%s', strtolower($scope), strtolower($code), $path);
+ $storeCode = strtolower($storeCode);
+ $scope = strtolower($scope);
+ $nodePath = sprintf('%s/%s/%s', $scope, $storeCode, $path);
$xmlConfig->setNode($nodePath, $value);
+ try {
+ if (!str_contains($nodePath, 'websites')) {
+ $store = Mage::app()->getStore($storeCode);
+ $this->setCache($store, $value, $path);
+ }
+ } catch (Throwable $exception) {
+ Mage::logException($exception);
+ }
break;
}
}
@@ -93,6 +110,16 @@ public function getEnv(): array
return $this->envStore;
}
+ protected function setCache(Mage_Core_Model_Store $store, $value, string $path): void
+ {
+ $refObject = new ReflectionObject($store);
+ $refProperty = $refObject->getProperty('_configCache');
+ $refProperty->setAccessible(true);
+ $configCache = $refProperty->getValue($store);
+ $configCache[$path] = $value;
+ $refProperty->setValue($store, $configCache);
+ }
+
protected function getConfigKey(string $configKey): array
{
$configKeyParts = array_filter(
From 99b3d84a7a7f1eee726e2a6885a13d5050432cd1 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sat, 8 Feb 2025 15:11:59 +0100
Subject: [PATCH 02/29] feat?: call env helper on config get
---
.../Mage/Core/Helper/EnvironmentConfigLoader.php | 12 ++++++++----
app/code/core/Mage/Core/Model/Store.php | 3 +++
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 675dabca7d5..116cbeaf459 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -66,8 +66,10 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
$nodePath = $this->buildNodePath($scope, $path);
$xmlConfig->setNode($nodePath, $value);
try {
- $store = Mage::app()->getStore(0);
- $this->setCache($store, $value, $path);
+ foreach (['0', 'admin'] as $store) {
+ $store = Mage::app()->getStore($store);
+ $this->setCache($store, $value, $path);
+ }
} catch (Throwable $exception) {
Mage::logException($exception);
}
@@ -83,8 +85,10 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
$xmlConfig->setNode($nodePath, $value);
try {
if (!str_contains($nodePath, 'websites')) {
- $store = Mage::app()->getStore($storeCode);
- $this->setCache($store, $value, $path);
+ foreach ([$storeCode, 'admin'] as $store) {
+ $store = Mage::app()->getStore($store);
+ $this->setCache($store, $value, $path);
+ }
}
} catch (Throwable $exception) {
Mage::logException($exception);
diff --git a/app/code/core/Mage/Core/Model/Store.php b/app/code/core/Mage/Core/Model/Store.php
index b0670a29402..cde4b284ae5 100644
--- a/app/code/core/Mage/Core/Model/Store.php
+++ b/app/code/core/Mage/Core/Model/Store.php
@@ -336,6 +336,9 @@ public function getConfig($path)
}
$config = Mage::getConfig();
+ /** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
+ $environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
+ $environmentConfigLoaderHelper->overrideEnvironment($config);
$fullPath = 'stores/' . $this->getCode() . '/' . $path;
$data = $config->getNode($fullPath);
From e08fccfc51bb5fd045465c7fbb3d237b90c6d94a Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sat, 8 Feb 2025 15:47:53 +0100
Subject: [PATCH 03/29] feat?: backend: show data from env
---
.../core/Mage/Adminhtml/Model/Config/Data.php | 7 ++++
.../Core/Helper/EnvironmentConfigLoader.php | 36 +++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/app/code/core/Mage/Adminhtml/Model/Config/Data.php b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
index edc85b37bda..c73eb313358 100644
--- a/app/code/core/Mage/Adminhtml/Model/Config/Data.php
+++ b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
@@ -341,6 +341,13 @@ protected function _getPathConfig($path, $full = true)
$config[$data->getPath()] = $data->getValue();
}
}
+
+ if (!$full) {
+ /** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
+ $environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
+ $envConfig = $environmentConfigLoaderHelper->getAsArray($scope = $this->getScope());
+ $config = array_merge($config, $envConfig);
+ }
return $config;
}
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 116cbeaf459..76f03204731 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -98,6 +98,42 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
}
}
+ public function getAsArray(string $wantedScope): array
+ {
+ $env = $this->getEnv();
+ $config = [];
+
+ foreach ($env as $configKey => $value) {
+ if (!$this->isConfigKeyValid($configKey)) {
+ continue;
+ }
+
+ list($configKeyParts, $scope) = $this->getConfigKey($configKey);
+ if (strtolower($scope) !== strtolower($wantedScope)) {
+ continue;
+ }
+
+ switch ($scope) {
+ case static::CONFIG_KEY_DEFAULT:
+ list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
+ $path = $this->buildPath($section, $group, $field);
+ $config[$path] = $value;
+ break;
+
+ case static::CONFIG_KEY_WEBSITES:
+ case static::CONFIG_KEY_STORES:
+ list($unused1, $unused2, $storeCode, $section, $group, $field) = $configKeyParts;
+ $path = $this->buildPath($section, $group, $field);
+ $storeCode = strtolower($storeCode);
+ $scope = strtolower($scope);
+ $config[$path] = $value;
+ break;
+ }
+ }
+
+ return $config;
+ }
+
/**
* @internal method mostly for mocking
*/
From 5d22f56c221e5e0739b745a784db90a6389f5cb0 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sat, 8 Feb 2025 16:07:00 +0100
Subject: [PATCH 04/29] feat?: backend: disable field when ENV data is
available
---
.../Adminhtml/Block/System/Config/Form.php | 15 ++++++++
.../Core/Helper/EnvironmentConfigLoader.php | 35 +++++++++++++++++--
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index 9c1e38d14f8..fcc7e6df229 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -372,6 +372,7 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
'name' => $name,
'label' => $label,
'comment' => $comment,
+ 'disabled' => $this->isDisabled($path),
'tooltip' => $tooltip,
'hint' => $hint,
'value' => $data,
@@ -622,6 +623,20 @@ public function getScope()
return $scope;
}
+ /**
+ * Render element as disabled, if overwritten by ENV variable
+ *
+ * @param string $data
+ * @return bool
+ */
+ public function isDisabled($path): bool
+ {
+ /** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
+ $environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
+ $path = $this->getScope() . '/' . $path;
+ return $environmentConfigLoaderHelper->hasPath($path);
+ }
+
/**
* Retrieve label for scope
*
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 76f03204731..8ff3bafd712 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -98,6 +98,39 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
}
}
+ public function hasPath(string $wantedPath): bool
+ {
+ $env = $this->getEnv();
+ $config = [];
+
+ foreach ($env as $configKey => $value) {
+ if (!$this->isConfigKeyValid($configKey)) {
+ continue;
+ }
+
+ list($configKeyParts, $scope) = $this->getConfigKey($configKey);
+
+ switch ($scope) {
+ case static::CONFIG_KEY_DEFAULT:
+ list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
+ $path = $this->buildPath($section, $group, $field);
+ $nodePath = $this->buildNodePath($scope, $path);
+ $config[$nodePath] = $value;
+ break;
+
+ case static::CONFIG_KEY_WEBSITES:
+ case static::CONFIG_KEY_STORES:
+ list($unused1, $unused2, $storeCode, $section, $group, $field) = $configKeyParts;
+ $path = $this->buildPath($section, $group, $field);
+ $nodePath = $this->buildNodePath($scope, $path);
+ $config[$nodePath] = $value;
+ break;
+ }
+ }
+ $hasConfig = array_key_exists($wantedPath, $config);
+ return $hasConfig;
+ }
+
public function getAsArray(string $wantedScope): array
{
$env = $this->getEnv();
@@ -124,8 +157,6 @@ public function getAsArray(string $wantedScope): array
case static::CONFIG_KEY_STORES:
list($unused1, $unused2, $storeCode, $section, $group, $field) = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
- $storeCode = strtolower($storeCode);
- $scope = strtolower($scope);
$config[$path] = $value;
break;
}
From ecd1157a773a3b87eeaa54c2524c18a2ffe131ed Mon Sep 17 00:00:00 2001
From: Sven Reichel
Date: Sat, 8 Feb 2025 23:49:03 +0100
Subject: [PATCH 05/29] Update
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
---
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index fcc7e6df229..fded9fb9470 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -625,11 +625,8 @@ public function getScope()
/**
* Render element as disabled, if overwritten by ENV variable
- *
- * @param string $data
- * @return bool
*/
- public function isDisabled($path): bool
+ public function isDisabled(string $path): bool
{
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
From 2bd48b9eaa73c666bbef8bf20c30aea67c7deb15 Mon Sep 17 00:00:00 2001
From: Sven Reichel
Date: Sun, 9 Feb 2025 00:00:40 +0100
Subject: [PATCH 06/29] Update
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
---
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 8ff3bafd712..ca1993b6897 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -127,8 +127,7 @@ public function hasPath(string $wantedPath): bool
break;
}
}
- $hasConfig = array_key_exists($wantedPath, $config);
- return $hasConfig;
+ return array_key_exists($wantedPath, $config);
}
public function getAsArray(string $wantedScope): array
From d9b0faab8b5472b0b52c1d194c12dd55fb7aa908 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sun, 9 Feb 2025 09:03:44 +0100
Subject: [PATCH 07/29] feat?: backend: do not log on invalid stores - creates
infinite loop
---
.../Core/Helper/EnvironmentConfigLoader.php | 23 +++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index ca1993b6897..156530f23c3 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -50,6 +50,10 @@ class Mage_Core_Helper_EnvironmentConfigLoader extends Mage_Core_Helper_Abstract
*/
public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
{
+ $data = Mage::registry('current_env_config');
+ if ($data) {
+ return;
+ }
$env = $this->getEnv();
foreach ($env as $configKey => $value) {
@@ -71,7 +75,7 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
$this->setCache($store, $value, $path);
}
} catch (Throwable $exception) {
- Mage::logException($exception);
+ // invalid store, intentionally empty
}
break;
@@ -91,15 +95,20 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
}
}
} catch (Throwable $exception) {
- Mage::logException($exception);
+ // invalid store, intentionally empty
}
break;
}
}
+ Mage::register("current_env_config", true, true);
}
public function hasPath(string $wantedPath): bool
{
+ $data = Mage::registry("config_env_has_path_$wantedPath");
+ if ($data !== null) {
+ return $data;
+ }
$env = $this->getEnv();
$config = [];
@@ -127,11 +136,17 @@ public function hasPath(string $wantedPath): bool
break;
}
}
- return array_key_exists($wantedPath, $config);
+ $hasConfig = array_key_exists($wantedPath, $config);
+ Mage::register("config_env_has_path_$wantedPath", $hasConfig);
+ return $hasConfig;
}
public function getAsArray(string $wantedScope): array
{
+ $data = Mage::registry("config_env_array_$wantedScope");
+ if ($data !== null) {
+ return $data;
+ }
$env = $this->getEnv();
$config = [];
@@ -160,7 +175,7 @@ public function getAsArray(string $wantedScope): array
break;
}
}
-
+ Mage::register("config_env_array_$wantedScope", $config);
return $config;
}
From 69ac537a7349009137ff3fb02a59e88ddef96969 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 21:08:31 +0100
Subject: [PATCH 08/29] feat: move env variables "str_starts_with"
---
.../core/Mage/Core/Helper/EnvironmentConfigLoader.php | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 156530f23c3..683d9982325 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -190,7 +190,11 @@ public function setEnvStore(array $envStorage): void
public function getEnv(): array
{
if (empty($this->envStore)) {
- $this->envStore = getenv();
+ $env = getenv();
+ $env = array_filter($env, function ($key) {
+ return str_starts_with($key, static::ENV_STARTS_WITH);
+ }, ARRAY_FILTER_USE_KEY);
+ $this->envStore = $env;
}
return $this->envStore;
}
@@ -220,10 +224,6 @@ protected function getConfigKey(string $configKey): array
protected function isConfigKeyValid(string $configKey): bool
{
- if (!str_starts_with($configKey, static::ENV_STARTS_WITH)) {
- return false;
- }
-
$sectionGroupFieldRegexp = sprintf('([%s]*)', implode('', static::ALLOWED_CHARS));
$allowedChars = sprintf('[%s]', implode('', static::ALLOWED_CHARS));
$regexp = '/' . static::ENV_STARTS_WITH . static::ENV_KEY_SEPARATOR . '(WEBSITES' . static::ENV_KEY_SEPARATOR
From 2f3402fd47d5bf8a9bfd9af5d46d91a3c596082e Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 22:06:02 +0100
Subject: [PATCH 09/29] feat: add env flag to disable/enable feature
---
.../Core/Helper/EnvironmentConfigLoader.php | 5 ++++
.../Helper/EnvironmentConfigLoaderTest.php | 27 +++++++++++++++++++
2 files changed, 32 insertions(+)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 683d9982325..f89338a72bb 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -15,6 +15,7 @@
class Mage_Core_Helper_EnvironmentConfigLoader extends Mage_Core_Helper_Abstract
{
protected const ENV_STARTS_WITH = 'OPENMAGE_CONFIG';
+ protected const ENV_FEATURE_ENABLED = 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED';
protected const ENV_KEY_SEPARATOR = '__';
protected const CONFIG_KEY_DEFAULT = 'DEFAULT';
protected const CONFIG_KEY_WEBSITES = 'WEBSITES';
@@ -196,6 +197,10 @@ public function getEnv(): array
}, ARRAY_FILTER_USE_KEY);
$this->envStore = $env;
}
+ if (!isset($this->envStore[static::ENV_FEATURE_ENABLED])) {
+ $this->envStore = [];
+ return $this->envStore;
+ }
return $this->envStore;
}
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 2029fe89bde..6cfc4920f2f 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -18,6 +18,9 @@
use OpenMage\Tests\Unit\OpenMageTest;
use Varien_Simplexml_Config;
+/**
+ * @group Mage_Core_EnvLoader
+ */
class EnvironmentConfigLoaderTest extends OpenMageTest
{
public const XML_PATH_GENERAL = 'general/store_information/name';
@@ -49,6 +52,30 @@ public function testBuildPath(): void
/**
* @group Helper
*/
+ public function testEnvFilter(): void
+ {
+ $environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
+ $environmentConfigLoaderHelper->setEnvStore([
+ 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => "some_value"
+ ]);
+ // empty because env flag is not set
+ $env = $environmentConfigLoaderHelper->getEnv();
+ $this->assertIsArray($env);
+ $this->assertEmpty($env);
+ $environmentConfigLoaderHelper->setEnvStore([
+ 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => "some_value",
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1 // enable feature
+ ]);
+ // flag is set => feature is enabled
+ $env = $environmentConfigLoaderHelper->getEnv();
+ $this->assertIsArray($env);
+ $this->assertNotEmpty($env);
+ }
+
+ /**
+ * @group Mage_Core
+ * @group Mage_Core_Helper
+ */
public function testBuildNodePath(): void
{
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
From b65cf705672dd91077987744d42e364f8f4b4e10 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 22:15:10 +0100
Subject: [PATCH 10/29] fix: fix tests
---
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 6cfc4920f2f..391a25d09b9 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -97,6 +97,7 @@ public function testXmlHasTestStrings(): void
}
/**
+ * @runInSeparateProcess
* @dataProvider envOverridesCorrectConfigKeysDataProvider
* @group Helper
*
@@ -114,6 +115,7 @@ public function testEnvOverridesForValidConfigKeys(array $config): void
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
/** @phpstan-ignore method.internal */
$loader->setEnvStore([
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['env_path'] => $config['value'],
]);
$loader->overrideEnvironment($xml);
@@ -197,6 +199,7 @@ public function envOverridesCorrectConfigKeysDataProvider(): Generator
}
/**
+ * @runInSeparateProcess
* @dataProvider envDoesNotOverrideOnWrongConfigKeysDataProvider
* @group Helper
*
@@ -221,6 +224,7 @@ public function testEnvDoesNotOverrideForInvalidConfigKeys(array $config): void
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
/** @phpstan-ignore method.internal */
$loader->setEnvStore([
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['path'] => $config['value'],
]);
$loader->overrideEnvironment($xml);
From d8927bcc08c17cbc8d8ac28df4ad24878ff48ba9 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 23:13:25 +0100
Subject: [PATCH 11/29] feat: set disabled, set scope label
---
.../Adminhtml/Block/System/Config/Form.php | 21 ++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index fded9fb9470..3c3a16ffbc5 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -17,6 +17,7 @@ class Mage_Adminhtml_Block_System_Config_Form extends Mage_Adminhtml_Block_Widge
public const SCOPE_DEFAULT = 'default';
public const SCOPE_WEBSITES = 'websites';
public const SCOPE_STORES = 'stores';
+ public const SCOPE_ENV = 'env';
/**
* Config data array
@@ -71,6 +72,7 @@ public function __construct()
self::SCOPE_DEFAULT => Mage::helper('adminhtml')->__('[GLOBAL]'),
self::SCOPE_WEBSITES => Mage::helper('adminhtml')->__('[WEBSITE]'),
self::SCOPE_STORES => Mage::helper('adminhtml')->__('[STORE VIEW]'),
+ self::SCOPE_ENV => Mage::helper('adminhtml')->__('[ENV]'),
];
}
@@ -368,11 +370,10 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
}
}
- $field = $fieldset->addField($id, $fieldType, [
+ $elementFieldData = [
'name' => $name,
'label' => $label,
'comment' => $comment,
- 'disabled' => $this->isDisabled($path),
'tooltip' => $tooltip,
'hint' => $hint,
'value' => $data,
@@ -383,9 +384,14 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
'scope' => $this->getScope(),
'scope_id' => $this->getScopeId(),
'scope_label' => $this->getScopeLabel($element),
- 'can_use_default_value' => $this->canUseDefaultValue((int) $element->show_in_default),
- 'can_use_website_value' => $this->canUseWebsiteValue((int) $element->show_in_website),
- ]);
+ 'can_use_default_value' => $this->canUseDefaultValue((int)$element->show_in_default),
+ 'can_use_website_value' => $this->canUseWebsiteValue((int)$element->show_in_website),
+ ];
+ if ($this->isOverwrittenByEnvVariable($path)) {
+ $elementFieldData['scope_label'] = $this->_scopeLabels[static::SCOPE_ENV];
+ $elementFieldData['disabled'] = 1;
+ }
+ $field = $fieldset->addField($id, $fieldType, $elementFieldData);
$this->_prepareFieldOriginalData($field, $element);
if (isset($element->validate)) {
@@ -624,9 +630,10 @@ public function getScope()
}
/**
- * Render element as disabled, if overwritten by ENV variable
+ * Returns true if element was overwritten by ENV variable
+ * @return bool
*/
- public function isDisabled(string $path): bool
+ public function isOverwrittenByEnvVariable(string $path): bool
{
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
From 9fbc1a6c582b5d64ee58e9f3055c255bd716c9cf Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 23:15:41 +0100
Subject: [PATCH 12/29] feat: remove checkbox to override
---
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php | 2 ++
1 file changed, 2 insertions(+)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index 3c3a16ffbc5..f7a3138a4b4 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -390,6 +390,8 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
if ($this->isOverwrittenByEnvVariable($path)) {
$elementFieldData['scope_label'] = $this->_scopeLabels[static::SCOPE_ENV];
$elementFieldData['disabled'] = 1;
+ $elementFieldData['can_use_default_value'] = 0;
+ $elementFieldData['can_use_website_value'] = 0;
}
$field = $fieldset->addField($id, $fieldType, $elementFieldData);
$this->_prepareFieldOriginalData($field, $element);
From 8362d2c6c54462d5155c427f325b2ec776a9e551 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 17 Feb 2025 23:35:56 +0100
Subject: [PATCH 13/29] chore: remove inline helper variable name
---
app/code/core/Mage/Adminhtml/Model/Config/Data.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/code/core/Mage/Adminhtml/Model/Config/Data.php b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
index c73eb313358..a8cf1fb6e21 100644
--- a/app/code/core/Mage/Adminhtml/Model/Config/Data.php
+++ b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
@@ -345,7 +345,7 @@ protected function _getPathConfig($path, $full = true)
if (!$full) {
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
- $envConfig = $environmentConfigLoaderHelper->getAsArray($scope = $this->getScope());
+ $envConfig = $environmentConfigLoaderHelper->getAsArray($this->getScope());
$config = array_merge($config, $envConfig);
}
return $config;
From de4e7fcd18a0e5b7facbbd50791bde4b14b92535 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 00:19:35 +0100
Subject: [PATCH 14/29] Update
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
Co-authored-by: Sven Reichel
---
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 391a25d09b9..1b5a0df0a28 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -56,7 +56,7 @@ public function testEnvFilter(): void
{
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
$environmentConfigLoaderHelper->setEnvStore([
- 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => "some_value"
+ 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value'
]);
// empty because env flag is not set
$env = $environmentConfigLoaderHelper->getEnv();
From c26f3843bc367805060569b19227f067866081e5 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 00:19:56 +0100
Subject: [PATCH 15/29] Update
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
Co-authored-by: Sven Reichel
---
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 1b5a0df0a28..ae1889bd84b 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -63,8 +63,8 @@ public function testEnvFilter(): void
$this->assertIsArray($env);
$this->assertEmpty($env);
$environmentConfigLoaderHelper->setEnvStore([
- 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => "some_value",
- 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1 // enable feature
+ 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value',
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1, // enable feature
]);
// flag is set => feature is enabled
$env = $environmentConfigLoaderHelper->getEnv();
From c6c9422ed1f3de811c6687278386f47d7c9998c6 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 00:20:38 +0100
Subject: [PATCH 16/29] Update
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
Co-authored-by: Sven Reichel
---
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index f89338a72bb..74c51b30372 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -101,7 +101,7 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
break;
}
}
- Mage::register("current_env_config", true, true);
+ Mage::register('current_env_config', true, true);
}
public function hasPath(string $wantedPath): bool
From cfeb9dd03546d1dbc7fae47223ebe8e8d67c7b31 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 00:20:53 +0100
Subject: [PATCH 17/29] Update
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
Co-authored-by: Sven Reichel
---
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index f7a3138a4b4..2d42bb1ab8e 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -384,8 +384,8 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
'scope' => $this->getScope(),
'scope_id' => $this->getScopeId(),
'scope_label' => $this->getScopeLabel($element),
- 'can_use_default_value' => $this->canUseDefaultValue((int)$element->show_in_default),
- 'can_use_website_value' => $this->canUseWebsiteValue((int)$element->show_in_website),
+ 'can_use_default_value' => $this->canUseDefaultValue((int) $element->show_in_default),
+ 'can_use_website_value' => $this->canUseWebsiteValue((int) $element->show_in_website),
];
if ($this->isOverwrittenByEnvVariable($path)) {
$elementFieldData['scope_label'] = $this->_scopeLabels[static::SCOPE_ENV];
From 1642a7d598468802a1d9f6d63052e9f296237be1 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 00:28:07 +0100
Subject: [PATCH 18/29] Update
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
Co-authored-by: Sven Reichel
---
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index ae1889bd84b..b765aa554ee 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -56,7 +56,7 @@ public function testEnvFilter(): void
{
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
$environmentConfigLoaderHelper->setEnvStore([
- 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value'
+ 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value',
]);
// empty because env flag is not set
$env = $environmentConfigLoaderHelper->getEnv();
From df382c809968f122a25ef727d8af175df9a563fb Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 21:55:20 +0100
Subject: [PATCH 19/29] Update
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
Co-authored-by: Sven Reichel
---
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index 2d42bb1ab8e..48c9c0c5e68 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -633,7 +633,6 @@ public function getScope()
/**
* Returns true if element was overwritten by ENV variable
- * @return bool
*/
public function isOverwrittenByEnvVariable(string $path): bool
{
From a04943b92ab3fa3b9e94d4e0a9130f0facbcd8b4 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 23:19:24 +0100
Subject: [PATCH 20/29] feat: add "hasPath" test
---
.../Helper/EnvironmentConfigLoaderTest.php | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index b765aa554ee..d3b223886b0 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -198,6 +198,44 @@ public function envOverridesCorrectConfigKeysDataProvider(): Generator
]];
}
+ /**
+ * @runInSeparateProcess
+ * @dataProvider envHasPathDataProvider
+ * @group Mage_Core
+ *
+ * @param array $config
+ */
+ public function testHasPath(array $config): void
+ {
+ // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
+ $loader = new Mage_Core_Helper_EnvironmentConfigLoader();
+ $loader->setEnvStore([
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
+ $config['env_path'] => 1,
+ ]);
+ $actual = $loader->hasPath($config['xml_path']);
+ $expected = $config['expected'];
+ $this->assertSame($expected, $actual);
+ }
+
+ public function envHasPathDataProvider(): Generator
+ {
+ yield 'hasPath' => [
+ [
+ 'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
+ 'xml_path' => 'default/general/store_information/name',
+ 'expected' => true,
+ ]
+ ];
+ yield 'hasNotPath' => [
+ [
+ 'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
+ 'xml_path' => 'foo/foo/foo',
+ 'expected' => false,
+ ]
+ ];
+ }
+
/**
* @runInSeparateProcess
* @dataProvider envDoesNotOverrideOnWrongConfigKeysDataProvider
From 47043183e09f997ef988fb403003ff4c6d28059f Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 23:24:32 +0100
Subject: [PATCH 21/29] feat: add "asArray" test
---
.../Helper/EnvironmentConfigLoaderTest.php | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index d3b223886b0..f649f418203 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -198,6 +198,46 @@ public function envOverridesCorrectConfigKeysDataProvider(): Generator
]];
}
+ /**
+ * @runInSeparateProcess
+ * @dataProvider envAsArrayDataProvider
+ * @group Mage_Core
+ *
+ * @param array $config
+ */
+ public function testAsArray(array $config): void
+ {
+ // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
+ $loader = new Mage_Core_Helper_EnvironmentConfigLoader();
+ $loader->setEnvStore([
+ 'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
+ $config['env_path'] => 1,
+ ]);
+ $actual = $loader->getAsArray($config['scope']);
+ $expected = $config['expected'];
+ $this->assertSame($expected, $actual);
+ }
+
+ public function envAsArrayDataProvider(): Generator
+ {
+ yield 'defaultScope' => [
+ [
+ 'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
+ 'scope' => 'default',
+ 'expected' => [
+ 'general/store_information/name' => 1,
+ ],
+ ]
+ ];
+ yield 'invalidScope' => [
+ [
+ 'env_path' => '',
+ 'scope' => 'foo',
+ 'expected' => [],
+ ]
+ ];
+ }
+
/**
* @runInSeparateProcess
* @dataProvider envHasPathDataProvider
From 59ce16b398a36adb756bb1434a9c908992b70739 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Tue, 18 Feb 2025 23:39:28 +0100
Subject: [PATCH 22/29] tests: add test cases
---
.../Helper/EnvironmentConfigLoaderTest.php | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index f649f418203..33e824c2990 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -229,6 +229,15 @@ public function envAsArrayDataProvider(): Generator
],
]
];
+ yield 'storeScope' => [
+ [
+ 'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
+ 'scope' => 'stores',
+ 'expected' => [
+ 'general/store_information/name' => 1,
+ ],
+ ]
+ ];
yield 'invalidScope' => [
[
'env_path' => '',
@@ -260,13 +269,20 @@ public function testHasPath(array $config): void
public function envHasPathDataProvider(): Generator
{
- yield 'hasPath' => [
+ yield 'hasPath default' => [
[
'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
'xml_path' => 'default/general/store_information/name',
'expected' => true,
]
];
+ yield 'hasPath store' => [
+ [
+ 'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
+ 'xml_path' => 'stores/general/store_information/name',
+ 'expected' => true,
+ ]
+ ];
yield 'hasNotPath' => [
[
'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
From 5a6f669aaa22f5dd36c9ed2ddf4fecf1223e0833 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sun, 9 Mar 2025 11:07:22 +0100
Subject: [PATCH 23/29] Update
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
Co-authored-by: Sven Reichel
---
app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 74c51b30372..746e0bc7fcf 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -197,7 +197,9 @@ public function getEnv(): array
}, ARRAY_FILTER_USE_KEY);
$this->envStore = $env;
}
- if (!isset($this->envStore[static::ENV_FEATURE_ENABLED])) {
+ if (!isset($this->envStore[static::ENV_FEATURE_ENABLED]) ||
+ (bool) $this->envStore[static::ENV_FEATURE_ENABLED] === false
+ ) {
$this->envStore = [];
return $this->envStore;
}
From 325ae2a98fc38cffbf0152f757cc3f21d9be4a46 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Sun, 9 Mar 2025 22:54:58 +0100
Subject: [PATCH 24/29] fix: onStore(re)init the env vars would be lost
---
app/code/core/Mage/Core/Model/App.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/app/code/core/Mage/Core/Model/App.php b/app/code/core/Mage/Core/Model/App.php
index 422c2830cb8..a5f10bba2e3 100644
--- a/app/code/core/Mage/Core/Model/App.php
+++ b/app/code/core/Mage/Core/Model/App.php
@@ -622,6 +622,7 @@ public function reinitStores()
*/
protected function _initStores()
{
+ Mage::unregister('current_env_config');
$this->_stores = [];
$this->_groups = [];
$this->_website = null;
From 32c813f2e1ee739a4e4deb4ff38aa9aef3f9ed5c Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 16 Jun 2025 18:13:36 +0200
Subject: [PATCH 25/29] envLoader: fix loading from scopes
---
.../Adminhtml/Block/System/Config/Form.php | 4 ++-
.../core/Mage/Adminhtml/Model/Config/Data.php | 3 +-
.../Core/Helper/EnvironmentConfigLoader.php | 33 +++++++++----------
3 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index 48c9c0c5e68..51ccf8f5a16 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -638,7 +638,9 @@ public function isOverwrittenByEnvVariable(string $path): bool
{
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
- $path = $this->getScope() . '/' . $path;
+ $store = Mage::app()->getRequest()->getParam('store');
+ $scope = $this->getScope();
+ $path = "$scope/$store/$path";
return $environmentConfigLoaderHelper->hasPath($path);
}
diff --git a/app/code/core/Mage/Adminhtml/Model/Config/Data.php b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
index a8cf1fb6e21..52fee450ee8 100644
--- a/app/code/core/Mage/Adminhtml/Model/Config/Data.php
+++ b/app/code/core/Mage/Adminhtml/Model/Config/Data.php
@@ -345,7 +345,8 @@ protected function _getPathConfig($path, $full = true)
if (!$full) {
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
- $envConfig = $environmentConfigLoaderHelper->getAsArray($this->getScope());
+ $store = $this->getStore();
+ $envConfig = $environmentConfigLoaderHelper->getAsArray($store);
$config = array_merge($config, $envConfig);
}
return $config;
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 746e0bc7fcf..6e568d65e06 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -82,7 +82,7 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
- [$unused1, $unused2, $code, $section, $group, $field] = $configKeyParts;
+ [$unused1, $unused2, $storeCode, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$storeCode = strtolower($storeCode);
$scope = strtolower($scope);
@@ -130,9 +130,11 @@ public function hasPath(string $wantedPath): bool
case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
- list($unused1, $unused2, $storeCode, $section, $group, $field) = $configKeyParts;
+ [$unused1, $unused2, $storeCode, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
- $nodePath = $this->buildNodePath($scope, $path);
+ $storeCode = strtolower($storeCode);
+ $scope = strtolower($scope);
+ $nodePath = sprintf('%s/%s/%s', $scope, $storeCode, $path);
$config[$nodePath] = $value;
break;
}
@@ -142,9 +144,9 @@ public function hasPath(string $wantedPath): bool
return $hasConfig;
}
- public function getAsArray(string $wantedScope): array
+ public function getAsArray(string $wantedStore): array
{
- $data = Mage::registry("config_env_array_$wantedScope");
+ $data = Mage::registry("config_env_array_$wantedStore");
if ($data !== null) {
return $data;
}
@@ -157,26 +159,20 @@ public function getAsArray(string $wantedScope): array
}
list($configKeyParts, $scope) = $this->getConfigKey($configKey);
- if (strtolower($scope) !== strtolower($wantedScope)) {
- continue;
- }
switch ($scope) {
- case static::CONFIG_KEY_DEFAULT:
- list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
- $path = $this->buildPath($section, $group, $field);
- $config[$path] = $value;
- break;
-
case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
- list($unused1, $unused2, $storeCode, $section, $group, $field) = $configKeyParts;
+ [$unused1, $unused2, $storeCode, $section, $group, $field] = $configKeyParts;
+ if (strtolower($storeCode) !== strtolower($wantedStore)) {
+ break;
+ }
$path = $this->buildPath($section, $group, $field);
$config[$path] = $value;
break;
}
}
- Mage::register("config_env_array_$wantedScope", $config);
+ Mage::register("config_env_array_$wantedStore", $config);
return $config;
}
@@ -212,8 +208,11 @@ protected function setCache(Mage_Core_Model_Store $store, $value, string $path):
$refProperty = $refObject->getProperty('_configCache');
$refProperty->setAccessible(true);
$configCache = $refProperty->getValue($store);
+ if (!is_array($configCache)) {
+ $configCache = [];
+ }
$configCache[$path] = $value;
- $refProperty->setValue($store, $configCache);
+ $store->setConfigCache($configCache);
}
protected function getConfigKey(string $configKey): array
From 912f557abb72f88d6d5034d35cf20cfe7df6b60c Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 16 Jun 2025 18:17:21 +0200
Subject: [PATCH 26/29] chore: run phpcs
---
.../Helper/EnvironmentConfigLoaderTest.php | 30 +++++++++----------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 33e824c2990..23f37317ab8 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -60,16 +60,16 @@ public function testEnvFilter(): void
]);
// empty because env flag is not set
$env = $environmentConfigLoaderHelper->getEnv();
- $this->assertIsArray($env);
- $this->assertEmpty($env);
+ static::assertIsArray($env);
+ static::assertEmpty($env);
$environmentConfigLoaderHelper->setEnvStore([
'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value',
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1, // enable feature
]);
// flag is set => feature is enabled
$env = $environmentConfigLoaderHelper->getEnv();
- $this->assertIsArray($env);
- $this->assertNotEmpty($env);
+ static::assertIsArray($env);
+ static::assertNotEmpty($env);
}
/**
@@ -215,7 +215,7 @@ public function testAsArray(array $config): void
]);
$actual = $loader->getAsArray($config['scope']);
$expected = $config['expected'];
- $this->assertSame($expected, $actual);
+ static::assertSame($expected, $actual);
}
public function envAsArrayDataProvider(): Generator
@@ -227,7 +227,7 @@ public function envAsArrayDataProvider(): Generator
'expected' => [
'general/store_information/name' => 1,
],
- ]
+ ],
];
yield 'storeScope' => [
[
@@ -236,14 +236,14 @@ public function envAsArrayDataProvider(): Generator
'expected' => [
'general/store_information/name' => 1,
],
- ]
+ ],
];
yield 'invalidScope' => [
[
'env_path' => '',
'scope' => 'foo',
'expected' => [],
- ]
+ ],
];
}
@@ -264,31 +264,31 @@ public function testHasPath(array $config): void
]);
$actual = $loader->hasPath($config['xml_path']);
$expected = $config['expected'];
- $this->assertSame($expected, $actual);
+ static::assertSame($expected, $actual);
}
public function envHasPathDataProvider(): Generator
{
yield 'hasPath default' => [
[
- 'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
- 'xml_path' => 'default/general/store_information/name',
- 'expected' => true,
- ]
+ 'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
+ 'xml_path' => 'default/general/store_information/name',
+ 'expected' => true,
+ ],
];
yield 'hasPath store' => [
[
'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
'xml_path' => 'stores/general/store_information/name',
'expected' => true,
- ]
+ ],
];
yield 'hasNotPath' => [
[
'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
'xml_path' => 'foo/foo/foo',
'expected' => false,
- ]
+ ],
];
}
From af92809edb2a175dd0098a58d7aaa7fe294d001f Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 16 Jun 2025 18:33:53 +0200
Subject: [PATCH 27/29] chore: phptan: add ignore line comment about internal
method
---
tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 23f37317ab8..970da795484 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -55,6 +55,7 @@ public function testBuildPath(): void
public function testEnvFilter(): void
{
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
+ /** @phpstan-ignore method.internal */
$environmentConfigLoaderHelper->setEnvStore([
'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value',
]);
@@ -62,6 +63,7 @@ public function testEnvFilter(): void
$env = $environmentConfigLoaderHelper->getEnv();
static::assertIsArray($env);
static::assertEmpty($env);
+ /** @phpstan-ignore method.internal */
$environmentConfigLoaderHelper->setEnvStore([
'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME' => 'some_value',
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1, // enable feature
@@ -209,6 +211,7 @@ public function testAsArray(array $config): void
{
// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
+ /** @phpstan-ignore method.internal */
$loader->setEnvStore([
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['env_path'] => 1,
@@ -258,6 +261,7 @@ public function testHasPath(array $config): void
{
// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
+ /** @phpstan-ignore method.internal */
$loader->setEnvStore([
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['env_path'] => 1,
From a2e51db9874f8faf18db603d63a3acf9f3d96b2d Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 16 Jun 2025 19:23:40 +0200
Subject: [PATCH 28/29] envLoader: fix tests, fix scope default
---
.../Adminhtml/Block/System/Config/Form.php | 8 +++--
.../Core/Helper/EnvironmentConfigLoader.php | 8 +++++
.../Helper/EnvironmentConfigLoaderTest.php | 32 ++++++++++++-------
3 files changed, 34 insertions(+), 14 deletions(-)
diff --git a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
index 51ccf8f5a16..b8cdcf6ab40 100644
--- a/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
+++ b/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
@@ -639,8 +639,12 @@ public function isOverwrittenByEnvVariable(string $path): bool
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
$store = Mage::app()->getRequest()->getParam('store');
- $scope = $this->getScope();
- $path = "$scope/$store/$path";
+ if ($store) {
+ $scope = $this->getScope();
+ $path = "$scope/$store/$path";
+ return $environmentConfigLoaderHelper->hasPath($path);
+ }
+ $path = "default/$path";
return $environmentConfigLoaderHelper->hasPath($path);
}
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 6e568d65e06..2fc0cd1c115 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -146,6 +146,9 @@ public function hasPath(string $wantedPath): bool
public function getAsArray(string $wantedStore): array
{
+ if (empty($wantedStore)) {
+ $wantedStore = 'default';
+ }
$data = Mage::registry("config_env_array_$wantedStore");
if ($data !== null) {
return $data;
@@ -161,6 +164,11 @@ public function getAsArray(string $wantedStore): array
list($configKeyParts, $scope) = $this->getConfigKey($configKey);
switch ($scope) {
+ case static::CONFIG_KEY_DEFAULT:
+ list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
+ $path = $this->buildPath($section, $group, $field);
+ $config[$path] = $value;
+ break;
case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
[$unused1, $unused2, $storeCode, $section, $group, $field] = $configKeyParts;
diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
index 970da795484..2ec92a95ec9 100644
--- a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
+++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
@@ -114,12 +114,14 @@ public function testEnvOverridesForValidConfigKeys(array $config): void
$xml = new Varien_Simplexml_Config();
$xml->loadString($xmlStruct);
+
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
/** @phpstan-ignore method.internal */
$loader->setEnvStore([
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['env_path'] => $config['value'],
]);
+ Mage::unregister('current_env_config');
$loader->overrideEnvironment($xml);
$configPath = $config['xml_path'];
@@ -127,7 +129,9 @@ public function testEnvOverridesForValidConfigKeys(array $config): void
$valueAfterOverride = $xml->getNode($configPath);
// assert
- static::assertNotSame((string) $defaultValue, (string) $valueAfterOverride, 'Default value was not overridden.');
+ $expected = (string) $defaultValue;
+ $actual = (string) $valueAfterOverride;
+ static::assertNotSame($expected, $actual, 'Default value was not overridden.');
}
public function envOverridesCorrectConfigKeysDataProvider(): Generator
@@ -216,35 +220,36 @@ public function testAsArray(array $config): void
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
$config['env_path'] => 1,
]);
- $actual = $loader->getAsArray($config['scope']);
+ $store = $config['store'];
+ $actual = $loader->getAsArray($store);
$expected = $config['expected'];
static::assertSame($expected, $actual);
}
public function envAsArrayDataProvider(): Generator
{
- yield 'defaultScope' => [
+ yield 'default' => [
[
'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
- 'scope' => 'default',
+ 'store' => '', // or 'default', which will be used internally, but this is how \Mage_Adminhtml_Model_Config_Data::_validate defines it
'expected' => [
'general/store_information/name' => 1,
],
],
];
- yield 'storeScope' => [
+ yield 'store' => [
[
'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
- 'scope' => 'stores',
+ 'store' => 'german',
'expected' => [
'general/store_information/name' => 1,
],
],
];
- yield 'invalidScope' => [
+ yield 'invalidStore' => [
[
'env_path' => '',
- 'scope' => 'foo',
+ 'store' => 'foo',
'expected' => [],
],
];
@@ -283,7 +288,7 @@ public function envHasPathDataProvider(): Generator
yield 'hasPath store' => [
[
'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
- 'xml_path' => 'stores/general/store_information/name',
+ 'xml_path' => 'stores/german/general/store_information/name',
'expected' => true,
],
];
@@ -313,11 +318,14 @@ public function testEnvDoesNotOverrideForInvalidConfigKeys(array $config): void
$xml->loadString($xmlStruct);
$defaultValue = 'test_default';
- static::assertSame($defaultValue, (string) $xml->getNode(self::XML_PATH_DEFAULT));
+ $actual = (string) $xml->getNode(self::XML_PATH_DEFAULT);
+ static::assertSame($defaultValue, $actual);
$defaultWebsiteValue = 'test_website';
- static::assertSame($defaultWebsiteValue, (string) $xml->getNode(self::XML_PATH_WEBSITE));
+ $actual = (string) $xml->getNode(self::XML_PATH_WEBSITE);
+ static::assertSame($defaultWebsiteValue, $actual);
$defaultStoreValue = 'test_store';
- static::assertSame($defaultStoreValue, (string) $xml->getNode(self::XML_PATH_STORE));
+ $actual = (string) $xml->getNode(self::XML_PATH_STORE);
+ static::assertSame($defaultStoreValue, $actual);
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
/** @phpstan-ignore method.internal */
From f9b3283bbdb81e4595ee45c768b2c5d5d85da231 Mon Sep 17 00:00:00 2001
From: Pascal Querner
Date: Mon, 16 Jun 2025 20:29:22 +0200
Subject: [PATCH 29/29] chore: run rector:fix
---
.../core/Mage/Core/Helper/EnvironmentConfigLoader.php | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
index 2fc0cd1c115..1f3c6d2d7d0 100644
--- a/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
+++ b/app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
@@ -118,11 +118,11 @@ public function hasPath(string $wantedPath): bool
continue;
}
- list($configKeyParts, $scope) = $this->getConfigKey($configKey);
+ [$configKeyParts, $scope] = $this->getConfigKey($configKey);
switch ($scope) {
case static::CONFIG_KEY_DEFAULT:
- list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
+ [$unused1, $unused2, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$nodePath = $this->buildNodePath($scope, $path);
$config[$nodePath] = $value;
@@ -161,11 +161,11 @@ public function getAsArray(string $wantedStore): array
continue;
}
- list($configKeyParts, $scope) = $this->getConfigKey($configKey);
+ [$configKeyParts, $scope] = $this->getConfigKey($configKey);
switch ($scope) {
case static::CONFIG_KEY_DEFAULT:
- list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
+ [$unused1, $unused2, $section, $group, $field] = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$config[$path] = $value;
break;