mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-01 05:39:10 +02:00
FIX Allow FALSE in Config API, call remove() will NULL key on update()
This commit is contained in:
parent
222e554ce6
commit
bb52f2a214
@ -310,7 +310,6 @@ class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function merge_high_into_low(&$result, $value) {
|
public static function merge_high_into_low(&$result, $value) {
|
||||||
if (!$value) return;
|
|
||||||
$newType = self::get_value_type($value);
|
$newType = self::get_value_type($value);
|
||||||
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
@ -509,7 +508,7 @@ class Config {
|
|||||||
* Update a configuration value
|
* Update a configuration value
|
||||||
*
|
*
|
||||||
* Configuration is modify only. The value passed is merged into the existing configuration. If you want to
|
* Configuration is modify only. The value passed is merged into the existing configuration. If you want to
|
||||||
* replace the current value, you'll need to call remove first.
|
* replace the current array value, you'll need to call remove first.
|
||||||
*
|
*
|
||||||
* @param $class string - The class to update a configuration value for
|
* @param $class string - The class to update a configuration value for
|
||||||
* @param $name string - The configuration property name to update
|
* @param $name string - The configuration property name to update
|
||||||
@ -522,10 +521,17 @@ class Config {
|
|||||||
* You will get an error if you try and override array values with non-array values or vice-versa
|
* You will get an error if you try and override array values with non-array values or vice-versa
|
||||||
*/
|
*/
|
||||||
public function update($class, $name, $val) {
|
public function update($class, $name, $val) {
|
||||||
|
if(is_null($val)) {
|
||||||
|
$this->remove($class, $name);
|
||||||
|
} else {
|
||||||
if (!isset($this->overrides[0][$class])) $this->overrides[0][$class] = array();
|
if (!isset($this->overrides[0][$class])) $this->overrides[0][$class] = array();
|
||||||
|
|
||||||
if (!isset($this->overrides[0][$class][$name])) $this->overrides[0][$class][$name] = $val;
|
if (!array_key_exists($name, $this->overrides[0][$class])) {
|
||||||
else self::merge_high_into_low($this->overrides[0][$class][$name], $val);
|
$this->overrides[0][$class][$name] = $val;
|
||||||
|
} else {
|
||||||
|
self::merge_high_into_low($this->overrides[0][$class][$name], $val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->cache->clean("__{$class}__{$name}");
|
$this->cache->clean("__{$class}__{$name}");
|
||||||
}
|
}
|
||||||
@ -560,7 +566,7 @@ class Config {
|
|||||||
*
|
*
|
||||||
* Matching is always by "==", not by "==="
|
* Matching is always by "==", not by "==="
|
||||||
*/
|
*/
|
||||||
public function remove($class, $name) {
|
public function remove($class, $name /*,$key = null*/ /*,$value = null*/) {
|
||||||
$argc = func_num_args();
|
$argc = func_num_args();
|
||||||
$key = $argc > 2 ? func_get_arg(2) : self::anything();
|
$key = $argc > 2 ? func_get_arg(2) : self::anything();
|
||||||
$value = $argc > 3 ? func_get_arg(3) : self::anything();
|
$value = $argc > 3 ? func_get_arg(3) : self::anything();
|
||||||
|
@ -21,6 +21,16 @@ class ConfigStaticTest_First extends Config {
|
|||||||
public static $first = array('test_1');
|
public static $first = array('test_1');
|
||||||
public static $second = array('test_1');
|
public static $second = array('test_1');
|
||||||
public static $third = 'test_1';
|
public static $third = 'test_1';
|
||||||
|
|
||||||
|
public static $bool = true;
|
||||||
|
public static $int = 42;
|
||||||
|
public static $string = 'value';
|
||||||
|
public static $nullable = 'value';
|
||||||
|
|
||||||
|
public static $default_false = false;
|
||||||
|
public static $default_null = null;
|
||||||
|
public static $default_zero = 0;
|
||||||
|
public static $default_emtpy_string = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConfigStaticTest_Second extends ConfigStaticTest_First {
|
class ConfigStaticTest_Second extends ConfigStaticTest_First {
|
||||||
@ -83,6 +93,44 @@ class ConfigTest extends SapphireTest {
|
|||||||
array('test_3_2'));
|
array('test_3_2'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testUpdateWithFalsyValues() {
|
||||||
|
// Booleans
|
||||||
|
$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'bool', false);
|
||||||
|
$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'bool'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'bool', true);
|
||||||
|
$this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool'));
|
||||||
|
|
||||||
|
// Integers
|
||||||
|
$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'int', 0);
|
||||||
|
$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'int'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'int', 42);
|
||||||
|
$this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int'));
|
||||||
|
|
||||||
|
// Strings
|
||||||
|
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'string', '');
|
||||||
|
$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'string'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'string', 'value');
|
||||||
|
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string'));
|
||||||
|
|
||||||
|
// Nulls
|
||||||
|
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'nullable', null);
|
||||||
|
$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'nullable'));
|
||||||
|
Config::inst()->update('ConfigStaticTest_First', 'nullable', 'value');
|
||||||
|
$this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetsFalsyDefaults() {
|
||||||
|
$this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'default_false'));
|
||||||
|
// Technically the same as an undefined config key
|
||||||
|
$this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'default_null'));
|
||||||
|
$this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'default_zero'));
|
||||||
|
$this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'default_empty_string'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testUninheritedStatic() {
|
public function testUninheritedStatic() {
|
||||||
$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'third', Config::UNINHERITED), 'test_1');
|
$this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'third', Config::UNINHERITED), 'test_1');
|
||||||
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'third', Config::UNINHERITED), null);
|
$this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'third', Config::UNINHERITED), null);
|
||||||
|
Loading…
Reference in New Issue
Block a user