Add a method to set/unset multiple config values in one call
This reduces the number of file writes we do for config.php and therefor hopefully helps lowering the chances for empty config.php files
This commit is contained in:
parent
bc93a8f140
commit
c61e9f3912
|
@ -69,6 +69,16 @@ class AllConfig implements \OCP\IConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets and deletes system wide values
|
||||||
|
*
|
||||||
|
* @param array $configs Associative array with `key => value` pairs
|
||||||
|
* If value is null, the config key will be deleted
|
||||||
|
*/
|
||||||
|
public function setSystemValues(array $configs) {
|
||||||
|
$this->systemConfig->setValues($configs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new system wide value
|
* Sets a new system wide value
|
||||||
*
|
*
|
||||||
|
|
|
@ -41,10 +41,10 @@ class Config {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists all available config keys
|
* Lists all available config keys
|
||||||
* @return array an array of key names
|
|
||||||
*
|
*
|
||||||
* This function returns all keys saved in config.php. Please note that it
|
* Please note that it does not return the values.
|
||||||
* does not return the values.
|
*
|
||||||
|
* @return array an array of key names
|
||||||
*/
|
*/
|
||||||
public function getKeys() {
|
public function getKeys() {
|
||||||
return array_keys($this->cache);
|
return array_keys($this->cache);
|
||||||
|
@ -52,12 +52,12 @@ class Config {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a value from config.php
|
* Gets a value from config.php
|
||||||
|
*
|
||||||
|
* If it does not exist, $default will be returned.
|
||||||
|
*
|
||||||
* @param string $key key
|
* @param string $key key
|
||||||
* @param mixed $default = null default value
|
* @param mixed $default = null default value
|
||||||
* @return mixed the value or $default
|
* @return mixed the value or $default
|
||||||
*
|
|
||||||
* This function gets the value from config.php. If it does not exist,
|
|
||||||
* $default will be returned.
|
|
||||||
*/
|
*/
|
||||||
public function getValue($key, $default = null) {
|
public function getValue($key, $default = null) {
|
||||||
if (isset($this->cache[$key])) {
|
if (isset($this->cache[$key])) {
|
||||||
|
@ -68,38 +68,83 @@ class Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a value
|
* Sets and deletes values and writes the config.php
|
||||||
* @param string $key key
|
|
||||||
* @param mixed $value value
|
|
||||||
*
|
|
||||||
* This function sets the value and writes the config.php.
|
|
||||||
*
|
*
|
||||||
|
* @param array $configs Associative array with `key => value` pairs
|
||||||
|
* If value is null, the config key will be deleted
|
||||||
*/
|
*/
|
||||||
public function setValue($key, $value) {
|
public function setValues(array $configs) {
|
||||||
// Add change
|
$needsUpdate = false;
|
||||||
$this->cache[$key] = $value;
|
foreach ($configs as $key => $value) {
|
||||||
|
if ($value !== null) {
|
||||||
|
$needsUpdate |= $this->set($key, $value);
|
||||||
|
} else {
|
||||||
|
$needsUpdate |= $this->delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($needsUpdate) {
|
||||||
// Write changes
|
// Write changes
|
||||||
$this->writeData();
|
$this->writeData();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a key from the config
|
* Sets the value and writes it to config.php if required
|
||||||
|
*
|
||||||
* @param string $key key
|
* @param string $key key
|
||||||
*
|
* @param mixed $value value
|
||||||
* This function removes a key from the config.php.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public function deleteKey($key) {
|
public function setValue($key, $value) {
|
||||||
if (isset($this->cache[$key])) {
|
if ($this->set($key, $value)) {
|
||||||
// Delete key from cache
|
|
||||||
unset($this->cache[$key]);
|
|
||||||
|
|
||||||
// Write changes
|
// Write changes
|
||||||
$this->writeData();
|
$this->writeData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function sets the value
|
||||||
|
*
|
||||||
|
* @param string $key key
|
||||||
|
* @param mixed $value value
|
||||||
|
* @return bool True if the file needs to be updated, false otherwise
|
||||||
|
*/
|
||||||
|
protected function set($key, $value) {
|
||||||
|
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
|
||||||
|
// Add change
|
||||||
|
$this->cache[$key] = $value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a key from the config and removes it from config.php if required
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function deleteKey($key) {
|
||||||
|
if ($this->delete($key)) {
|
||||||
|
// Write changes
|
||||||
|
$this->writeData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function removes a key from the config
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return bool True if the file needs to be updated, false otherwise
|
||||||
|
*/
|
||||||
|
protected function delete($key) {
|
||||||
|
if (isset($this->cache[$key])) {
|
||||||
|
// Delete key from cache
|
||||||
|
unset($this->cache[$key]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the config file
|
* Loads the config file
|
||||||
*
|
*
|
||||||
|
|
|
@ -58,6 +58,16 @@ class OC_Config {
|
||||||
self::$object->setValue($key, $value);
|
self::$object->setValue($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets and deletes values and writes the config.php
|
||||||
|
*
|
||||||
|
* @param array $configs Associative array with `key => value` pairs
|
||||||
|
* If value is null, the config key will be deleted
|
||||||
|
*/
|
||||||
|
public static function setValues(array $configs) {
|
||||||
|
self::$object->setValues($configs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a key from the config
|
* Removes a key from the config
|
||||||
* @param string $key key
|
* @param string $key key
|
||||||
|
|
|
@ -27,6 +27,16 @@ class SystemConfig {
|
||||||
\OC_Config::setValue($key, $value);
|
\OC_Config::setValue($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets and deletes values and writes the config.php
|
||||||
|
*
|
||||||
|
* @param array $configs Associative array with `key => value` pairs
|
||||||
|
* If value is null, the config key will be deleted
|
||||||
|
*/
|
||||||
|
public function setValues(array $configs) {
|
||||||
|
\OC_Config::setValues($configs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Looks up a system wide defined value
|
* Looks up a system wide defined value
|
||||||
*
|
*
|
||||||
|
|
|
@ -34,6 +34,14 @@ namespace OCP;
|
||||||
* Access to all the configuration options ownCloud offers
|
* Access to all the configuration options ownCloud offers
|
||||||
*/
|
*/
|
||||||
interface IConfig {
|
interface IConfig {
|
||||||
|
/**
|
||||||
|
* Sets and deletes system wide values
|
||||||
|
*
|
||||||
|
* @param array $configs Associative array with `key => value` pairs
|
||||||
|
* If value is null, the config key will be deleted
|
||||||
|
*/
|
||||||
|
public function setSystemValues(array $configs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a new system wide value
|
* Sets a new system wide value
|
||||||
*
|
*
|
||||||
|
|
|
@ -71,6 +71,36 @@ class Test_Config extends \Test\TestCase {
|
||||||
$this->assertEquals($expected, $content);
|
$this->assertEquals($expected, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetValues() {
|
||||||
|
$content = file_get_contents($this->configFile);
|
||||||
|
$this->assertEquals(self::TESTCONTENT, $content);
|
||||||
|
|
||||||
|
// Changing configs to existing values and deleting non-existing once
|
||||||
|
// should not rewrite the config.php
|
||||||
|
$this->config->setValues([
|
||||||
|
'foo' => 'bar',
|
||||||
|
'not_exists' => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertAttributeEquals($this->initialConfig, 'cache', $this->config);
|
||||||
|
$content = file_get_contents($this->configFile);
|
||||||
|
$this->assertEquals(self::TESTCONTENT, $content);
|
||||||
|
|
||||||
|
$this->config->setValues([
|
||||||
|
'foo' => 'moo',
|
||||||
|
'alcohol_free' => null,
|
||||||
|
]);
|
||||||
|
$expectedConfig = $this->initialConfig;
|
||||||
|
$expectedConfig['foo'] = 'moo';
|
||||||
|
unset($expectedConfig['alcohol_free']);
|
||||||
|
$this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
|
||||||
|
|
||||||
|
$content = file_get_contents($this->configFile);
|
||||||
|
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
||||||
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
|
||||||
|
$this->assertEquals($expected, $content);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDeleteKey() {
|
public function testDeleteKey() {
|
||||||
$this->config->deleteKey('foo');
|
$this->config->deleteKey('foo');
|
||||||
$expectedConfig = $this->initialConfig;
|
$expectedConfig = $this->initialConfig;
|
||||||
|
|
Loading…
Reference in New Issue