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
This commit is contained in:
2016-07-25 11:28:53 +00:00
parent 71975da3cb
commit c3ad1085c1

View File

@@ -88,37 +88,34 @@ class config
$rc = include ($this->confFile); $rc = include ($this->confFile);
if ($rc !== 1) if ($rc !== 1)
throw new Exception ("Error in configuration file", 500); 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])) if ($key === "not configured")
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 (! isset ($conf[$param][$key])) if (! array_key_exists ($param, $conf))
$conf[$param][$key] = $val;
}
foreach ($conf[$param] as $key=>$val)
{
if ($key === "not configured")
continue; continue;
if (is_array ($val)) foreach ($conf[$param] as $k=>$v)
{ {
if (! isset ($this->default[$param]["not configured"])) $conf[$param][$k] = array_replace_recursive ($val,
continue; $conf[$param][$k]);
foreach ($this->default[$param]["not configured"] as $k=>$v)
{
if (! array_key_exists ($k, $conf[$param][$key]))
$conf[$param][$key][$k] = $v;
}
} }
continue;
} }
if (count ($conf[$param]) > 1 && isset ($conf[$param]["not configured"])) if (! isset ($conf[$param][$key]))
unset ($conf[$param]["not configured"]); $conf[$param][$key] = $val;
return $conf[$param]; 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 /** Define a value for the parameter in the config file. Add all the default