Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add dump() method to IniReader.

  • Loading branch information...
commit c703a633bd94bcef9dff8213577379c937e3ab8d 1 parent e7fa2a5
@markstory markstory authored
View
31 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 @@ 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);
+ }
}
View
30 lib/Cake/Test/Case/Configure/IniReaderTest.php
@@ -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);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.