From 97e59b12a13fcaf51280818c176a2631ebc1d3a4 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 4 Apr 2024 17:40:31 -0100 Subject: [PATCH] fix(appconfig): only convert single entry on searchValues() Signed-off-by: Maxence Lange --- lib/private/AppConfig.php | 64 +++++++++++-------- .../AppFramework/Services/AppConfig.php | 2 +- .../AppFramework/Services/IAppConfig.php | 2 +- lib/public/IAppConfig.php | 5 +- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index 3c6993201b5..c72a4073832 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -211,7 +211,7 @@ class AppConfig implements IAppConfig { * @param string $prefix config keys prefix to search * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} * - * @return array [configKey => configValue] + * @return array [configKey => configValue] * @since 29.0.0 */ public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array { @@ -254,14 +254,14 @@ class AppConfig implements IAppConfig { * * @param string $key config key * @param bool $lazy search within lazy loaded config + * @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others) * - * @return array [appId => configValue] + * @return array [appId => configValue] * @since 29.0.0 */ - public function searchValues(string $key, bool $lazy = false): array { + public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array { $this->assertParams('', $key, true); $this->loadConfig($lazy); - $values = []; /** @var array> $cache */ if ($lazy) { @@ -270,10 +270,10 @@ class AppConfig implements IAppConfig { $cache = $this->fastCache; } + $values = []; foreach (array_keys($cache) as $app) { if (isset($cache[$app][$key])) { - $appCache = $this->formatAppValues((string)$app, $cache[$app], $lazy); - $values[$app] = $appCache[$key]; + $values[$app] = $this->convertTypedValue($cache[$app][$key], $typedAs ?? $this->getValueType((string)$app, $key, $lazy)); } } @@ -1371,7 +1371,7 @@ class AppConfig implements IAppConfig { $key = ($key === false) ? '' : $key; if (!$app) { - return $this->searchValues($key); + return $this->searchValues($key, false, self::VALUE_MIXED); } else { return $this->getAllValues($app, $key); } @@ -1395,10 +1395,10 @@ class AppConfig implements IAppConfig { * load all lazy values from the database * * @param string $app - * @param array $values + * @param array $values ['key' => 'value'] * @param bool|null $lazy * - * @return array + * @return array */ private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { foreach($values as $key => $value) { @@ -1408,29 +1408,39 @@ class AppConfig implements IAppConfig { 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 - } - break; - } + $values[$key] = $this->convertTypedValue($value, $type); } return $values; } + /** + * convert string value to the expected type + * + * @param string $value + * @param int $type + * + * @return string|int|float|bool|array + */ + private function convertTypedValue(string $value, int $type): string|int|float|bool|array { + switch ($type) { + case self::VALUE_INT: + return (int)$value; + case self::VALUE_FLOAT: + return (float)$value; + case self::VALUE_BOOL: + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); + case self::VALUE_ARRAY: + try { + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); + } catch (JsonException $e) { + // ignoreable + } + break; + } + return $value; + } + /** * @param string $app * diff --git a/lib/private/AppFramework/Services/AppConfig.php b/lib/private/AppFramework/Services/AppConfig.php index 1d18baef9ed..d3dfd3d362b 100644 --- a/lib/private/AppFramework/Services/AppConfig.php +++ b/lib/private/AppFramework/Services/AppConfig.php @@ -97,7 +97,7 @@ class AppConfig implements IAppConfig { * @param string $key config keys prefix to search * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} * - * @return array [configKey => configValue] + * @return array [configKey => configValue] * @since 29.0.0 */ public function getAllAppValues(string $key = '', bool $filtered = false): array { diff --git a/lib/public/AppFramework/Services/IAppConfig.php b/lib/public/AppFramework/Services/IAppConfig.php index 9340fdd3c32..74588ef2c99 100644 --- a/lib/public/AppFramework/Services/IAppConfig.php +++ b/lib/public/AppFramework/Services/IAppConfig.php @@ -88,7 +88,7 @@ interface IAppConfig { * @param string $key config keys prefix to search, can be empty. * @param bool $filtered filter sensitive config values * - * @return array [configKey => configValue] + * @return array [configKey => configValue] * @since 29.0.0 */ public function getAllAppValues(string $key = '', bool $filtered = false): array; diff --git a/lib/public/IAppConfig.php b/lib/public/IAppConfig.php index 421a735ef06..10fc7c90650 100644 --- a/lib/public/IAppConfig.php +++ b/lib/public/IAppConfig.php @@ -140,7 +140,7 @@ interface IAppConfig { * @param string $prefix config keys prefix to search, can be empty. * @param bool $filtered filter sensitive config values * - * @return array [configKey => configValue] + * @return array [configKey => configValue] * @since 29.0.0 */ public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array; @@ -151,11 +151,12 @@ interface IAppConfig { * * @param string $key config key * @param bool $lazy search within lazy loaded config + * @param int|null $typedAs enforce type for the returned values {@see self::VALUE_STRING} and others * * @return array [appId => configValue] * @since 29.0.0 */ - public function searchValues(string $key, bool $lazy = false): array; + public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array; /** * Get config value assigned to a config key.