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:
253
config.php
253
config.php
@@ -8,6 +8,14 @@ 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
|
||||||
|
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 :
|
POC :
|
||||||
$config = new config();
|
$config = new config();
|
||||||
$config->default = array ("param"=>"default",
|
$config->default = array ("param"=>"default",
|
||||||
@@ -166,168 +174,135 @@ class config
|
|||||||
return $phpcode;
|
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 ();
|
$values = str_replace ("array (", "array(", $values);
|
||||||
$startDefault = false;
|
if (stripos ($values, "array(") !== false)
|
||||||
$parenthesis = 0;
|
|
||||||
$i = 0;
|
|
||||||
while (count ($tokens) > 0 && $i < 5000)
|
|
||||||
{
|
{
|
||||||
$i++;
|
$values = substr ($values, 6, -1);
|
||||||
$token = array_shift ($tokens);
|
$values = explode (",", $values);
|
||||||
if (is_array ($token))
|
$new = array ();
|
||||||
|
foreach ($values as $key=>$val)
|
||||||
{
|
{
|
||||||
list($id, $text) = $token;
|
if (strpos ($val, "=>") !== false)
|
||||||
if ($id === T_WHITESPACE) continue;
|
|
||||||
if ($id === T_VARIABLE && $text === '$this')
|
|
||||||
{
|
{
|
||||||
array_push ($lasttokens, "this");
|
// Associated array
|
||||||
continue;
|
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
|
else
|
||||||
{
|
{
|
||||||
list($id, $text, $line) = $token;
|
// Unique value (string or integer or float)
|
||||||
if ($id === 379) continue;
|
if ($val[0] === "\"" || $val[0] === "'")
|
||||||
echo token_name ($id)."(id $id) $text [line $line]<br>\n";
|
$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 an array containing the definitions read from the default config
|
||||||
return $this->tokenRead ($tokens);
|
file */
|
||||||
|
public function docComment ($modelFile)
|
||||||
exit;
|
{
|
||||||
|
$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 ();
|
$params = array ();
|
||||||
$string = "";
|
$group = dgettext("domframework", "Default parameters");
|
||||||
$start = false;
|
|
||||||
foreach ($tokens as $token)
|
foreach ($tokens as $token)
|
||||||
{
|
{
|
||||||
if (is_array ($token))
|
if (is_array ($token))
|
||||||
{
|
{
|
||||||
list($id, $text) = $token;
|
list ($id, $text) = $token;
|
||||||
// Look at the $this->default, and skip all before this token
|
if ($debug) echo "ARRAY : $id, $text\n";
|
||||||
if ($id === T_STRING && $text === "default" && $start === false)
|
if ($foundDefault === "" && $text === "->")
|
||||||
|
$foundDefault = $text;
|
||||||
|
if ($id === T_DOC_COMMENT)
|
||||||
{
|
{
|
||||||
// Found the start of the analysis
|
// Look at @param, @description,@type, @values, @default
|
||||||
$start = true;
|
if ($debug) echo "DOC_COMMENT : $text\n";
|
||||||
continue;
|
// Append the not completed lines
|
||||||
}
|
$text = trim (substr ($text, 3, -2));
|
||||||
if ($start === false)
|
$text = preg_replace ("/\n\s+/", " ", $text);
|
||||||
continue;
|
$text = preg_replace (
|
||||||
$string = "";
|
"/(@(param|description|type|values|default|group))/",
|
||||||
switch ($id)
|
"\n\${1}", $text);
|
||||||
{
|
$text = ltrim ($text);
|
||||||
case T_COMMENT:
|
// Look at each parameter and save them in a data array
|
||||||
case T_ML_COMMENT: // we've defined this
|
$data = array ();
|
||||||
case T_DOC_COMMENT: // and this
|
foreach (explode ("\n", $text) as $line)
|
||||||
// Skip the lines which don't contains phpdoc standard
|
{
|
||||||
if (substr ($text, 0, 3) !== "/**")
|
$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;
|
continue;
|
||||||
$definition = trim (substr ($text, 4, -2));
|
$data["depth"] = $parenthesis;
|
||||||
echo $definition."<br>\n";
|
$data["group"] = $group;
|
||||||
break;
|
//if (isset ($data["values"]) && $data["values"][0] !== "\"")
|
||||||
case T_CONSTANT_ENCAPSED_STRING:
|
if (isset ($data["values"]))
|
||||||
$params[$text] = $definition;
|
{
|
||||||
break;
|
// 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
|
else
|
||||||
{
|
{
|
||||||
$string .= $token;
|
if ($debug) echo "TEXT : $token\n";
|
||||||
|
if ($foundDefault !== "" && $token === "(")
|
||||||
|
$parenthesis++;
|
||||||
|
if ($foundDefault !== "" && $token === ")")
|
||||||
|
{
|
||||||
|
$parenthesis--;
|
||||||
|
if ($parenthesis === 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print_r ($params);
|
return $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user