Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] fix(config): Add missing handling for envCache in getKeys() #48100

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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])) {
Expand Down Expand Up @@ -257,7 +256,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;
}
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Loading