Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[ZF-6289] Zend_Config_Writer_Ini jumbling sections
Browse files Browse the repository at this point in the history
git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21983 44c647ce-9c0f-0410-b52a-842ac1e357ba
  • Loading branch information
jan committed Apr 25, 2010
1 parent 26e1a5e commit 59c83c4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Writer/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function render()
. $this->_addBranch($this->_config)
. "\n";
} else {
foreach ($this->_config as $sectionName => $data) {
$config = $this->_sortRootElements($this->_config);
foreach ($config as $sectionName => $data) {
if (!($data instanceof Zend_Config)) {
$iniString .= $sectionName
. ' = '
Expand Down Expand Up @@ -160,4 +161,33 @@ protected function _prepareValue($value)
throw new Zend_Config_Exception('Value can not contain double quotes "');
}
}

/**
* Root elements that are not assigned to any section needs to be
* on the top of config.
*
* @see http://framework.zend.com/issues/browse/ZF-6289
* @param Zend_Config
* @return Zend_Config
*/
protected function _sortRootElements(Zend_Config $config)
{
$configArray = $config->toArray();
$sections = array();

// remove sections from config array
foreach ($configArray as $key => $value) {
if (is_array($value)) {
$sections[$key] = $value;
unset($configArray[$key]);
}
}

// readd sections to the end
foreach ($sections as $key => $value) {
$configArray[$key] = $value;
}

return new Zend_Config($configArray);
}
}
35 changes: 35 additions & 0 deletions test/Writer/IniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,39 @@ public function testZF6521_NoDoubleQuoutesInValue()
$this->assertContains('Value can not contain double quotes "', $expected->getMessage());
}
}

/**
* @group ZF-6289
*/
public function testZF6289_NonSectionElementsAndSectionJumbling()
{
$config = new Zend_Config(array(
'one' => 'element',
'two' => array('type' => 'section'),
'three' => 'element',
'four' => array('type' => 'section'),
'five' => 'element'
));

$writer = new Zend_Config_Writer_Ini;
$iniString = $writer->setConfig($config)->render($config);

$expected = <<<ECS
one = "element"
three = "element"
five = "element"
[two]
type = "section"
[four]
type = "section"
ECS;

$this->assertEquals(
$expected,
$iniString
);
}
}

0 comments on commit 59c83c4

Please sign in to comment.