Skip to content
Permalink
Browse files

Add dump() method to IniReader.

  • Loading branch information...
markstory committed Apr 20, 2012
1 parent e7fa2a5 commit c703a633bd94bcef9dff8213577379c937e3ab8d
Showing with 57 additions and 4 deletions.
  1. +27 −4 lib/Cake/Configure/IniReader.php
  2. +30 −0 lib/Cake/Test/Case/Configure/IniReaderTest.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 @@ protected function _parseNestedValues($values) {
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);
}
}
@@ -125,4 +125,34 @@ public function testReadingWithoutExtension() {
$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 = <<<INI
[One]
two = value
three.four = value four
[Asset]
timestamp = force
INI;
$this->assertEquals($expected, $result);
}
}

0 comments on commit c703a63

Please sign in to comment.
You can’t perform that action at this time.