diff --git a/lib/private/Config.php b/lib/private/Config.php index 6823ab7027fdc..08f9b028856b7 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -49,7 +49,7 @@ public function __construct($configDir, $fileName = 'config.php') { * @return array an array of key names */ public function getKeys() { - return array_keys($this->cache); + return array_merge(array_keys($this->cache), array_keys($this->envCache)); } /** @@ -64,9 +64,8 @@ public function getKeys() { * @return mixed the value or $default */ public function getValue($key, $default = null) { - $envKey = self::ENV_PREFIX . $key; - if (isset($this->envCache[$envKey])) { - return $this->envCache[$envKey]; + if (isset($this->envCache[$key])) { + return $this->envCache[$key]; } if (isset($this->cache[$key])) { @@ -226,7 +225,16 @@ private function readData() { } } - $this->envCache = getenv(); + // grab any "NC_" environment variables + $envRaw = getenv(); + // only save environment variables prefixed with "NC_" in the cache + $envPrefixLen = strlen(self::ENV_PREFIX); + foreach ($envRaw as $rawEnvKey => $rawEnvValue) { + if (str_starts_with($rawEnvKey, self::ENV_PREFIX)) { + $realKey = substr($rawEnvKey, $envPrefixLen); + $this->envCache[$realKey] = $rawEnvValue; + } + } } /** diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 60006aa43de1e..94ee8da5dec22 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -41,6 +41,13 @@ public function testGetKeys(): void { $this->assertSame($expectedConfig, $this->getConfig()->getKeys()); } + public function testGetKeysReturnsEnvironmentKeysIfSet() { + $expectedConfig = ['foo', 'beers', 'alcohol_free', 'taste']; + putenv('NC_taste=great'); + $this->assertSame($expectedConfig, $this->getConfig()->getKeys()); + putenv('NC_taste'); + } + public function testGetValue(): void { $config = $this->getConfig(); $this->assertSame('bar', $config->getValue('foo'));