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

[IAppConfig] format values based on type #43258

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 48 additions & 4 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], $lazy);
$values[$app] = $appCache[$key];
}
}

Expand Down Expand Up @@ -510,9 +512,9 @@ private function getTypedValue(
* @see VALUE_BOOL
* @see VALUE_ARRAY
*/
public function getValueType(string $app, string $key): int {
public function getValueType(string $app, string $key, ?bool $lazy = null): int {
$this->assertParams($app, $key);
$this->loadConfigAll();
$this->loadConfig($lazy);

if (!isset($this->valueTypes[$app][$key])) {
throw new AppConfigUnknownKeyException('unknown config key');
Expand Down Expand Up @@ -1386,6 +1388,48 @@ public function getFilteredValues($app) {
return $this->getAllValues($app, filtered: true);
}


/**
* **Warning:** avoid default NULL value for $lazy as this will
* load all lazy values from the database
*
* @param string $app
* @param array $values
* @param bool|null $lazy
*
* @return array
*/
private function formatAppValues(string $app, array $values, ?bool $lazy = null): array {
foreach($values as $key => $value) {
try {
$type = $this->getValueType($app, $key, $lazy);
} catch (AppConfigUnknownKeyException $e) {
continue;
}

switch ($type) {
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
}
ArtificialOwl marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}

return $values;
}

/**
* @param string $app
*
Expand Down
4 changes: 3 additions & 1 deletion lib/public/IAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ public function getValueArray(string $app, string $key, array $default = [], boo
* returns the type of config value
*
* **WARNING:** ignore lazy filtering, all config values are loaded from database
* unless lazy is set to false
*
* @param string $app id of the app
* @param string $key config key
* @param bool|null $lazy
*
* @return int
* @throws AppConfigUnknownKeyException
Expand All @@ -274,7 +276,7 @@ public function getValueArray(string $app, string $key, array $default = [], boo
* @see VALUE_BOOL
* @see VALUE_ARRAY
*/
public function getValueType(string $app, string $key): int;
public function getValueType(string $app, string $key, ?bool $lazy = null): int;

/**
* Store a config key and its value in database
Expand Down
Loading