config : read the correct file automatically

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2093 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-04-29 09:47:47 +00:00
parent 2fedd63db4
commit 22432f3e90

View File

@@ -20,15 +20,14 @@ class config
/** All the parameters allowed with their default value */
public $default = array ();
/** Use the .php to protect the informations */
public $confFile = "./datas/configuration.php";
public $confFile = null;
/** The constructor can update the confFile variable if it is defined in a
constant */
public function __construct ()
/** Select the configuration file */
public function selectConfFile ()
{
if (defined("CONFIGFILE"))
$this->confFile = CONFIGFILE;
if (file_exists ("./datas/configuration.php"))
elseif (file_exists ("./datas/configuration.php"))
$this->confFile = "./datas/configuration.php";
elseif (file_exists ("./data/configuration.php"))
$this->confFile = "./data/configuration.php";
@@ -37,6 +36,7 @@ class config
/** List all the parameters configurable in the software */
public function params ()
{
$this->selectConfFile ();
return $this->default;
}
@@ -45,6 +45,7 @@ class config
@param string $param The option name to be returned */
public function get ($param)
{
$this->selectConfFile ();
if (!array_key_exists ($param, $this->default))
throw new Exception ("Unknown parameter '$param'");
if (!file_exists ($this->confFile))
@@ -76,6 +77,7 @@ class config
@return TRUE if the parameter is saved or an exception */
public function set ($param, $value)
{
$this->selectConfFile ();
if (!array_key_exists ($param, $this->default))
throw new Exception ("Unknown parameter '$param'");
if (!file_exists ($this->confFile))
@@ -151,6 +153,150 @@ class config
return $phpcode;
}
private function tokenRead (&$tokens, $output = array ())
{
$lasttokens = array ();
$startDefault = false;
$parenthesis = 0;
$i = 0;
while (count ($tokens) > 0 && $i < 5000)
{
$i++;
$token = array_shift ($tokens);
if (is_array ($token))
{
list($id, $text) = $token;
if ($id === T_WHITESPACE) continue;
if ($id === T_VARIABLE && $text === '$this')
{
array_push ($lasttokens, "this");
continue;
}
if (end ($lasttokens) === "this" && $id === T_OBJECT_OPERATOR)
{
array_push ($lasttokens, "->");
continue;
}
if ($lasttokens === array ("this","->") && $id === T_STRING &&
$text === "default")
{
$startDefault = true;
$lasttokens = array ();
}
if ($startDefault === false)
continue;
// Here we are in the default array
echo "TEXT=$text<br>\n";
}
else
{
if ($startDefault === true)
{
if ($token === "(")
$parenthesis++;
if ($token === ")")
{
$parenthesis--;
if ($parenthesis === 0)
return $output;
}
}
echo "TOK =$token<br>\n";
}
}
if ($i === 5000)
throw new Exception ("Too much lines in file (>5000)", 500);
return $output;
}
/** Create the HTML form to configure the software where the definitions are
done. Allow to edit an existing configuration */
public function configHTML ($values=array ())
{
$debug = 1;
if (!defined('T_ML_COMMENT'))
define('T_ML_COMMENT', T_COMMENT);
else
define('T_DOC_COMMENT', T_ML_COMMENT);
$source = file_get_contents ("models/model_configuration.php");
$tokens = token_get_all ($source);
// In debug, display the tokens for analyzis
if ($debug)
{
echo "DEBUG<br/>\n";
foreach ($tokens as $token)
{
if (is_string ($token))
echo "STRING : $token<br>\n";
else
{
list($id, $text, $line) = $token;
if ($id === 379) continue;
echo token_name ($id)."(id $id) $text [line $line]<br>\n";
}
}
}
return $this->tokenRead ($tokens);
exit;
$params = array ();
$string = "";
$start = false;
foreach ($tokens as $token)
{
if (is_array ($token))
{
list($id, $text) = $token;
// Look at the $this->default, and skip all before this token
if ($id === T_STRING && $text === "default" && $start === false)
{
// Found the start of the analysis
$start = true;
continue;
}
if ($start === false)
continue;
$string = "";
switch ($id)
{
case T_COMMENT:
case T_ML_COMMENT: // we've defined this
case T_DOC_COMMENT: // and this
// Skip the lines which don't contains phpdoc standard
if (substr ($text, 0, 3) !== "/**")
continue;
$definition = trim (substr ($text, 4, -2));
echo $definition."<br>\n";
break;
case T_CONSTANT_ENCAPSED_STRING:
$params[$text] = $definition;
break;
}
}
elseif ($start === true && substr ($string.$token, -2) === ");")
{
// Skip all the lines after the end of the $this->default definition
break;
}
else
{
$string .= $token;
}
}
print_r ($params);
exit;
$fields = $this->configRead ($this->default);
exit;
print_r ($fields);
$f = new form ();
$f->fields ($fields);
echo $f->printHTML ();
}
/** Return an array of fields */
private function configRead ($params)
{