ConfigManager: add a method to remove an entry

This commit is contained in:
ArthurHoaro 2017-11-11 14:00:18 +01:00
parent 1b93137e16
commit a3724717ec
2 changed files with 72 additions and 1 deletions

View file

@ -147,6 +147,33 @@ public function set($setting, $value, $write = false, $isLoggedIn = false)
} }
} }
/**
* Remove a config element from the config file.
*
* @param string $setting Asked setting, keys separated with dots.
* @param bool $write Write the new setting in the config file, default false.
* @param bool $isLoggedIn User login state, default false.
*
* @throws \Exception Invalid
*/
public function remove($setting, $write = false, $isLoggedIn = false)
{
if (empty($setting) || ! is_string($setting)) {
throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
}
// During the ConfigIO transition, map legacy settings to the new ones.
if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
$setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
}
$settings = explode('.', $setting);
self::removeConfig($settings, $this->loadedConfig);
if ($write) {
$this->write($isLoggedIn);
}
}
/** /**
* Check if a settings exists. * Check if a settings exists.
* *
@ -289,6 +316,27 @@ protected static function setConfig($settings, $value, &$conf)
$conf[$setting] = $value; $conf[$setting] = $value;
} }
/**
* Recursive function which find asked setting in the loaded config and deletes it.
*
* @param array $settings Ordered array which contains keys to find.
* @param array $conf Loaded settings, then sub-array.
*
* @return mixed Found setting or NOT_FOUND flag.
*/
protected static function removeConfig($settings, &$conf)
{
if (!is_array($settings) || count($settings) == 0) {
return self::$NOT_FOUND;
}
$setting = array_shift($settings);
if (count($settings) > 0) {
return self::removeConfig($settings, $conf[$setting]);
}
unset($conf[$setting]);
}
/** /**
* Set a bunch of default values allowing Shaarli to start without a config file. * Set a bunch of default values allowing Shaarli to start without a config file.
*/ */

View file

@ -81,6 +81,18 @@ public function testSetWriteGetNested()
$this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff')); $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
} }
public function testSetDeleteNested()
{
$this->conf->set('foo.bar.key.stuff', 'testSetDeleteNested');
$this->assertTrue($this->conf->exists('foo.bar'));
$this->assertTrue($this->conf->exists('foo.bar.key.stuff'));
$this->assertEquals('testSetDeleteNested', $this->conf->get('foo.bar.key.stuff'));
$this->conf->remove('foo.bar');
$this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
$this->assertFalse($this->conf->exists('foo.bar'));
}
/** /**
* Set with an empty key. * Set with an empty key.
* *
@ -103,6 +115,17 @@ public function testSetArrayKey()
$this->conf->set(array('foo' => 'bar'), 'stuff'); $this->conf->set(array('foo' => 'bar'), 'stuff');
} }
/**
* Remove with an empty key.
*
* @expectedException \Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/
public function testRmoveEmptyKey()
{
$this->conf->remove('');
}
/** /**
* Try to write the config without mandatory parameter (e.g. 'login'). * Try to write the config without mandatory parameter (e.g. 'login').
* *