Add Configure::dump().

This commit is contained in:
mark_story 2012-04-19 22:45:01 -04:00
parent 578dac9259
commit 9f37277dab
2 changed files with 46 additions and 0 deletions

View file

@ -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.
*

View file

@ -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('<?php', $result);
$this->assertContains('$config = ', $result);
@unlink(TMP . 'config_test.php');
}
}