Add dump() method to IniReader.

This commit is contained in:
mark_story 2012-04-19 21:53:45 -04:00
parent e7fa2a526f
commit c703a633bd
2 changed files with 57 additions and 4 deletions

View file

@ -16,13 +16,15 @@
* @since CakePHP(tm) v 2.0 * @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @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, * Ini file configuration engine.
* 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 * 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 * 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: * you to create nested arrays structures in an ini config file. For example:
* *
@ -135,4 +137,25 @@ class IniReader implements ConfigReaderInterface {
return $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);
}
} }

View file

@ -125,4 +125,34 @@ class IniReaderTest extends CakeTestCase {
$config = $reader->read('nested'); $config = $reader->read('nested');
$this->assertTrue($config['bools']['test_on']); $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);
}
} }