apolinux/commandrun
Composer 安装命令:
composer require apolinux/commandrun
包简介
command line interface running method classes as parameters
README 文档
README
Run command line programs calling method from class, using parameters as method arguments.
Description
Running an unique command it lets to call methods from several classes inside a defined dir.
It shows a complete help about the program itself and the possible classes to be called.
Sometimes it is convenient to call commands with configurable parameters and convert them to method objects. This class let's to skip manually processing command line arguments and get info about every class configured to be called.
Example
It is provided a file called cmdrun.php to show how the program must be called:
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();
when is executed:
./cmdrun.php
it shows:
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.
using a test dir:
./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!
Where class TestRunClass is defined here:
<?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 ;
}
}
New in version 0.7
Using anonymous and custom classes. It can be used with a anonymous class list. Example:
$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);
will get something like this:
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
Details
The method CommandRun::start receives an optional parameter to replace global $argv argument list if it's required.
apolinux/commandrun 适用场景与选型建议
apolinux/commandrun 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 apolinux/commandrun 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 apolinux/commandrun 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-03-23