From 4b73564f31536153e6374f871ec9be4eee825225 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 12 Dec 2024 22:32:19 +0100 Subject: [PATCH] App: fix setting default configuration values --- src/App.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/App.php b/src/App.php index 3fe0fa8..fd23105 100644 --- a/src/App.php +++ b/src/App.php @@ -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 $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; }