Skip to content

Commit 01f3698

Browse files
authored
Merge pull request #3966 from nextcloud/downstream-26570
Override config.php values through environment variables
2 parents 7cb6038 + 95a21e2 commit 01f3698

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

lib/private/Config.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
* configuration file of ownCloud.
4040
*/
4141
class Config {
42+
43+
const ENV_PREFIX = 'NC_';
44+
4245
/** @var array Associative array ($key => $value) */
4346
protected $cache = array();
4447
/** @var string */
@@ -71,15 +74,22 @@ public function getKeys() {
7174
}
7275

7376
/**
74-
* Gets a value from config.php
77+
* Returns a config value
7578
*
76-
* If it does not exist, $default will be returned.
79+
* gets its value from an `NC_` prefixed environment variable
80+
* if it doesn't exist from config.php
81+
* if this doesn't exist either, it will return the given `$default`
7782
*
7883
* @param string $key key
7984
* @param mixed $default = null default value
8085
* @return mixed the value or $default
8186
*/
8287
public function getValue($key, $default = null) {
88+
$envValue = getenv(self::ENV_PREFIX . $key);
89+
if ($envValue !== false) {
90+
return $envValue;
91+
}
92+
8393
if (isset($this->cache[$key])) {
8494
return $this->cache[$key];
8595
}

tests/lib/ConfigTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ public function testGetValue() {
4848
$this->assertSame(array('Appenzeller', 'Guinness', 'Kölsch'), $this->config->getValue('beers'));
4949
}
5050

51+
public function testGetValueReturnsEnvironmentValueIfSet() {
52+
$this->assertEquals('bar', $this->config->getValue('foo'));
53+
putenv('NC_foo=baz');
54+
$this->assertEquals('baz', $this->config->getValue('foo'));
55+
putenv('NC_foo'); // unset the env variable
56+
}
57+
58+
public function testGetValueReturnsEnvironmentValueIfSetToZero() {
59+
$this->assertEquals('bar', $this->config->getValue('foo'));
60+
putenv('NC_foo=0');
61+
$this->assertEquals('0', $this->config->getValue('foo'));
62+
putenv('NC_foo'); // unset the env variable
63+
}
64+
65+
public function testGetValueReturnsEnvironmentValueIfSetToFalse() {
66+
$this->assertEquals('bar', $this->config->getValue('foo'));
67+
putenv('NC_foo=false');
68+
$this->assertEquals('false', $this->config->getValue('foo'));
69+
putenv('NC_foo'); // unset the env variable
70+
}
71+
5172
public function testSetValue() {
5273
$this->config->setValue('foo', 'moo');
5374
$expectedConfig = $this->initialConfig;

0 commit comments

Comments
 (0)