Browse Source

[ZF-6289] Zend_Config_Writer_Ini jumbling sections

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21983 44c647ce-9c0f-0410-b52a-842ac1e357ba
jan 15 years ago
parent
commit
9887a0012e
2 changed files with 66 additions and 1 deletions
  1. 31 1
      library/Zend/Config/Writer/Ini.php
  2. 35 0
      tests/Zend/Config/Writer/IniTest.php

+ 31 - 1
library/Zend/Config/Writer/Ini.php

@@ -93,7 +93,8 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
                        .  $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
                                .  ' = '
@@ -160,4 +161,33 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract
             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 - 0
tests/Zend/Config/Writer/IniTest.php

@@ -246,4 +246,39 @@ ECS;
             $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
+        );
+    }
 }