App: fix setting default configuration values

This commit is contained in:
Benjamin Renard 2024-12-12 22:32:19 +01:00
parent 6f47b2aea7
commit 4b73564f31

View file

@ -203,9 +203,19 @@ class App {
*
* @param string $key The configuration variable key
* @param mixed $value The configuration variable default value
* @param boolean $override Set to true to override default value if already set (optional,
* default: false)
* @return boolean
**/
public static function set_default($key, $value) {
public static function set_default($key, $value, $override=false) {
if (is_array($value)) {
$result = true;
foreach($value as $subkey => $subvalue)
$result = $result && self :: set_default("$key.$subkey", $subvalue, $override);
return $result;
}
if (self :: isset("default.$key") && !$override)
return true;
return Config::set("default.$key", $value, self :: $options);
}
@ -214,12 +224,14 @@ class App {
*
* @param array<string,mixed> $values Associative array of configuration variables name and
* default values
* @param boolean $override Set to true to override default value if already set (optional,
* default: false)
* @return boolean
**/
public static function set_defaults($values) {
public static function set_defaults($values, $override=false) {
$error = false;
foreach($values as $key => $value)
if (!self :: set_default($key, $value))
if (!self :: set_default($key, $value, $override))
$error = true;
return !$error;
}