diff --git a/lib/Cake/Core/Configure.php b/lib/Cake/Core/Configure.php index 45e60fd2d..9a4e1adc9 100644 --- a/lib/Cake/Core/Configure.php +++ b/lib/Cake/Core/Configure.php @@ -278,6 +278,28 @@ class Configure { 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. * diff --git a/lib/Cake/Test/Case/Core/ConfigureTest.php b/lib/Cake/Test/Case/Core/ConfigureTest.php index 705e83b95..88a996e19 100644 --- a/lib/Cake/Test/Case/Core/ConfigureTest.php +++ b/lib/Cake/Test/Case/Core/ConfigureTest.php @@ -362,4 +362,28 @@ class ConfigureTest extends CakeTestCase { $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('assertContains('$config = ', $result); + @unlink(TMP . 'config_test.php'); + } + }