From dd245747e8e3ca2324bd91d5e35fa1301e01caa6 Mon Sep 17 00:00:00 2001 From: dereuromark Date: Tue, 29 Mar 2011 04:39:35 +0200 Subject: [PATCH] merge configs instead of overriding them completely --- cake/libs/configure.php | 13 ++++++++++++- cake/tests/cases/libs/configure.test.php | 7 +++++++ cake/tests/test_app/config/var_test2.php | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cake/tests/test_app/config/var_test2.php diff --git a/cake/libs/configure.php b/cake/libs/configure.php index 29633732c..cabe174c8 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -286,14 +286,25 @@ class Configure { * @link http://book.cakephp.org/view/929/load * @param string $key name of configuration resource to load. * @param string $config Name of the configured reader to use to read the resource identfied by $key. + * @param boolean $merge if config files should be merged instead of simply overridden * @return mixed false if file not found, void if load successful. * @throws ConfigureException Will throw any exceptions the reader raises. */ - public static function load($key, $config = 'default') { + public static function load($key, $config = 'default', $merge = false) { if (!isset(self::$_readers[$config])) { return false; } $values = self::$_readers[$config]->read($key); + + if ($merge) { + $keys = array_keys($values); + foreach ($keys as $key) { + if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) { + $values[$key] = array_merge_recursive($c, $values[$key]); + } + } + } + return self::write($values); } diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index e58874e21..dad3ed227 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -204,6 +204,13 @@ class ConfigureTest extends CakeTestCase { $this->assertTrue($result); $this->assertEquals('value', Configure::read('Read')); + + $result = Configure::load('var_test2', 'test', true); + $this->assertTrue($result); + + $this->assertEquals('value2', Configure::read('Read')); + $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest')); + $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest')); } /** diff --git a/cake/tests/test_app/config/var_test2.php b/cake/tests/test_app/config/var_test2.php new file mode 100644 index 000000000..33345b4ec --- /dev/null +++ b/cake/tests/test_app/config/var_test2.php @@ -0,0 +1,9 @@ + 'value2', + 'Deep' => array( + 'Second' => array( + 'SecondDeepest' => 'buried2' + ) + ) +); \ No newline at end of file