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
This commit is contained in:
Matthias Jentsch 2014-11-11 14:59:34 +01:00
parent 548b54253e
commit fdfad34e5c
2 changed files with 24 additions and 87 deletions

View File

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

View File

@ -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
*