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:
51
cli.php
51
cli.php
@@ -84,13 +84,36 @@ class cli
|
|||||||
$file = reset (array_keys ($classes, $class));
|
$file = reset (array_keys ($classes, $class));
|
||||||
require_once ($file);
|
require_once ($file);
|
||||||
$refclass = new ReflectionClass ($class);
|
$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();
|
$methods = $refclass->getMethods();
|
||||||
echo "List of methods available in the class '$class' :\n";
|
echo "List of methods available in the class '$class' :\n";
|
||||||
foreach ($methods as $key=>$method)
|
foreach ($methods as $key=>$method)
|
||||||
{
|
{
|
||||||
|
if ($method->name === "__construct")
|
||||||
|
continue;
|
||||||
if ($key > 0)
|
if ($key > 0)
|
||||||
echo "\n";
|
echo "\n";
|
||||||
echo " ".$method->name;
|
echo " ".$method->name.$constParams;
|
||||||
$r = new ReflectionMethod ($class, $method->name);
|
$r = new ReflectionMethod ($class, $method->name);
|
||||||
$params = $r->getParameters();
|
$params = $r->getParameters();
|
||||||
foreach ($params as $param)
|
foreach ($params as $param)
|
||||||
@@ -145,8 +168,19 @@ class cli
|
|||||||
|
|
||||||
if ($found === FALSE)
|
if ($found === FALSE)
|
||||||
die ("Method '$method' not available in class '$class'\n");
|
die ("Method '$method' not available in class '$class'\n");
|
||||||
$r = new ReflectionMethod ($class, $method);
|
|
||||||
$min = 0; $max = 0;
|
$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();
|
$params = $r->getParameters();
|
||||||
foreach ($params as $param)
|
foreach ($params as $param)
|
||||||
{
|
{
|
||||||
@@ -154,6 +188,7 @@ class cli
|
|||||||
$min++;
|
$min++;
|
||||||
$max++;
|
$max++;
|
||||||
}
|
}
|
||||||
|
unset ($r);
|
||||||
|
|
||||||
if (count ($argv) < $min)
|
if (count ($argv) < $min)
|
||||||
die ("Not enough parameters provided to method\n");
|
die ("Not enough parameters provided to method\n");
|
||||||
@@ -174,10 +209,18 @@ class cli
|
|||||||
$argv[$key] = $val;
|
$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
|
try
|
||||||
{
|
{
|
||||||
$r = new $class;
|
$classReflection = new ReflectionClass($class);
|
||||||
|
$r = $classReflection->newInstanceArgs($paramConst);
|
||||||
$s = call_user_func_array(array($r, $method), $argv);
|
$s = call_user_func_array(array($r, $method), $argv);
|
||||||
var_dump ($s);
|
var_dump ($s);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user