From c3ad1085c18634d00fefeac7d8250d0135d6be55 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 25 Jul 2016 11:28:53 +0000 Subject: [PATCH] config : allow the defaults vals to be returned in array of arrays git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2945 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- config.php | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/config.php b/config.php index b611108..52036c4 100644 --- a/config.php +++ b/config.php @@ -88,37 +88,34 @@ class config $rc = include ($this->confFile); if ($rc !== 1) throw new Exception ("Error in configuration file", 500); - if (array_key_exists ($param, $conf)) + if (! array_key_exists ($param, $this->default)) + throw new Exception (sprintf ("Configuration parameter '%s' not defined", + $param), 500); + // Create a conf where all the keys are defined. If the keys are already + // define, use them, or use the default ones + // Don't allow keys not defined in default ones + foreach ($this->default[$param] as $key=>$val) { - if (! is_array ($conf[$param])) - return $conf[$param]; - // if the configuration is an array, check if all the keys are defined - // or use the default - foreach ($this->default[$param] as $key=>$val) + if ($key === "not configured") { - if (! isset ($conf[$param][$key])) - $conf[$param][$key] = $val; - } - foreach ($conf[$param] as $key=>$val) - { - if ($key === "not configured") + if (! array_key_exists ($param, $conf)) continue; - if (is_array ($val)) + foreach ($conf[$param] as $k=>$v) { - if (! isset ($this->default[$param]["not configured"])) - continue; - foreach ($this->default[$param]["not configured"] as $k=>$v) - { - if (! array_key_exists ($k, $conf[$param][$key])) - $conf[$param][$key][$k] = $v; - } + $conf[$param][$k] = array_replace_recursive ($val, + $conf[$param][$k]); } + continue; } - if (count ($conf[$param]) > 1 && isset ($conf[$param]["not configured"])) - unset ($conf[$param]["not configured"]); - return $conf[$param]; + if (! isset ($conf[$param][$key])) + $conf[$param][$key] = $val; + elseif (is_array ($val)) + $conf[$param][$key] = array_replace_recursive ($val, + $conf[$param][$key]); } - return $this->default[$param]; + if (! array_key_exists ($param, $conf)) + return $this->default[$param]; + return $conf[$param]; } /** Define a value for the parameter in the config file. Add all the default