Easyswoole源码分析-7-help

介绍

所有command类都实现了CommandInterface接口

interface CommandInterface
{
    public function commandName():string;
    public function exec(array $args):?string ;
    public function help(array $args):?string ;
}

分析

CommandRunner类里面当输入不存在的command时,会默认为help

function run(array $args):?string 
{
       ···
        if(!CommandContainer::getInstance()->get($command)){
            $command = 'help';
        }
       ···
}

help类

class Help implements CommandInterface
{

    public function commandName(): string
    {
        // TODO: Implement commandName() method.
        return 'help';
    }

    public function exec(array $args): ?string
    {
        // TODO: Implement exec() method.
        // 如果没有任何command,则直接调用本类的help方法
        if (!isset($args[0])) {
            return $this->help($args);
        } else {
            $actionName = $args[0];
            array_shift($args);
            // 获取相应命令的对象
            $call = CommandContainer::getInstance()->get($actionName);
            // 是否实现了CommandInterface接口
            if ($call instanceof CommandInterface) {
                // 执行相应command类的help方法
                return $call->help($args);
            } else {
                return "no help message for command {$actionName} was found";
            }
        }
    }

    public function help(array $args): ?string
    {
        // TODO: Implement help() method.
        // 获取所有command
        $allCommand = implode(PHP_EOL, CommandContainer::getInstance()->getCommandList());
        // 展示es log
        $logo = Utility::easySwooleLog();
        return $logo.<<<HELP
Welcome To EASYSWOOLE Command Console!
Usage: php easyswoole [command] [arg]
Get help : php easyswoole help [command]
Current Register Command:
{$allCommand}
HELP;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值