Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added functionality to override config.php values with 'OC_' prefixed…
… environment variables
  • Loading branch information
philippschaffrath authored and phisch committed Jan 24, 2017
commit 07016159a6eb81f1c47a0aaccdcda78a230b37a0
14 changes: 12 additions & 2 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
* configuration file of ownCloud.
*/
class Config {

const ENV_PREFIX = 'OC_';

/** @var array Associative array ($key => $value) */
protected $cache = [];
/** @var string */
Expand Down Expand Up @@ -70,15 +73,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 `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) {
$envKey = 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.

use getenv()? makes it case insensitive and searches $_SERVER and $_ENV (which might not be populated). See http://stackoverflow.com/a/27077452

Copy link
Contributor Author

@phisch phisch Nov 8, 2016

Choose a reason for hiding this comment

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

You are right! Fixed that and the tests.
Seems like environment variables set by Apache are case insensitive, those set by putenv are not. Just php things...

if (isset($_ENV[$envKey])) {
return $_ENV[$envKey];
}

if (isset($this->cache[$key])) {
return $this->cache[$key];
}
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public function testGetValue() {
$this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $this->config->getValue('beers'));
}

public function testGetValueReturnsEnvironmentValueIfSet() {
$this->assertEquals('bar', $this->config->getValue('foo'));
$_ENV['OC_foo'] = 'baz';
$this->assertEquals('baz', $this->config->getValue('foo'));
}

public function testSetValue() {
$this->config->setValue('foo', 'moo');
$expectedConfig = $this->initialConfig;
Expand Down