merge configs instead of overriding them completely

This commit is contained in:
dereuromark 2011-03-29 04:39:35 +02:00
parent 2c79450228
commit dd245747e8
3 changed files with 28 additions and 1 deletions

View file

@ -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);
}

View file

@ -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'));
}
/**

View file

@ -0,0 +1,9 @@
<?php
$config = array(
'Read' => 'value2',
'Deep' => array(
'Second' => array(
'SecondDeepest' => 'buried2'
)
)
);