config: Manage the configuration in update too
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3957 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
178
config.php
178
config.php
@@ -3,8 +3,6 @@
|
|||||||
@package domframework
|
@package domframework
|
||||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||||
|
|
||||||
require_once ("domframework/form.php");
|
|
||||||
|
|
||||||
/** Manage the configurations of the module done by administrator in a config
|
/** Manage the configurations of the module done by administrator in a config
|
||||||
* file
|
* file
|
||||||
* It is based on the module configuration defaults
|
* It is based on the module configuration defaults
|
||||||
@@ -54,7 +52,7 @@ class config
|
|||||||
return $this->default;
|
return $this->default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the defined values
|
/** Return the defined values in array format
|
||||||
*/
|
*/
|
||||||
public function getAll ()
|
public function getAll ()
|
||||||
{
|
{
|
||||||
@@ -66,6 +64,34 @@ class config
|
|||||||
return $conf;
|
return $conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the defined values in Slash format
|
||||||
|
*/
|
||||||
|
public function getAllInSlash ()
|
||||||
|
{
|
||||||
|
$conf = $this->getAll ();
|
||||||
|
$vals = array ();
|
||||||
|
foreach ($this->docComment () as $key)
|
||||||
|
{
|
||||||
|
if (! key_exists ("param", $key))
|
||||||
|
continue;
|
||||||
|
if (substr ($key["param"], 0, 1) !== "/")
|
||||||
|
throw new \Exception ("Config param '".$key["param"].
|
||||||
|
"' don't start by /");
|
||||||
|
$exp = explode ("/", $key["param"]);
|
||||||
|
unset ($exp[0]);
|
||||||
|
if (count ($exp) === 1 && key_exists ($exp[1], $conf))
|
||||||
|
$vals[$key["param"]] = $conf[$exp[1]];
|
||||||
|
elseif (count ($exp) === 2 && key_exists ($exp[1], $conf) &&
|
||||||
|
key_exists ($exp[2], $conf[$exp[1]]))
|
||||||
|
$vals[$key["param"]] = $conf[$exp[1]][$exp[2]];
|
||||||
|
elseif (count ($exp) === 3 && key_exists ($exp[1], $conf) &&
|
||||||
|
key_exists ($exp[2], $conf[$exp[1]]) &&
|
||||||
|
key_exists ($exp[3], $conf[$exp[1]][$exp[2]]))
|
||||||
|
$vals[$key["param"]] = $conf[$exp[1]][$exp[2]][$exp[3]];
|
||||||
|
}
|
||||||
|
return $vals;
|
||||||
|
}
|
||||||
|
|
||||||
/** Get the value of the provided parameter recorded in .php file
|
/** Get the value of the provided parameter recorded in .php file
|
||||||
* If it is not set in .php file, use the default value
|
* If it is not set in .php file, use the default value
|
||||||
* @param string $param The option name to be returned
|
* @param string $param The option name to be returned
|
||||||
@@ -136,6 +162,7 @@ class config
|
|||||||
*/
|
*/
|
||||||
public function set ($param, $value)
|
public function set ($param, $value)
|
||||||
{
|
{
|
||||||
|
die ("Config : set DO NOT USE");
|
||||||
$this->selectConfFile ();
|
$this->selectConfFile ();
|
||||||
if (!array_key_exists ($param, $this->default))
|
if (!array_key_exists ($param, $this->default))
|
||||||
throw new Exception ("Unknown parameter '$param'", 500);
|
throw new Exception ("Unknown parameter '$param'", 500);
|
||||||
@@ -182,6 +209,7 @@ class config
|
|||||||
*/
|
*/
|
||||||
private function writePHP ($values, $phpcode, $indent)
|
private function writePHP ($values, $phpcode, $indent)
|
||||||
{
|
{
|
||||||
|
die ("Config : writePHP DO NOT USE");
|
||||||
foreach ($values as $key=>$val)
|
foreach ($values as $key=>$val)
|
||||||
{
|
{
|
||||||
$phpcode .= str_pad (" ", $indent);
|
$phpcode .= str_pad (" ", $indent);
|
||||||
@@ -264,27 +292,148 @@ class config
|
|||||||
elseif ($values === "null")
|
elseif ($values === "null")
|
||||||
$new = null;
|
$new = null;
|
||||||
elseif ($values === "false")
|
elseif ($values === "false")
|
||||||
$new = false;
|
$new = "false";
|
||||||
elseif ($values === "true")
|
elseif ($values === "true")
|
||||||
$new = true;
|
$new = "true";
|
||||||
else
|
else
|
||||||
$new = intval ($values);
|
$new = intval ($values);
|
||||||
}
|
}
|
||||||
return $new;
|
return $new;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return an array containing the definitions read from the default config
|
/** Update the configuration file with the provided parameters
|
||||||
* file
|
* @param array $paramsSlash The parameters to analyze for updating the file
|
||||||
* @param string $modelFile The file model to read
|
* The params are in an array like array ("/authentication/html" => "value")
|
||||||
|
* All the parameters are not needed
|
||||||
|
* To remove a parameter, set it to default value
|
||||||
*/
|
*/
|
||||||
public function docComment ($modelFile)
|
public function updateParamsSlash ($paramsSlash)
|
||||||
{
|
{
|
||||||
$debug = 0;
|
$debug = 0;
|
||||||
|
if ($debug) echo "<pre>";
|
||||||
|
if ($debug) echo "########## BEFORE\n";
|
||||||
|
if ($debug) echo "PARAMSLASH=";
|
||||||
|
if ($debug) var_dump ($paramsSlash);
|
||||||
|
// Check if the provided slash parameters are defined
|
||||||
|
$definitions = $this->docComment ();
|
||||||
|
// Actually stored values
|
||||||
|
$stored = $this->getAllInSlash ();
|
||||||
|
if ($debug) echo "stored =";
|
||||||
|
if ($debug) var_dump ($stored);
|
||||||
|
$new = array ();
|
||||||
|
// Update the stored values with the provided ones
|
||||||
|
foreach ($definitions as $key=>$unused)
|
||||||
|
{
|
||||||
|
if ($debug) echo "CHECK $key\n";
|
||||||
|
if (! key_exists ($key, $paramsSlash) && key_exists ($key, $stored))
|
||||||
|
{
|
||||||
|
if ($debug) echo "COPY the OLD value fo '$key' to the new array\n";
|
||||||
|
$new[$key] = $this->cast ($stored[$key]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (! key_exists ($key, $paramsSlash))
|
||||||
|
{
|
||||||
|
if ($debug) echo "Param $key not provided : skipped\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$val = $paramsSlash[$key];
|
||||||
|
if ($val === "")
|
||||||
|
{
|
||||||
|
if ($debug) echo "MUST REMOVE $key => Empty val\n";
|
||||||
|
}
|
||||||
|
// Do NOT use the === as the $val is always a string and must be compared
|
||||||
|
// with default integers
|
||||||
|
elseif (key_exists ("default", $definitions[$key]) &&
|
||||||
|
$this->cast ($val) ===
|
||||||
|
$this->cast ($definitions[$key]["default"]))
|
||||||
|
{
|
||||||
|
if ($debug) echo "MUST REMOVE $key => default\n";
|
||||||
|
}
|
||||||
|
elseif (key_exists ($key, $paramsSlash) && key_exists ($key, $stored) &&
|
||||||
|
$paramsSlash[$key] === $stored[$key])
|
||||||
|
{
|
||||||
|
if ($debug) echo "MUST COPY $key\n";
|
||||||
|
$new[$key] = $this->cast ($val);
|
||||||
|
}
|
||||||
|
elseif (key_exists ($key, $stored) && $stored[$key] !== $val)
|
||||||
|
{
|
||||||
|
if ($debug) echo "MUST OVERWRITE $key WITH NEW VALUE\n";
|
||||||
|
$new[$key] = $this->cast ($val);
|
||||||
|
}
|
||||||
|
elseif (! key_exists ($key, $stored))
|
||||||
|
{
|
||||||
|
if ($debug) echo "MUST ADD $key without DEFAULT\n";
|
||||||
|
$new[$key] = $this->cast ($val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ($debug) echo "NOT CATCHED !!!\n";
|
||||||
|
if ($debug) echo " STORED=";var_dump ($stored);
|
||||||
|
if ($debug) echo " DEFS=";var_dump ($definitions[$key]);
|
||||||
|
if ($debug) echo " SLASH=";var_dump ($val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the new stored values in the file
|
||||||
|
$txt = "<?php\n";
|
||||||
|
$txt .= "\$conf = array ();\n";
|
||||||
|
foreach ($new as $key=>$val)
|
||||||
|
{
|
||||||
|
$exp = explode ("/", substr ($key, 1));
|
||||||
|
$txt .= "\$conf['".implode ("']['", $exp)."'] = ".
|
||||||
|
var_export ($val, true).";\n";
|
||||||
|
}
|
||||||
|
if (file_put_contents ($this->confFile, $txt, LOCK_EX) === FALSE)
|
||||||
|
throw new \Exception (sprintf (dgettext("domframework",
|
||||||
|
"Can't save configuration file '%s'"),
|
||||||
|
$this->confFile), 500);
|
||||||
|
// The next command clear the include cache file and force the PHP to
|
||||||
|
// read it again on next request.
|
||||||
|
// If it is not used, include () return the old value
|
||||||
|
opcache_invalidate ($this->confFile);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cast the provided string into the associated value
|
||||||
|
* @param string $val The val to cast
|
||||||
|
*/
|
||||||
|
private function cast ($val)
|
||||||
|
{
|
||||||
|
if (isset ($val[0]) && ($val[0] === "\"" || $val[0] === "'" ))
|
||||||
|
$new = substr ($val, 1, -1);
|
||||||
|
elseif ($val === "null")
|
||||||
|
$new = null;
|
||||||
|
elseif ($val === "false")
|
||||||
|
$new = false;
|
||||||
|
elseif ($val === "true")
|
||||||
|
$new = true;
|
||||||
|
elseif ($val === "")
|
||||||
|
$new = "";
|
||||||
|
elseif (is_array ($val))
|
||||||
|
$new = $val;
|
||||||
|
elseif (strspn ($val, "0123456789") === strlen ($val))
|
||||||
|
$new = intval ($val);
|
||||||
|
elseif (strspn ($val, "0123456789.") === strlen ($val))
|
||||||
|
$new = floatval ($val);
|
||||||
|
else
|
||||||
|
$new = $val;
|
||||||
|
return $new;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return an array containing the definitions read from the default config
|
||||||
|
* file
|
||||||
|
* The definition of param are in Slash format
|
||||||
|
*/
|
||||||
|
public function docComment ()
|
||||||
|
{
|
||||||
|
$debug = 0;
|
||||||
|
$reflector = new ReflectionClass (get_class ($this));
|
||||||
|
$modelFile = $reflector->getFileName();
|
||||||
if (! file_exists ($modelFile))
|
if (! file_exists ($modelFile))
|
||||||
throw new Exception (dgettext("domframework",
|
throw new \Exception (dgettext("domframework",
|
||||||
"The configuration model file is missing"), 500);
|
"The configuration model file is missing"), 500);
|
||||||
if (! is_readable ($modelFile))
|
if (! is_readable ($modelFile))
|
||||||
throw new Exception (dgettext("domframework",
|
throw new \Exception (dgettext("domframework",
|
||||||
"The configuration model file is not readable"),
|
"The configuration model file is not readable"),
|
||||||
500);
|
500);
|
||||||
$filecontent = file_get_contents ($modelFile);
|
$filecontent = file_get_contents ($modelFile);
|
||||||
@@ -326,6 +475,9 @@ class config
|
|||||||
$group = $data["group"];
|
$group = $data["group"];
|
||||||
if (! isset ($data["param"]))
|
if (! isset ($data["param"]))
|
||||||
continue;
|
continue;
|
||||||
|
if (substr ($data["param"], 0, 1) !== "/")
|
||||||
|
throw new Exception (sprintf (dgettext("domframework",
|
||||||
|
"Parameter '%s' doesn't start by slash"), $data["param"]), 500);
|
||||||
$data["depth"] = $parenthesis;
|
$data["depth"] = $parenthesis;
|
||||||
$data["group"] = $group;
|
$data["group"] = $group;
|
||||||
//if (isset ($data["values"]) && $data["values"][0] !== "\"")
|
//if (isset ($data["values"]) && $data["values"][0] !== "\"")
|
||||||
@@ -339,10 +491,8 @@ class config
|
|||||||
}
|
}
|
||||||
if (isset ($data["default"]))
|
if (isset ($data["default"]))
|
||||||
$data["default"] = $this->strToType ($data["default"]);
|
$data["default"] = $this->strToType ($data["default"]);
|
||||||
if (isset ($data["prefix"]))
|
|
||||||
$data["prefix"] = $this->strToType ($data["prefix"]);
|
|
||||||
if ($debug) var_dump ($data);
|
if ($debug) var_dump ($data);
|
||||||
$params[] = $data;
|
$params[$data["param"]] = $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user