Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Oct 8, 2024
1 parent cd03fc9 commit ec2c8a9
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public function render(Varien_Data_Form_Element_Abstract $element)
$checkboxLabel = $this->__('Use Default');
}

$envConfig = Mage::getConfig()->getEnvOverriddenConfigPaths();
$envConfigIds = array_combine(str_replace('/', '_', array_keys($envConfig)), array_values($envConfig), );
if (array_key_exists($element->getId(), $envConfigIds)) {
$addInheritCheckbox = false;
$element->setDisabled(true);
$element->setScopeLabel('[ENV]]');
$element->setValue($envConfigIds[$element->getId()]);
}

if ($addInheritCheckbox) {
$inherit = $element->getInherit() == 1 ? 'checked="checked"' : '';
if ($inherit) {
Expand Down
15 changes: 13 additions & 2 deletions app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Mage_Core_Helper_EnvironmentConfigLoader extends Mage_Core_Helper_Abstract
*/
public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
{
if (!$xmlConfig instanceof Mage_Core_Model_Config) {
// return;
}

$env = $this->getEnv();

foreach ($env as $configKey => $value) {
Expand All @@ -66,20 +70,25 @@ public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
list($configKeyParts, $scope) = $this->getConfigKey($configKey);

switch ($scope) {
default:
case static::CONFIG_KEY_DEFAULT:
list($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('stores/' . $nodePath, $value);
break;

case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
list($unused1, $unused2, $code, $section, $group, $field) = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$nodePath = sprintf('%s/%s/%s', strtolower($scope), strtolower($code), $path);
$xmlConfig->setNode($nodePath, $value);
break;
}

$xmlConfig->addEnvOverriddenConfigPaths($path, $value);

Check failure on line 89 in app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php

View workflow job for this annotation

GitHub Actions / PHPStan / Analyze (ubuntu-latest, 8.4)

Call to an undefined method Varien_Simplexml_Config::addEnvOverriddenConfigPaths().
$xmlConfig->addEnvOverriddenConfigPaths($nodePath, $value);

Check failure on line 90 in app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php

View workflow job for this annotation

GitHub Actions / PHPStan / Analyze (ubuntu-latest, 8.4)

Call to an undefined method Varien_Simplexml_Config::addEnvOverriddenConfigPaths().
$xmlConfig->setNode($nodePath, $value);
}
}

Expand All @@ -94,6 +103,7 @@ public function setEnvStore(array $envStorage): void
public function getEnv(): array
{
if (empty($this->envStore)) {
// phpcs:ignore Ecg.Security.ForbiddenFunction.Found
$this->envStore = getenv();
}
return $this->envStore;
Expand Down Expand Up @@ -144,5 +154,6 @@ protected function buildPath(string $section, string $group, string $field): str
protected function buildNodePath(string $scope, string $path): string
{
return strtolower($scope) . '/' . $path;
// return 'stores/' . strtolower($scope) . '/' . $path;
}
}
18 changes: 17 additions & 1 deletion app/code/core/Mage/Core/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ class Mage_Core_Model_Config extends Mage_Core_Model_Config_Base
*/
protected $_allowedModules = [];

/**
* Config paths overloaded by ENV
*/
protected array $envOverriddenConfigPaths = [];

/**
* Class construct
*
Expand Down Expand Up @@ -739,7 +744,7 @@ public function getSectionNode($path)
* Returns node found by the $path and scope info
*
* @inheritDoc
* @return Mage_Core_Model_Config_Element
* @return Mage_Core_Model_Config_Element|false
*/
public function getNode($path = null, $scope = '', $scopeCode = null)
{
Expand Down Expand Up @@ -1822,4 +1827,15 @@ protected function _isNodeNameHasUpperCase(Mage_Core_Model_Config_Element $event
{
return (strtolower($event->getName()) !== (string)$event->getName());
}

public function getEnvOverriddenConfigPaths(): array
{
return $this->envOverriddenConfigPaths;
}

public function addEnvOverriddenConfigPaths(string $path, string $value): Mage_Core_Model_Config
{
$this->envOverriddenConfigPaths[$path] = $value;
return $this;
}
}
15 changes: 12 additions & 3 deletions app/code/core/Mage/Core/Model/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,22 @@ public function getCode()
*/
public function getConfig($path)
{
$config = Mage::getConfig();
$fullPath = 'stores/' . $this->getCode() . '/' . $path;

$envConfig = $config->getEnvOverriddenConfigPaths();
if (array_key_exists($fullPath, $envConfig)) {
return $envConfig[$fullPath];
}

if (array_key_exists($path, $envConfig)) {
return $envConfig[$path];
}

if (isset($this->_configCache[$path])) {
return $this->_configCache[$path];
}

$config = Mage::getConfig();

$fullPath = 'stores/' . $this->getCode() . '/' . $path;
$data = $config->getNode($fullPath);
if (!$data && !Mage::isInstalled()) {
$data = $config->getNode('default/' . $path);
Expand Down
2 changes: 2 additions & 0 deletions lib/Varien/Data/Form/Element/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @method bool getNoSpan()
* @method $this setName(string $value)
* @method bool getRequired()
* @method string getScopeLabel()
* @method $this setScopeLabel(string $value)
* @method string getValue()
* @method array getValues()
* @method $this setValues(array|int|string $value)
Expand Down
15 changes: 15 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
</testsuite>
</testsuites>

<php>
<env name="OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME" value="ENV default"/>
<env name="OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO-BAR__NAME" value="ENV default dashes"/>
<env name="OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO_BAR__NAME" value="ENV default underscore"/>
<env name="OPENMAGE_CONFIG__DEFAULT__GENERAL__ST" value="ENV default invalid"/>
<env name="OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__STORE_INFORMATION__NAME" value="ENV website"/>
<env name="OPENMAGE_CONFIG__WEBSITES__BASE-AT__GENERAL__STORE_INFORMATION__NAME" value="ENV website dashes"/>
<env name="OPENMAGE_CONFIG__WEBSITES__BASE_CH__GENERAL__STORE_INFORMATION__NAME" value="ENV website underscore"/>
<env name="OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__ST" value="ENV website invalid"/>
<env name="OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME" value="ENV store"/>
<env name="OPENMAGE_CONFIG__STORES__GERMAN-AT__GENERAL__STORE_INFORMATION__NAME" value="ENV store dashes"/>
<env name="OPENMAGE_CONFIG__STORES__GERMAN_CH__GENERAL__STORE_INFORMATION__NAME" value="ENV store underscore"/>
<env name="OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__ST" value="ENV store invalid"/>
</php>

<coverage
includeUncoveredFiles="true"
processUncoveredFiles="false"
Expand Down
Loading

0 comments on commit ec2c8a9

Please sign in to comment.