From d43e0aa0db24e9b515566bd47a0a90606902da58 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 30 Apr 2012 21:07:32 -0400 Subject: [PATCH] Update dump() to allow partial dumps. --- lib/Cake/Core/Configure.php | 25 +++++++++++++++++++---- lib/Cake/Test/Case/Core/ConfigureTest.php | 19 +++++++++++++++++ lib/Cake/Test/test_app/Config/test.php | 16 +++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 lib/Cake/Test/test_app/Config/test.php diff --git a/lib/Cake/Core/Configure.php b/lib/Cake/Core/Configure.php index 9b4a85ee3..b90fcfc6d 100644 --- a/lib/Cake/Core/Configure.php +++ b/lib/Cake/Core/Configure.php @@ -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); } /** diff --git a/lib/Cake/Test/Case/Core/ConfigureTest.php b/lib/Cake/Test/Case/Core/ConfigureTest.php index a384c591c..7351f83f1 100644 --- a/lib/Cake/Test/Case/Core/ConfigureTest.php +++ b/lib/Cake/Test/Case/Core/ConfigureTest.php @@ -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('assertContains('$config = ', $result); + $this->assertContains('Error', $result); + $this->assertNotContains('debug', $result); + + @unlink(TMP . 'config_test.php'); + } + } diff --git a/lib/Cake/Test/test_app/Config/test.php b/lib/Cake/Test/test_app/Config/test.php new file mode 100644 index 000000000..bc7c05ce2 --- /dev/null +++ b/lib/Cake/Test/test_app/Config/test.php @@ -0,0 +1,16 @@ + + array ( + 'two' => 'value', + 'three' => + array ( + 'four' => 'value four', + ), + 'null' => NULL, + ), + 'Asset' => + array ( + 'timestamp' => 'force', + ), +); \ No newline at end of file