aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2024-09-16 18:54:23 +0200
committerGitHub <noreply@github.com>2024-09-16 18:54:23 +0200
commit0a20c69c85cdf8f8b07bdb12b836752a32898304 (patch)
tree243704d7c7f31ad9fede3761b3d53746aa7de7d8
parent1c7b5c83ff391bd6e410937c5e1ac3b6d0e8c323 (diff)
parent75555d34b19d5eef96c4cc4fe2bd2033ad5ee6a5 (diff)
downloadnextcloud-server-0a20c69c85cdf8f8b07bdb12b836752a32898304.tar.gz
nextcloud-server-0a20c69c85cdf8f8b07bdb12b836752a32898304.zip
Merge pull request #48100 from nextcloud/backport/46140/stable29
[stable29] 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 278c6816e70..d69a9837e75 100644
--- a/lib/private/Config.php
+++ b/lib/private/Config.php
@@ -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;
+ }
+ }
}
/**
diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php
index 7c289472abd..22860f6f130 100644
--- a/tests/lib/ConfigTest.php
+++ b/tests/lib/ConfigTest.php
@@ -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'));