apolinux / commandrun
该软件包最新版本(v0.51)没有提供许可证信息。
通过参数运行作为参数的方法类
v0.51
2020-03-23 16:07 UTC
README
运行从类中调用的命令行程序,使用参数作为方法参数。
描述
运行一个独特的命令,可以在定义的目录内调用多个类中的方法。
它显示了关于程序本身和可能调用的类的完整帮助信息。
有时,使用可配置的参数调用命令并将其转换为方法对象是很方便的。这个类允许跳过手动处理命令行参数,并获取有关配置为被调用的每个类的信息。
示例
提供了一个名为 cmdrun.php 的文件,以展示程序在执行时如何被调用
use \Apolinux\ClassManager\DirectoryClassType;
use \Apolinux\ClassManager\ClassContainer;
require __DIR__ .'/vendor/autoload.php' ;
$container = new ClassContainer ;
$container->add(new DirectoryClassType(__DIR__. '/test/unit/Commands'));
$cmd = new \Apolinux\CommandRun($container);
$cmd->start();
当执行时
./cmdrun.php
它显示
command run
Usage: cmdrun.php [ -h | --help ] [ -l | --list ] ClassName [ -h | -help ] [ method ] [ --param1=value1 ] [ --param2=value2 ] ...
where:
-h | --help : shows this help.
-l | --list : shows a list of possible classname commands.
-d | --classdir : specify classes directory where to find class commands.
ClassName : name of class that contains "run" method with command instructions.
-h | -help : after classname. show class methods available with their respective parameters.
method : alternative method to be called.
--paramX=valueX : parameter names of run (or "method") method in ClassName class.
使用测试目录
./cmdrun.php -l
classes list:
* TestProcess
* TestRunClass
To view method details of class run as cmdrun.php ... ClassName -h
./cmdrun.php TestRunClass
The parameter 'param1' is not defined. Parameters are defined like '--paramX=valueX'.
Method definition: ::run [default] ( int $param1, bool $param2, undefined $param3=null, string $param4='' )
./cmdrun.php TestRunClass --param1=541 --param2=false
in method TestRunClass::run
receive parameters:
param1:541
param2:false
param3:
param4:
./cmdrun.php TestRunClass --param1=541 --param2=false --param3=11 --param4="hey you!"
in method TestRunClass::run
receive parameters:
param1:541
param2:false
param3:11
param4:hey you!
其中 TestRunClass 类在此定义
<?php
use Apolinux\Commandable;
class TestRunClass implements Commandable{
public function run(int $param1, bool $param2, $param3=null, string $param4=''){
echo "in method ". __METHOD__ .PHP_EOL ;
echo "receive parameters: ". PHP_EOL;
echo "param1:". ($param1). PHP_EOL;
echo "param2:". ($param2 ? 'true' : 'false'). PHP_EOL;
echo "param3:". ($param3). PHP_EOL;
echo "param4:". ($param4). PHP_EOL;
}
public function processSome($name, $phone, $amount=10){
echo "name: $name, $phone:$phone, $amount:$amount". PHP_EOL ;
}
}
0.7 版本新增
使用匿名类和自定义类。它可以与匿名类列表一起使用。例如
$argv=['cmd', '-l'];
class testInternal {
public function run(){
echo 'in class testInternal' ;
}
};
class testInternal2{
public function run(){
echo 'in class testInternal2';
}
};
$argv=['fido', '-l'];
$class_list = [
'testanonymous1' => new class{
public function run(){
echo 'in class testanonymous1' ;
}
} ,
'testanonymous2' => new class{
public function run(){
echo 'in class testanonymous2' ;
}
}
];
// run command
$class_container = new ClassContainer();
$class_container->add(new DirectoryClassType(__DIR__ .'/Commands'));
$class_container->add(new AnonymousClassType($class_list));
$class_container->add(new InternalClassType(['testInternal',testInternal2::class ]) ); // define explicitamente las clases
$cmd = new \Apolinux\CommandRun($class_container);
$cmd->start($argv);
将得到类似以下内容
php -d display_errors=1 test.php -l
classes list:
* testanonymous1
* testanonymous2
* testInternal
* testInternal2
To view method details of class run as test.php ... ClassName -h
详细信息
CommandRun::start 方法接收一个可选参数,用于替换全局 $argv 参数列表,如果需要的话。