Commit 994e2c0a authored by Philipp Schaffrath's avatar Philipp Schaffrath Committed by GitHub
Browse files

Override config.php values through ENV variables (#26570)

* added functionality to override config.php values with 'OC_' prefixed environment variables

* use getenv to read environment variables since apache does not set $_ENV variables, fixed test
parent aa92a65a
......@@ -38,6 +38,9 @@ namespace OC;
* configuration file of ownCloud.
*/
class Config {
const ENV_PREFIX = 'OC_';
/** @var array Associative array ($key => $value) */
protected $cache = [];
/** @var string */
......@@ -70,15 +73,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 `OC_` 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 mixed $default = null default value
* @return mixed the value or $default
*/
public function getValue($key, $default = null) {
$envValue = getenv(self::ENV_PREFIX . $key);
if ($envValue) {
return $envValue;
}
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
......
......@@ -48,6 +48,12 @@ class ConfigTest extends TestCase {
$this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $this->config->getValue('beers'));
}
public function testGetValueReturnsEnvironmentValueIfSet() {
$this->assertEquals('bar', $this->config->getValue('foo'));
putenv('OC_foo=baz');
$this->assertEquals('baz', $this->config->getValue('foo'));
}
public function testSetValue() {
$this->config->setValue('foo', 'moo');
$expectedConfig = $this->initialConfig;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment