From 4b9a74658dddae9bdd1757f5a88f932316c21cd0 Mon Sep 17 00:00:00 2001 From: Josh Richards Date: Wed, 26 Jun 2024 09:31:44 -0400 Subject: [PATCH 1/3] fix(config): Add missing handling for envCache in getKeys() NC_ env variable overrides were not appearing in the output of `occ config:list system` nor `occ config:system:get xxx`. This was creating nearly impossible to diagnose configuration/ behavior disprepancies. - Refactored readData() so that we aren't saving the entire environment in the envCache anymore (only those prefixed "NC_") and so that we save NC_ provided config values under their real key. - Refactored getValue() to accommodate readData() refactor - Fixed getKeys() to properly return envCache keys too Environment provided config variables now appear in `occ config:list system` as expected. Environment provided config variables now appear when queried via `occ config:system:get KEY` envCache is now free of non-NC stuff. Signed-off-by: Josh Richards --- lib/private/Config.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index 278c6816e70ea..301edcd8c1152 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -80,7 +80,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)); } /** @@ -95,9 +95,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])) { @@ -257,7 +256,15 @@ private function readData() { } } - $this->envCache = getenv(); + // grab any "NC_" environment variables + $envRaw = getenv(); + // only save environment variables prefixed with "NC_" in the cache + foreach ($envRaw as $rawEnvKey => $rawEnvValue) { + if (str_starts_with($rawEnvKey, self::ENV_PREFIX)) { + $realKey = explode(self::ENV_PREFIX, $rawEnvKey)[1]; + $this->envCache[$realKey] = $rawEnvValue; + } + } } /** From 80bbffa78a7c826493f8a68439f467a26780477b Mon Sep 17 00:00:00 2001 From: Josh Richards Date: Wed, 26 Jun 2024 10:16:26 -0400 Subject: [PATCH 2/3] fix(config): add envCache tests for getKeys() Signed-off-by: Josh Richards --- tests/lib/ConfigTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 7c289472abd4e..22860f6f130c8 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -42,6 +42,13 @@ public function testGetKeys() { $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')); From 75555d34b19d5eef96c4cc4fe2bd2033ad5ee6a5 Mon Sep 17 00:00:00 2001 From: Josh Richards Date: Thu, 27 Jun 2024 10:57:14 -0400 Subject: [PATCH 3/3] fix: switch from explode to substr (faster) Signed-off-by: Josh Richards --- lib/private/Config.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/private/Config.php b/lib/private/Config.php index 301edcd8c1152..d69a9837e7543 100644 --- a/lib/private/Config.php +++ b/lib/private/Config.php @@ -259,9 +259,10 @@ private function readData() { // 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 = explode(self::ENV_PREFIX, $rawEnvKey)[1]; + $realKey = substr($rawEnvKey, $envPrefixLen); $this->envCache[$realKey] = $rawEnvValue; } }