Merge pull request #48099 from nextcloud/backport/46140/stable28

[stable28] fix(config): Add missing handling for `envCache` in `getKeys()`
pull/48281/head
Robin Appelman 2024-09-23 07:27:44 +07:00 committed by GitHub
commit 6baa60a5dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 5 deletions

@ -80,7 +80,7 @@ class Config {
* @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));
}
/**
@ -95,9 +95,8 @@ class Config {
* @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])) {
@ -257,7 +256,16 @@ class Config {
}
}
$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;
}
}
}
/**

@ -42,6 +42,13 @@ class ConfigTest extends TestCase {
$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() {
$config = $this->getConfig();
$this->assertSame('bar', $config->getValue('foo'));