Skip to content

Commit

Permalink
fix(appconfig): only convert single entry on searchValues()
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 4, 2024
1 parent 7ad2461 commit 40d7716
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
64 changes: 37 additions & 27 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function isLazy(string $app, string $key): bool {
* @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<string, string> [configKey => configValue]
* @return array<string, string|int|float|bool|array> [configKey => configValue]
* @since 29.0.0
*/
public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array {
Expand Down Expand Up @@ -254,14 +254,14 @@ function (string $key) use ($prefix): bool {
*
* @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<string, string> [appId => configValue]
* @return array<string, string|int|float|bool|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<array-key, array<array-key, mixed>> $cache */
if ($lazy) {
Expand All @@ -270,10 +270,10 @@ public function searchValues(string $key, bool $lazy = false): array {
$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));
}
}

Expand Down Expand Up @@ -1371,7 +1371,7 @@ public function getValues($app, $key) {

$key = ($key === false) ? '' : $key;
if (!$app) {
return $this->searchValues($key);
return $this->searchValues($key, false, self::VALUE_MIXED);
} else {
return $this->getAllValues($app, $key);
}
Expand All @@ -1395,10 +1395,10 @@ public function getFilteredValues($app) {
* load all lazy values from the database
*
* @param string $app
* @param array $values
* @param array<string, string> $values ['key' => 'value']
* @param bool|null $lazy
*
* @return array
* @return array<string, string|int|float|bool|array>
*/
private function formatAppValues(string $app, array $values, ?bool $lazy = null): array {
foreach($values as $key => $value) {
Expand All @@ -1408,29 +1408,39 @@ private function formatAppValues(string $app, array $values, ?bool $lazy = null)
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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/private/AppFramework/Services/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function isLazy(string $key): bool {
* @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<string, string> [configKey => configValue]
* @return array<string, string|int|float|bool|array> [configKey => configValue]
* @since 29.0.0
*/
public function getAllAppValues(string $key = '', bool $filtered = false): array {
Expand Down
2 changes: 1 addition & 1 deletion lib/public/AppFramework/Services/IAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function isLazy(string $key): bool;
* @param string $key config keys prefix to search, can be empty.
* @param bool $filtered filter sensitive config values
*
* @return array<string, string> [configKey => configValue]
* @return array<string, string|int|float|bool|array> [configKey => configValue]
* @since 29.0.0
*/
public function getAllAppValues(string $key = '', bool $filtered = false): array;
Expand Down
5 changes: 3 additions & 2 deletions lib/public/IAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function isLazy(string $app, string $key): bool;
* @param string $prefix config keys prefix to search, can be empty.
* @param bool $filtered filter sensitive config values
*
* @return array<string, string> [configKey => configValue]
* @return array<string, string|int|float|bool|array> [configKey => configValue]
* @since 29.0.0
*/
public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array;
Expand All @@ -151,11 +151,12 @@ public function getAllValues(string $app, string $prefix = '', bool $filtered =
*
* @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<string, string|int|float|bool|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.
Expand Down

0 comments on commit 40d7716

Please sign in to comment.