From b30cacc2831bab3f81bff4ea6d21c6269a4b8f3d Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Sun, 16 Dec 2012 12:56:44 +0100 Subject: [PATCH 1/2] added suport for multi-level nested ini config variables --- library/Zend/Config/Reader/Ini.php | 25 ++++++++++++++++++++++-- tests/ZendTest/Config/Reader/IniTest.php | 16 +++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/library/Zend/Config/Reader/Ini.php b/library/Zend/Config/Reader/Ini.php index cd06bc915ca..edb93de47a4 100644 --- a/library/Zend/Config/Reader/Ini.php +++ b/library/Zend/Config/Reader/Ini.php @@ -131,8 +131,8 @@ protected function process(array $data) foreach ($data as $section => $value) { if (is_array($value)) { if (strpos($section, $this->nestSeparator) !== false) { - $section = explode($this->nestSeparator, $section, 2); - $config[$section[0]][$section[1]] = $this->processSection($value); + $sections = explode($this->nestSeparator, $section); + $config = array_merge_recursive($config, $this->buildNestedSection($sections, $value)); } else { $config[$section] = $this->processSection($value); } @@ -144,6 +144,27 @@ protected function process(array $data) return $config; } + /** + * Process a nested section + * + * @param array $sections + * @param mixed $value + * @return array + */ + private function buildNestedSection($sections, $value) + { + if(count($sections) == 0) { + return $this->processSection($value); + } else { + $nestedSection = array(); + + $first = array_shift($sections); + $nestedSection[$first] = $this->buildNestedSection($sections, $value); + + return $nestedSection; + } + } + /** * Process a section. * diff --git a/tests/ZendTest/Config/Reader/IniTest.php b/tests/ZendTest/Config/Reader/IniTest.php index 66484b9100b..48838d6856d 100644 --- a/tests/ZendTest/Config/Reader/IniTest.php +++ b/tests/ZendTest/Config/Reader/IniTest.php @@ -87,17 +87,17 @@ public function testFromStringWithSection() public function testFromStringNested() { $ini = <<reader->fromString($ini); - $this->assertEquals($arrayIni['foo']['bar'], 'foobar'); - $this->assertEquals($arrayIni['foobar'][0], 'foobarArray'); - $this->assertEquals($arrayIni['foo']['baz'][0], 'foobaz1'); - $this->assertEquals($arrayIni['foo']['baz'][1], 'foobaz2'); + $this->assertEquals($arrayIni['bla']['foo']['bar'], 'foobar'); + $this->assertEquals($arrayIni['bla']['foobar'][0], 'foobarArray'); + $this->assertEquals($arrayIni['bla']['foo']['baz'][0], 'foobaz1'); + $this->assertEquals($arrayIni['bla']['foo']['baz'][1], 'foobaz2'); } } From b45f32b659ec7050975405d891e32c243670cc96 Mon Sep 17 00:00:00 2001 From: Robert Boloc Date: Mon, 17 Dec 2012 08:40:17 +0100 Subject: [PATCH 2/2] removed else condition after return --- library/Zend/Config/Reader/Ini.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/Zend/Config/Reader/Ini.php b/library/Zend/Config/Reader/Ini.php index edb93de47a4..c264a115d8a 100644 --- a/library/Zend/Config/Reader/Ini.php +++ b/library/Zend/Config/Reader/Ini.php @@ -155,14 +155,14 @@ private function buildNestedSection($sections, $value) { if(count($sections) == 0) { return $this->processSection($value); - } else { - $nestedSection = array(); + } - $first = array_shift($sections); - $nestedSection[$first] = $this->buildNestedSection($sections, $value); + $nestedSection = array(); - return $nestedSection; - } + $first = array_shift($sections); + $nestedSection[$first] = $this->buildNestedSection($sections, $value); + + return $nestedSection; } /**