Permalink
Browse files

Add Configure::dump().

  • Loading branch information...
1 parent 578dac9 commit 9f37277dab6eeeb4959beb862c34f5ae43e6bd99 @markstory markstory committed Apr 20, 2012
Showing with 46 additions and 0 deletions.
  1. +22 −0 lib/Cake/Core/Configure.php
  2. +24 −0 lib/Cake/Test/Case/Core/ConfigureTest.php
@@ -278,6 +278,28 @@ public static function load($key, $config = 'default', $merge = true) {
return self::write($values);
}
+/**
+ * Dump data currently in Configure into $filename. The serialization format
+ * is decided by the config reader attached as $config. For example, if the
+ * 'default' adapter is a PhpReader, the generated file will be a PHP configuration
+ * file loadable by the PhpReader.
+ *
+ * @param string $filename The filename to save content into.
+ * @param string $config The name of the configured adapter to dump data with.
+ * @return void
+ * @throws ConfigureException if the adapter does not implement a `dump` method.
+ */
+ public static function dump($filename, $config = 'default') {
+ if (empty(self::$_readers[$config])) {
+ throw new ConfigureException(__d('cake', 'There is no "%s" adapter.', $config));
+ }
+ if (!method_exists(self::$_readers[$config], 'dump')) {
+ throw new ConfigureException(__d('cake', 'The "%s" adapter, does not have a dump() method.', $config));
+ }
+ $content = self::$_readers[$config]->dump(self::$_values);
+ return file_put_contents($filename, $content);
+ }
+
/**
* Used to determine the current version of CakePHP.
*
@@ -362,4 +362,28 @@ public function testClear() {
$this->assertNull(Configure::read('debug'));
$this->assertNull(Configure::read('test'));
}
+
+/**
+ * @expectedException ConfigureException
+ */
+ public function testDumpNoAdapter() {
+ Configure::dump(TMP . 'test.php', 'does_not_exist');
+ }
+
+/**
+ * test dump integrated with the PhpReader.
+ *
+ * @return void
+ */
+ public function testDump() {
+ Configure::config('test_reader', new PhpReader(TMP));
+
+ $result = Configure::dump(TMP . 'config_test.php', 'test_reader');
+ $this->assertTrue($result > 0);
+ $result = file_get_contents(TMP . 'config_test.php');
+ $this->assertContains('<?php', $result);
+ $this->assertContains('$config = ', $result);
+ @unlink(TMP . 'config_test.php');
+ }
+
}

0 comments on commit 9f37277

Please sign in to comment.