Skip to content

Commit

Permalink
Merge branch 'master' into feature/backend-classes-7635
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Gelf committed Nov 11, 2014
2 parents 1ca83c0 + c70f738 commit a03dc5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 92 deletions.
15 changes: 10 additions & 5 deletions library/Icinga/File/Ini/IniEditor.php
Expand Up @@ -433,6 +433,9 @@ private function formatKeyValuePair(array $key, $value)
*/
private function formatKey(array $key)
{
foreach ($key as $i => $separator) {
$key[$i] = $this->sanitize($separator);
}
return implode($this->nestSeparator, $key);
}

Expand Down Expand Up @@ -599,7 +602,7 @@ private function removeFromArray($array, $pos)
}

/**
* Prepare a value for INe
* Prepare a value for INI
*
* @param $value The value of the string
*
Expand All @@ -613,10 +616,12 @@ private function formatValue($value)
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} elseif (strpos($value, '"') === false) {
return '"' . $value . '"';
} else {
return '"' . str_replace('"', '\"', $value) . '"';
}
return '"' . str_replace('"', '\"', $this->sanitize($value)) . '"';
}

private function sanitize($value)
{
return str_replace('\n', '', $value);
}
}
49 changes: 0 additions & 49 deletions library/Icinga/File/Ini/IniWriter.php
Expand Up @@ -44,45 +44,6 @@ public function __construct(array $options = null)
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
*
Expand All @@ -96,16 +57,6 @@ public function render()
$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);
Expand Down
62 changes: 24 additions & 38 deletions test/php/library/Icinga/File/Ini/IniWriterTest.php
Expand Up @@ -29,6 +29,30 @@ public function tearDown()
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');
Expand Down Expand Up @@ -702,44 +726,6 @@ public function testWhetherCommentsOnSectionPropertyLinesArePreserved()
);
}

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
*
Expand Down

0 comments on commit a03dc5a

Please sign in to comment.