定制 apolinux/commandrun 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-03-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固