Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
* configuration file of ownCloud.
*/
class Config {

const ENV_PREFIX = 'NC_';

/** @var array Associative array ($key => $value) */
protected $cache = array();
/** @var string */
Expand Down Expand Up @@ -71,15 +74,22 @@ public function getKeys() {
}

/**
* 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 mixed $default = null default value
* @return mixed the value or $default
*/
public function getValue($key, $default = null) {
$envValue = getenv(self::ENV_PREFIX . $key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we cache this as well? I think it makes sense. otherwise we might call this hunderd times

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem is: How do we know that we need to cache a ENV variable? I can move it below the cache statement, but then the ENV variable would not overwrite config.php values. I guess this is fine. Don't set the config option in config.php, but only in the ENV and it will be used from there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also just add $this->envCache

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this would not solve anything for empty env variables, because they are not cached then and the getenv would happen all the time for them.

if ($envValue !== false) {
return $envValue;
}

if (isset($this->cache[$key])) {
return $this->cache[$key];
}
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ public function testGetValue() {
$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() {
$this->config->setValue('foo', 'moo');
$expectedConfig = $this->initialConfig;
Expand Down