Skip to content

Commit

Permalink
Fixed #3792
Browse files Browse the repository at this point in the history
  • Loading branch information
andris-sevcenko committed Feb 21, 2019
1 parent f6f29d2 commit be381c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Fixed
- Fixed an error where the `dateModified` key was updated in project config when there was no point in doing that. ([#3792](https://github.com/craftcms/cms/issues/3792))

### Security
- Fixed a bug where sensitive environment variable values weren’t getting redacted correctly.

Expand Down
29 changes: 29 additions & 0 deletions src/helpers/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,33 @@ public static function ensureAllUserGroupsProcessed()
}
}
}

/**
* Traverse and clean a config array, removing empty values and sorting keys.
*
* @param array $config Config array to clean
*
* @return array
*/
public static function cleanupConfig(array $config) {
$remove = [];
foreach ($config as $key => &$value) {
if (\is_array($value)) {
$value = static::cleanupConfig($value);

if (empty($value)) {
$remove[] = $key;
}
}
}

// Remove empty stuff
foreach ($remove as $removeKey) {
unset($config[$removeKey]);
}

ksort($config);

return $config;
}
}
29 changes: 7 additions & 22 deletions src/services/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use craft\helpers\DateTimeHelper;
use craft\helpers\FileHelper;
use craft\helpers\Json;
use craft\helpers\ProjectConfig as ProjectConfigHelper;
use craft\helpers\Path as PathHelper;
use Symfony\Component\Yaml\Yaml;
use yii\base\Application;
use yii\base\Component;
use yii\base\ErrorException;
use yii\base\Event;
use yii\base\Exception;
use yii\base\NotSupportedException;
use yii\web\ServerErrorHttpException;
Expand Down Expand Up @@ -341,6 +341,10 @@ public function get(string $path = null, $getFromYaml = false)
*/
public function set(string $path, $value)
{
if (\is_array($value)) {
$value = ProjectConfigHelper::cleanupConfig($value);
}

if ($value !== $this->get($path)) {
if ($this->readOnly) {
throw new NotSupportedException('Changes to the project config are not possible while in read-only mode.');
Expand Down Expand Up @@ -585,38 +589,19 @@ public function updateParsedConfigTimes(): bool
*/
public function saveModifiedConfigData()
{
$traverseAndClean = function(&$array) use (&$traverseAndClean) {
$remove = [];
foreach ($array as $key => &$value) {
if (\is_array($value)) {
$traverseAndClean($value);
if (empty($value)) {
$remove[] = $key;
}
}
}

// Remove empty stuff
foreach ($remove as $removeKey) {
unset($array[$removeKey]);
}

ksort($array);
};

if (!empty($this->_modifiedYamlFiles) && $this->_useConfigFile()) {
// Save modified yaml files

foreach (array_keys($this->_modifiedYamlFiles) as $filePath) {
$data = $this->_parsedConfigs[$filePath];
$traverseAndClean($data);
$data = ProjectConfigHelper::cleanupConfig($data);
FileHelper::writeToFile($filePath, Yaml::dump($data, 20, 2));
}
}

if (($this->_updateConfigMap && $this->_useConfigFile()) || $this->_updateConfig) {
$previousConfig = $this->_getStoredConfig();
$traverseAndClean($previousConfig);
$value = ProjectConfigHelper::cleanupConfig($previousConfig);
$this->_storeYamlHistory($previousConfig);

$info = Craft::$app->getInfo();
Expand Down

0 comments on commit be381c8

Please sign in to comment.