First version of CLI support

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1212 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-02-28 09:22:18 +00:00
parent c89b621bc5
commit 8b96e19467

178
cli.php Normal file
View File

@@ -0,0 +1,178 @@
<?php
class cli
{
/** Run in CLI mode with parameters */
/* Example of cli code :
#!/usr/bin/php5
<?php
require ("domframework/cli.php");
$cli = new cli;
$cli->run(); */
public function run ()
{
global $argv;
$launcher = $argv[0];
chdir (dirname ($argv[0])."/..");
array_shift ($argv);
if (isset ($argv[0]) && $argv[0] === "-h")
{
echo "Execute in CLI a controller or a model (in expert mode)\n";
echo " $launcher -h : display this help\n";
echo " $launcher -list : display controllers\n";
echo " $launcher -expert -list : display controllers and models\n";
echo " $launcher -listmethods <class> : \n";
echo " display the methods available the controller class\n";
echo " $launcher -expert -listmethods <class> :\n";
echo " display the methods available the model or controller class\n";
echo " $launcher -listmethodsdetails <class> : \n";
echo " display the methods available the controller class\n";
echo " $launcher -expert -listmethodsdetails <class> :\n";
echo " display the methods available the model or controller class\n";
echo " $launcher <class> <method> [args]\n";
echo " execute the method with the provided args\n";
echo " $launcher -expert <class> <method> [args]\n";
echo " execute the method with the provided args\n";
echo "You can replace ONE arg by a dash (-) to read from stdin\n";
exit;
}
$EXPERT = FALSE;
if (isset ($argv[0]) && $argv[0] === "-expert")
{
$EXPERT = TRUE;
array_shift ($argv);
}
// List the controllers and the models if the expert mode is activated
$files = glob ("controllers/*.php");
if ($EXPERT)
$files = array_merge ($files, glob ("models/*.php"));
if (count ($files) === 0 && $EXPERT === FALSE)
die ("No controllers available in ".getcwd()."\n");
if (count ($files) === 0 && $EXPERT === TRUE)
die ("No controllers/models available in ".getcwd()."\n");
foreach ($files as $file)
$classes[$file] = substr (strstr ($file, "_"), 1, -4);
if (isset ($argv[0]) && $argv[0] === "-list")
{
// Lists the classes available in controllers and models if expert mode
echo "List of classes available :\n";
echo " ".implode ("\n ", $classes)."\n";
echo "Usage : ".$launcher;
if ($EXPERT) echo " -expert";
echo " -listmethods <class>\n";
exit;
}
if (isset ($argv[0]) && $argv[0] === "-listmethods" ||
isset ($argv[0]) && $argv[0] === "-listmethodsdetails")
{
// Lists the methods of a class
if (!isset ($argv[1]))
die ("Missing parameter 'class'\n");
$class = $argv[1];
if (!in_array ($class, $classes))
die ("Unknown class '$class'\n");
$file = reset (array_keys ($classes, $class));
require_once ($file);
$refclass = new ReflectionClass ($class);
$methods = $refclass->getMethods();
echo "List of methods available in the class '$class' :\n";
foreach ($methods as $method)
{
echo " ".$method->name;
$r = new ReflectionMethod ($class, $method->name);
$params = $r->getParameters();
foreach ($params as $param)
{
echo " ";
if ($param->isOptional())
echo "[";
else
echo " <";
echo $param->name;
if ($param->isOptional())
echo "]";
else
echo ">";
}
if ($argv[0] === "-listmethodsdetails")
{
echo "\n";
echo trim (substr ($method->getDocComment(), 3, -2));
}
echo "\n";
}
exit;
}
// Execution of the class->method
if (!isset ($argv[0]))
die ("No class to execute provided\n");
$class = $argv[0];
if (!in_array ($class, $classes))
die ("Unknown class '$class'\n");
array_shift ($argv);
$file = reset (array_keys ($classes, $class));
require_once ($file);
if (!isset ($argv[0]))
die ("No method for class '$class' to execute\n");
$method = $argv[0];
array_shift ($argv);
$refclass = new ReflectionClass ($class);
$methods = $refclass->getMethods();
$found = FALSE;
foreach ($methods as $meth)
{
if ($meth->name === $method)
{
$found = TRUE;
break;
}
}
if ($found === FALSE)
die ("Method '$method' not available in class '$class'\n");
$r = new ReflectionMethod ($class, $method);
$min = 0; $max = 0;
$params = $r->getParameters();
foreach ($params as $param)
{
if (!$param->isOptional())
$min++;
$max++;
}
if (count ($argv) < $min)
die ("Not enough parameters provided to method\n");
if (in_array ("-", $argv))
{
$key = reset (array_keys ($argv, "-"));
$argv[$key] = file_get_contents ("php://stdin");
}
// TODO : Manage a parameter in the constructor of the class
$r = new $class;
switch (count ($argv))
{
case 0: $s=$r->$method(); break;
case 1: $s=$r->$method($argv[0]); break;
case 2: $s=$r->$method($argv[0], $argv[1]); break;
case 3: $s=$r->$method($argv[0], $argv[1], $argv[2]); break;
case 4: $s=$r->$method($argv[0], $argv[1], $argv[2], $argv[3]); break;
case 5: $s=$r->$method($argv[0], $argv[1], $argv[2], $argv[3], $argv[4]);
break;
default:
die ("Too much parameter on the method to be used in CLI\n");
}
var_dump ($s);
exit;
}
}