From c703a633bd94bcef9dff8213577379c937e3ab8d Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 19 Apr 2012 21:53:45 -0400 Subject: [PATCH] Add dump() method to IniReader. --- lib/Cake/Configure/IniReader.php | 31 ++++++++++++++++--- .../Test/Case/Configure/IniReaderTest.php | 30 ++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Configure/IniReader.php b/lib/Cake/Configure/IniReader.php index f47713294..77035e9c2 100644 --- a/lib/Cake/Configure/IniReader.php +++ b/lib/Cake/Configure/IniReader.php @@ -16,13 +16,15 @@ * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::uses('Hash', 'Utility'); /** - * Ini file configuration parser. Since IniReader uses parse_ini_file underneath, - * you should be aware that this class shares the same behavior, especially with - * regards to boolean and null values. + * Ini file configuration engine. * - * In addition to the native parse_ini_file features, IniReader also allows you + * Since IniReader uses parse_ini_file underneath, you should be aware that this + * class shares the same behavior, especially with regards to boolean and null values. + * + * In addition to the native `parse_ini_file` features, IniReader also allows you * to create nested array structures through usage of `.` delimited names. This allows * you to create nested arrays structures in an ini config file. For example: * @@ -135,4 +137,25 @@ class IniReader implements ConfigReaderInterface { return $values; } +/** + * Dumps the state of Configure data into an ini formatted string. + * + * @param array $data The data to convert to ini file. + * @return string The converted configuration as an ini string + */ + public function dump($data) { + $result = array(); + foreach ($data as $key => $value) { + if ($key[0] != '[') { + $result[] = "[$key]"; + } + if (is_array($value)) { + $keyValues = Hash::flatten($value, '.'); + foreach ($keyValues as $k => $v) { + $result[] = "$k = " . trim(var_export($v, true), "'"); + } + } + } + return join("\n", $result); + } } diff --git a/lib/Cake/Test/Case/Configure/IniReaderTest.php b/lib/Cake/Test/Case/Configure/IniReaderTest.php index 6ce077fa2..6d69c0371 100644 --- a/lib/Cake/Test/Case/Configure/IniReaderTest.php +++ b/lib/Cake/Test/Case/Configure/IniReaderTest.php @@ -125,4 +125,34 @@ class IniReaderTest extends CakeTestCase { $config = $reader->read('nested'); $this->assertTrue($config['bools']['test_on']); } + +/** + * test dump method. + * + * @return void + */ + public function testDump() { + $reader = new IniReader($this->path); + $data = array( + 'One' => array( + 'two' => 'value', + 'three' => array( + 'four' => 'value four' + ) + ), + 'Asset' => array( + 'timestamp' => 'force' + ), + ); + $result = $reader->dump($data); + $expected = <<assertEquals($expected, $result); + } + }