Add support to parameter of constructor in CLI. Actually, the parameter must not be optionnal

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1279 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-04-21 15:57:20 +00:00
parent a0698e2f8e
commit 3f72772aec

51
cli.php
View File

@@ -84,13 +84,36 @@ class cli
$file = reset (array_keys ($classes, $class));
require_once ($file);
$refclass = new ReflectionClass ($class);
// Look at constructor parameters
// PARAMETERS MUST NOT BE OPTIONNAL
$constParams = "";
try
{
$r = new ReflectionMethod ($class, "__construct");
$params = $r->getParameters();
foreach ($params as $param)
{
$constParams .= " ";
$constParams .= " <";
$constParams .= $param->name;
$constParams .= ">";
}
}
catch (Exception $e)
{
// No constructor
}
// Look at methods
$methods = $refclass->getMethods();
echo "List of methods available in the class '$class' :\n";
foreach ($methods as $key=>$method)
{
if ($method->name === "__construct")
continue;
if ($key > 0)
echo "\n";
echo " ".$method->name;
echo " ".$method->name.$constParams;
$r = new ReflectionMethod ($class, $method->name);
$params = $r->getParameters();
foreach ($params as $param)
@@ -145,8 +168,19 @@ class cli
if ($found === FALSE)
die ("Method '$method' not available in class '$class'\n");
$r = new ReflectionMethod ($class, $method);
$min = 0; $max = 0;
try
{
$r = new ReflectionMethod ($class, "__construct");
$paramsConst = $r->getParameters();
$min = $max = count ($paramsConst);
}
catch (Exception $e)
{
// No constructor available in class
}
$r = new ReflectionMethod ($class, $method);
$params = $r->getParameters();
foreach ($params as $param)
{
@@ -154,6 +188,7 @@ class cli
$min++;
$max++;
}
unset ($r);
if (count ($argv) < $min)
die ("Not enough parameters provided to method\n");
@@ -174,10 +209,18 @@ class cli
$argv[$key] = $val;
}
// TODO : Manage a parameter in the constructor of the class
// Manage a parameter in the constructor of the class
$paramConst = array ();
if (count ($paramsConst) > 0)
{
$paramConst = array_slice ($argv, 0, count ($paramsConst));
$argv = array_slice ($argv, count ($paramsConst));
}
try
{
$r = new $class;
$classReflection = new ReflectionClass($class);
$r = $classReflection->newInstanceArgs($paramConst);
$s = call_user_func_array(array($r, $method), $argv);
var_dump ($s);
}