mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
merge configs instead of overriding them completely
This commit is contained in:
parent
2c79450228
commit
dd245747e8
3 changed files with 28 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
9
cake/tests/test_app/config/var_test2.php
Normal file
9
cake/tests/test_app/config/var_test2.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
$config = array(
|
||||
'Read' => 'value2',
|
||||
'Deep' => array(
|
||||
'Second' => array(
|
||||
'SecondDeepest' => 'buried2'
|
||||
)
|
||||
)
|
||||
);
|
Loading…
Reference in a new issue