Update dump() to allow partial dumps.

This commit is contained in:
mark_story 2012-04-30 21:07:32 -04:00
parent 6ba923d20f
commit d43e0aa0db
3 changed files with 56 additions and 4 deletions

View file

@ -281,23 +281,40 @@ class Configure {
/**
* 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.
* 'default' adapter is a PhpReader, the generated file will be a PHP
* configuration file loadable by the PhpReader.
*
* ## Usage
*
* Given that the 'default' reader is an instance of PhpReader.
* Save all data in Configure to the file `my_config.php`:
*
* `Configure::dump('my_config.php', 'default');`
*
* Save only the error handling configuration:
*
* `Configure::dump('error.php', 'default', array('Error', 'Exception');`
*
* @param string $key The identifier to create in the config adapter.
* This could be a filename or a cache key depending on the adapter being used.
* @param string $config The name of the configured adapter to dump data with.
* @param array $keys The name of the top-level keys you want to dump.
* This allows you save only some data stored in Configure.
* @return boolean success
* @throws ConfigureException if the adapter does not implement a `dump` method.
*/
public static function dump($key, $config = 'default') {
public static function dump($key, $config = 'default', $keys = array()) {
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));
}
return (bool)self::$_readers[$config]->dump($key, self::$_values);
$values = self::$_values;
if (!empty($keys) && is_array($keys)) {
$values = array_intersect_key($values, array_flip($keys));
}
return (bool)self::$_readers[$config]->dump($key, $values);
}
/**

View file

@ -386,4 +386,23 @@ class ConfigureTest extends CakeTestCase {
@unlink(TMP . 'config_test.php');
}
/**
* Test dumping only some of the data.
*
* @return
*/
public function testDumpPartial() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump('config_test.php', 'test_reader', array('Error'));
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
$this->assertContains('Error', $result);
$this->assertNotContains('debug', $result);
@unlink(TMP . 'config_test.php');
}
}

View file

@ -0,0 +1,16 @@
<?php
$config = array (
'One' =>
array (
'two' => 'value',
'three' =>
array (
'four' => 'value four',
),
'null' => NULL,
),
'Asset' =>
array (
'timestamp' => 'force',
),
);