Skip to content
Permalink
Browse files

Update dump() to allow partial dumps.

  • Loading branch information...
markstory committed May 1, 2012
1 parent 6ba923d commit d43e0aa0db24e9b515566bd47a0a90606902da58
Showing with 56 additions and 4 deletions.
  1. +21 −4 lib/Cake/Core/Configure.php
  2. +19 −0 lib/Cake/Test/Case/Core/ConfigureTest.php
  3. +16 −0 lib/Cake/Test/test_app/Config/test.php
@@ -281,23 +281,40 @@ public static function load($key, $config = 'default', $merge = true) {
/**
* 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);
}
/**
@@ -386,4 +386,23 @@ public function testDump() {
@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');
}
}
@@ -0,0 +1,16 @@
<?php
$config = array (
'One' =>
array (
'two' => 'value',
'three' =>
array (
'four' => 'value four',
),
'null' => NULL,
),
'Asset' =>
array (
'timestamp' => 'force',
),
);

0 comments on commit d43e0aa

Please sign in to comment.
You can’t perform that action at this time.