Merge pull request #3966 from nextcloud/downstream-26570
Override config.php values through environment variables
This commit is contained in:
commit
01f3698175
|
@ -39,6 +39,9 @@ namespace OC;
|
||||||
* configuration file of ownCloud.
|
* configuration file of ownCloud.
|
||||||
*/
|
*/
|
||||||
class Config {
|
class Config {
|
||||||
|
|
||||||
|
const ENV_PREFIX = 'NC_';
|
||||||
|
|
||||||
/** @var array Associative array ($key => $value) */
|
/** @var array Associative array ($key => $value) */
|
||||||
protected $cache = array();
|
protected $cache = array();
|
||||||
/** @var string */
|
/** @var string */
|
||||||
|
@ -71,15 +74,22 @@ class Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a value from config.php
|
* Returns a config value
|
||||||
*
|
*
|
||||||
* If it does not exist, $default will be returned.
|
* gets its value from an `NC_` prefixed environment variable
|
||||||
|
* if it doesn't exist from config.php
|
||||||
|
* if this doesn't exist either, it will return the given `$default`
|
||||||
*
|
*
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
public function getValue($key, $default = null) {
|
public function getValue($key, $default = null) {
|
||||||
|
$envValue = getenv(self::ENV_PREFIX . $key);
|
||||||
|
if ($envValue !== false) {
|
||||||
|
return $envValue;
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($this->cache[$key])) {
|
if (isset($this->cache[$key])) {
|
||||||
return $this->cache[$key];
|
return $this->cache[$key];
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,27 @@ class ConfigTest extends TestCase {
|
||||||
$this->assertSame(array('Appenzeller', 'Guinness', 'Kölsch'), $this->config->getValue('beers'));
|
$this->assertSame(array('Appenzeller', 'Guinness', 'Kölsch'), $this->config->getValue('beers'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetValueReturnsEnvironmentValueIfSet() {
|
||||||
|
$this->assertEquals('bar', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo=baz');
|
||||||
|
$this->assertEquals('baz', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo'); // unset the env variable
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetValueReturnsEnvironmentValueIfSetToZero() {
|
||||||
|
$this->assertEquals('bar', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo=0');
|
||||||
|
$this->assertEquals('0', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo'); // unset the env variable
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetValueReturnsEnvironmentValueIfSetToFalse() {
|
||||||
|
$this->assertEquals('bar', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo=false');
|
||||||
|
$this->assertEquals('false', $this->config->getValue('foo'));
|
||||||
|
putenv('NC_foo'); // unset the env variable
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetValue() {
|
public function testSetValue() {
|
||||||
$this->config->setValue('foo', 'moo');
|
$this->config->setValue('foo', 'moo');
|
||||||
$expectedConfig = $this->initialConfig;
|
$expectedConfig = $this->initialConfig;
|
||||||
|
|
Loading…
Reference in New Issue