summaryrefslogtreecommitdiffstats
path: root/lib/private/config.php
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@gmx.de>2015-01-23 10:50:25 +0100
committerJoas Schilling <nickvergessen@gmx.de>2015-01-23 11:00:53 +0100
commitc61e9f391273e5f7f4b7aa91ee4174d47402cae9 (patch)
tree1993ccdd315b3ea8d14123ee3514fde2b8707d4f /lib/private/config.php
parentbc93a8f14094dfb59c826ffa76d7b046370b0410 (diff)
downloadnextcloud-server-c61e9f391273e5f7f4b7aa91ee4174d47402cae9.tar.gz
nextcloud-server-c61e9f391273e5f7f4b7aa91ee4174d47402cae9.zip
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
Diffstat (limited to 'lib/private/config.php')
-rw-r--r--lib/private/config.php89
1 files changed, 67 insertions, 22 deletions
diff --git a/lib/private/config.php b/lib/private/config.php
index 8544de34b72..586e8c20587 100644
--- a/lib/private/config.php
+++ b/lib/private/config.php
@@ -41,10 +41,10 @@ class Config {
/**
* 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
- * does not return the values.
+ * Please note that it does not return the values.
+ *
+ * @return array an array of key names
*/
public function getKeys() {
return array_keys($this->cache);
@@ -52,12 +52,12 @@ class Config {
/**
* Gets a value from config.php
+ *
+ * If it does not exist, $default will be returned.
+ *
* @param string $key key
* @param mixed $default = null default value
* @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) {
if (isset($this->cache[$key])) {
@@ -68,36 +68,81 @@ class Config {
}
/**
- * Sets a value
- * @param string $key key
- * @param mixed $value value
- *
- * This function sets the value and writes the config.php.
+ * 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 setValue($key, $value) {
- // Add change
- $this->cache[$key] = $value;
+ public function setValues(array $configs) {
+ $needsUpdate = false;
+ foreach ($configs as $key => $value) {
+ if ($value !== null) {
+ $needsUpdate |= $this->set($key, $value);
+ } else {
+ $needsUpdate |= $this->delete($key);
+ }
+ }
- // Write changes
- $this->writeData();
+ if ($needsUpdate) {
+ // Write changes
+ $this->writeData();
+ }
}
/**
- * Removes a key from the config
- * @param string $key key
+ * Sets the value and writes it to config.php if required
*
- * This function removes a key from the config.php.
+ * @param string $key key
+ * @param mixed $value value
+ */
+ public function setValue($key, $value) {
+ if ($this->set($key, $value)) {
+ // Write changes
+ $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]);
-
- // Write changes
- $this->writeData();
+ return true;
}
+ return false;
}
/**