From fdfad34e5cf14a2fc93ff9f01ce2b1e9243860f1 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Tue, 11 Nov 2014 14:59:34 +0100 Subject: [PATCH] Do not normalize configuration keys to nested arrays in IniWriter More than one nesting level (the section) is no longer allowed in configuration files. Dots in keys are now part of the key and will not lead to a nested configuration. fixes #7120 --- library/Icinga/File/Ini/IniWriter.php | 49 --------------- .../library/Icinga/File/Ini/IniWriterTest.php | 62 +++++++------------ 2 files changed, 24 insertions(+), 87 deletions(-) diff --git a/library/Icinga/File/Ini/IniWriter.php b/library/Icinga/File/Ini/IniWriter.php index 0db8be139..c9b4b3c20 100644 --- a/library/Icinga/File/Ini/IniWriter.php +++ b/library/Icinga/File/Ini/IniWriter.php @@ -44,45 +44,6 @@ class IniWriter extends Zend_Config_Writer_FileAbstract parent::__construct($options); } - /** - * Find all keys containing dots and convert it to a nested configuration - * - * Ensure that configurations with the same ini representation the have - * similarly nested Zend_Config objects. The configuration may be altered - * during that process. - * - * @param Zend_Config $config The configuration to normalize - * @return Zend_Config The normalized config - */ - private function normalizeKeys(Zend_Config $config) - { - foreach ($config as $key => $value) { - if (preg_match('/\./', $key) > 0) { - // remove old key - unset ($config->$key); - - // insert new key - $nests = explode('.', $key); - $current = $config; - $i = 0; - for (; $i < count($nests) - 1; $i++) { - if (! isset($current->{$nests[$i]})) { - // configuration key doesn't exist, create a new nesting level - $current->{$nests[$i]} = new Zend_Config (array(), true); - } - // move to next nesting level - $current = $current->{$nests[$i]}; - } - // reached last nesting level, insert value - $current->{$nests[$i]} = $value; - } - if ($value instanceof Zend_Config) { - $config->$key = $this->normalizeKeys ($value); - } - } - return $config; - } - /** * Render the Zend_Config into a config file string * @@ -96,16 +57,6 @@ class IniWriter extends Zend_Config_Writer_FileAbstract $oldconfig = new Zend_Config(array()); } - // create an internal copy of the given configuration, since the user of this class - // won't expect that a configuration will ever be altered during - // the rendering process. - $extends = $this->_config->getExtends(); - $this->_config = new Zend_Config ($this->_config->toArray(), true); - foreach ($extends as $extending => $extended) { - $this->_config->setExtend($extending, $extended); - } - $this->_config = $this->normalizeKeys($this->_config); - $newconfig = $this->_config; $editor = new IniEditor(file_get_contents($this->_filename), $this->options); $this->diffConfigs($oldconfig, $newconfig, $editor); diff --git a/test/php/library/Icinga/File/Ini/IniWriterTest.php b/test/php/library/Icinga/File/Ini/IniWriterTest.php index 53bd750d8..1b9580fb3 100644 --- a/test/php/library/Icinga/File/Ini/IniWriterTest.php +++ b/test/php/library/Icinga/File/Ini/IniWriterTest.php @@ -29,6 +29,30 @@ class IniWriterTest extends BaseTestCase unlink($this->tempFile2); } + public function testWhetherPointInSectionIsNotNormalized() + { + $writer = new IniWriter( + array( + 'config' => new Config( + array( + 'section' => array( + 'foo.bar' => 1337 + ), + 'section.with.multiple.dots' => array( + 'some more' => array( + 'nested stuff' => 'With more values' + ) + ) + ) + ), + 'filename' => $this->tempFile + ) + ); + $writer->write(); + $config = Config::fromIni($this->tempFile)->toArray(); + $this->assertTrue(array_key_exists('section.with.multiple.dots', $config), 'Section names not normalized'); + } + public function testWhetherSimplePropertiesAreInsertedInEmptyFiles() { $this->markTestSkipped('Implementation has changed. Section-less properties are not supported anymore'); @@ -702,44 +726,6 @@ EOD; ); } - public function testKeyNormalization() - { - $normalKeys = new IniWriter( - array ( - 'config' => new Config(array ( - 'foo' => 'bar', - 'nest' => array ( - 'nested' => array ( - 'stuff' => 'nested configuration element' - ) - ), - 'preserving' => array ( - 'ini' => array( - 'writer' => 'n' - ), - 'foo' => 'this should not be overwritten' - ) - )), - 'filename' => $this->tempFile - ) - - ); - - $nestedKeys = new IniWriter( - array ( - 'config' => new Config(array ( - 'foo' => 'bar', - 'nest.nested.stuff' => 'nested configuration element', - 'preserving.ini.writer' => 'n', - 'preserving.foo' => 'this should not be overwritten' - )), - 'filename' => $this->tempFile2 - ) - - ); - $this->assertEquals($normalKeys->render(), $nestedKeys->render()); - } - /** * Write a INI-configuration string to a temporary file and return it's path *