From 695a17804ed261980e9c3c20a588c1f3a3a3507f Mon Sep 17 00:00:00 2001 From: Philipp Schaffrath Date: Tue, 24 Jan 2017 12:18:29 +0100 Subject: [PATCH] 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 Signed-off-by: Morris Jobke --- lib/private/Config.php | 14 ++++++++++++-- tests/lib/ConfigTest.php | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index e6a27a76b2..28d8c560c5 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -39,6 +39,9 @@ namespace OC; * configuration file of ownCloud. */ class Config { + + const ENV_PREFIX = 'OC_'; + /** @var array Associative array ($key => $value) */ protected $cache = array(); /** @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 `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]; } diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 74dcdc192c..f7add114aa 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -48,6 +48,12 @@ class ConfigTest extends TestCase { $this->assertSame(array('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;