aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2024-09-16 18:59:32 +0200
committerGitHub <noreply@github.com>2024-09-16 18:59:32 +0200
commitc4eb1ac8fe2ceae5141b3b8748b6e7cd575d6284 (patch)
treeb7b80280c3cc1e446e9c7e7825012ae7207d28cf
parentcea10d643ec4605e9f03231828b102034dcf2674 (diff)
parent344c9682de06134faae31499fe38afb2df55903a (diff)
downloadnextcloud-server-c4eb1ac8fe2ceae5141b3b8748b6e7cd575d6284.tar.gz
nextcloud-server-c4eb1ac8fe2ceae5141b3b8748b6e7cd575d6284.zip
Merge pull request #48101 from nextcloud/backport/46140/stable30
[stable30] fix(config): Add missing handling for `envCache` in `getKeys()`
-rw-r--r--lib/private/Config.php18
-rw-r--r--tests/lib/ConfigTest.php7
2 files changed, 20 insertions, 5 deletions
diff --git a/lib/private/Config.php b/lib/private/Config.php
index d37c5c38995..aa20f627edc 100644
--- a/lib/private/Config.php
+++ b/lib/private/Config.php
@@ -49,7 +49,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));
}
/**
@@ -64,9 +64,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])) {
@@ -226,7 +225,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;
+ }
+ }
}
/**
diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php
index e65cf7633e1..e5d0c94003b 100644
--- a/tests/lib/ConfigTest.php
+++ b/tests/lib/ConfigTest.php
@@ -41,6 +41,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'));