config : add the function docComment to read the parameters from the default config file. It permits to create easily the interface to configure the soft

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2148 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-05-06 13:30:26 +00:00
parent a266381f34
commit 6b21c41ff9

View File

@@ -8,6 +8,14 @@ require_once ("domframework/form.php");
/** Manage the configurations of the module done by administrator in a config
file
It is based on the module configuration defaults
The DocType allow to define the configuration HTML page. It must contains
@param : the name of the parameter
@description : the textual description of the parameter
@type : the type of the parameter (string, integer, array)
@values : an array containing the allowed values to the parameter. Can be
a function like timezone_identifiers_list
@default : the default value (can be an array or a string or a number)
@group : Group all the parameters in a group
POC :
$config = new config();
$config->default = array ("param"=>"default",
@@ -166,168 +174,135 @@ class config
return $phpcode;
}
private function tokenRead (&$tokens, $output = array ())
/** Convert a string to the right PHP type, without using the eval function */
private function strToType ($values)
{
$lasttokens = array ();
$startDefault = false;
$parenthesis = 0;
$i = 0;
while (count ($tokens) > 0 && $i < 5000)
$values = str_replace ("array (", "array(", $values);
if (stripos ($values, "array(") !== false)
{
$i++;
$token = array_shift ($tokens);
if (is_array ($token))
$values = substr ($values, 6, -1);
$values = explode (",", $values);
$new = array ();
foreach ($values as $key=>$val)
{
list($id, $text) = $token;
if ($id === T_WHITESPACE) continue;
if ($id === T_VARIABLE && $text === '$this')
if (strpos ($val, "=>") !== false)
{
array_push ($lasttokens, "this");
continue;
// Associated array
unset ($values[$key]);
list ($key1, $val1) = explode ("=>", $val);
if ($val1[0] === "\"" || $val1[0] === "'")
$val1 = substr($val1, 1, -1);
elseif (strpos ($val1, "."))
$val1 = floatval ($val1);
else
$val1 = intval ($val1);
$new[$key1] = $val1;
}
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";
// Unique value (string or integer or float)
if ($val[0] === "\"" || $val[0] === "'")
$val = substr($val, 1, -1);
elseif (strpos ($val, "."))
$val = floatval ($val);
else
$val = intval ($val);
$new[$key] = $val;
}
}
}
else
{
if ($values[0] === "\"" || $values[0] === "'" )
$new = substr ($values, 1, -1);
elseif (strpos ($values, "."))
$new = floatval ($values);
else
$new = intval ($values);
}
return $new;
}
return $this->tokenRead ($tokens);
exit;
/** Return an array containing the definitions read from the default config
file */
public function docComment ($modelFile)
{
$debug = 0;
if (! file_exists ($modelFile))
throw new Exception (dgettext("domframework",
"The configuration model file is missing"), 500);
if (! is_readable ($modelFile))
throw new Exception (dgettext("domframework",
"The configuration model file is not readable"),
500);
$filecontent = file_get_contents ($modelFile);
$tokens = token_get_all ($filecontent);
$foundDefault = "";
$parenthesis = 0;
$params = array ();
$string = "";
$start = false;
$group = dgettext("domframework", "Default parameters");
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)
list ($id, $text) = $token;
if ($debug) echo "ARRAY : $id, $text\n";
if ($foundDefault === "" && $text === "->")
$foundDefault = $text;
if ($id === T_DOC_COMMENT)
{
// 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) !== "/**")
// Look at @param, @description,@type, @values, @default
if ($debug) echo "DOC_COMMENT : $text\n";
// Append the not completed lines
$text = trim (substr ($text, 3, -2));
$text = preg_replace ("/\n\s+/", " ", $text);
$text = preg_replace (
"/(@(param|description|type|values|default|group))/",
"\n\${1}", $text);
$text = ltrim ($text);
// Look at each parameter and save them in a data array
$data = array ();
foreach (explode ("\n", $text) as $line)
{
$tmp = explode (" ", $line);
$key = reset ($tmp);
$key = substr ($key, 1);
$data[$key] = trim (implode (" ",array_slice ($tmp, 1)));
}
if (isset ($data["group"]))
$group = $data["group"];
if (! isset ($data["param"]))
continue;
$definition = trim (substr ($text, 4, -2));
echo $definition."<br>\n";
break;
case T_CONSTANT_ENCAPSED_STRING:
$params[$text] = $definition;
break;
$data["depth"] = $parenthesis;
$data["group"] = $group;
//if (isset ($data["values"]) && $data["values"][0] !== "\"")
if (isset ($data["values"]))
{
// A function or an array or a number is provided
if ( function_exists ($data["values"]))
$data["values"] = call_user_func ($data["values"]);
elseif (is_string ($data["values"]))
$data["values"] = $this->strToType ($data["values"]);
}
if (isset ($data["default"]))
$data["default"] = $this->strToType ($data["default"]);
if ($debug) var_dump ($data);
$params[] = $data;
}
}
elseif ($start === true && substr ($string.$token, -2) === ");")
{
// Skip all the lines after the end of the $this->default definition
break;
}
else
{
$string .= $token;
if ($debug) echo "TEXT : $token\n";
if ($foundDefault !== "" && $token === "(")
$parenthesis++;
if ($foundDefault !== "" && $token === ")")
{
$parenthesis--;
if ($parenthesis === 0)
break;
}
}
}
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)
{
$fields = array ();
foreach ($params as $p1=>$vals1)
{
if (is_array ($vals1))
{
$fields = array_merge ($fields, $this->configRead ($vals1));
}
else
{
$field = new formfield ($vals1, $vals1);
$field->group = $p1;
$fields[] = $field;
unset ($field);
}
}
return $fields;
return $params;
}
}