Skip to content

Commit

Permalink
fix(appconfig): format app values
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Mar 14, 2024
1 parent d3da21b commit 0443c8f
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ public function getAllValues(string $app, string $prefix = '', bool $filtered =
// if we want to filter values, we need to get sensitivity
$this->loadConfigAll();
// array_merge() will remove numeric keys (here config keys), so addition arrays instead
$values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? []));
$values = array_filter(
(($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])),
$values,
function (string $key) use ($prefix): bool {
return str_starts_with($key, $prefix); // filter values based on $prefix
}, ARRAY_FILTER_USE_KEY
Expand Down Expand Up @@ -271,7 +272,8 @@ public function searchValues(string $key, bool $lazy = false): array {

foreach (array_keys($cache) as $app) {
if (isset($cache[$app][$key])) {
$values[$app] = $cache[$app][$key];
$appCache = $this->formatAppValues((string)$app, $cache[$app]);
$values[$app] = $appCache[$key];
}
}

Expand Down Expand Up @@ -1386,6 +1388,38 @@ public function getFilteredValues($app) {
return $this->getAllValues($app, filtered: true);
}


/**
* @param string $app
* @param array $values
*
* @return array
*/
private function formatAppValues(string $app, array $values): array {
foreach($values as $key => $value) {
switch ($this->getValueType($app, $key)) {
case self::VALUE_INT:
$values[$key] = (int)$value;
break;
case self::VALUE_FLOAT:
$values[$key] = (float)$value;
break;
case self::VALUE_BOOL:
$values[$key] = in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
break;
case self::VALUE_ARRAY:
try {
$values[$key] = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// ignoreable
}
break;
}
}

return $values;
}

/**
* @param string $app
*
Expand Down

0 comments on commit 0443c8f

Please sign in to comment.