Skip to content

Commit

Permalink
merge configs instead of overriding them completely
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 29, 2011
1 parent 2c79450 commit dd24574
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cake/libs/configure.php
Expand Up @@ -286,14 +286,25 @@ public static function drop($name) {
* @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);
}

Expand Down
7 changes: 7 additions & 0 deletions cake/tests/cases/libs/configure.test.php
Expand Up @@ -204,6 +204,13 @@ function testLoad() {
$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'));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions cake/tests/test_app/config/var_test2.php
@@ -0,0 +1,9 @@
<?php
$config = array(
'Read' => 'value2',
'Deep' => array(
'Second' => array(
'SecondDeepest' => 'buried2'
)
)
);

0 comments on commit dd24574

Please sign in to comment.