在线看片18_亚洲性吧_在线污_神马午夜达达兔_奇米色网_在线免费看污

自定義命令

EasySwoole 默認(rèn)自帶有 5 個(gè)命令,如下所示:

php easyswoole.php crontab  對(duì)定時(shí)任務(wù)進(jìn)行管理
php easyswoole.php install  安裝(需要在./vendor/easyswoole/easyswoole/bin/easyswoole 文件中調(diào)用)
php easyswoole.php phpunit  執(zhí)行單元測(cè)試 
php easyswoole.php process  對(duì)自定義進(jìn)程進(jìn)行管理
php easyswoole.php server   啟動(dòng)、停止、重啟服務(wù)等
php easyswoole.php task     查看 task 任務(wù)的運(yùn)行狀態(tài)

默認(rèn)命令詳細(xì)內(nèi)容可查看 基礎(chǔ)管理命令

舊版本(3.4.x 之前版本)框架自定義命令的實(shí)現(xiàn)可查看 自定義命令 3.3.x

定義命令

通過(guò)實(shí)現(xiàn) \EasySwoole\EasySwoole\Command\CommandInterface 接口,用戶可自定義命令:

該接口定義的方法如下:

<?php

namespace EasySwoole\Command\AbstractInterface;

interface CommandInterface
{
    public function commandName(): string;

    public function exec(): ?string;

    public function help(CommandHelpInterface $commandHelp): CommandHelpInterface;

    public function desc(): string;
}

自定義命令使用示例

實(shí)現(xiàn)自定義命令接口(AbstractInterface)

新建文件 App/Command/Test.php,內(nèi)容如下:

<?php

namespace App\Command;

use EasySwoole\Command\AbstractInterface\CommandHelpInterface;
use EasySwoole\Command\AbstractInterface\CommandInterface;
use EasySwoole\Command\CommandManager;
use EasySwoole\EasySwoole\Command\Utility;

class Test implements CommandInterface
{
    // 設(shè)置命令名稱
    public function commandName(): string
    {
        return 'test';
    }

    // 處理執(zhí)行命令的邏輯
    public function exec(): ?string
    {
        // 獲取用戶輸入的命令參數(shù)
        $argv = CommandManager::getInstance()->getOriginArgv();

        if (count($argv) < 3) {
            echo "please input the action param!" . PHP_EOL;
            return null;
        }

        // remove test
        array_shift($argv);

        // 獲取 action 參數(shù)
        $action = $argv[1];

        // 下面就是對(duì) 自定義命令 的一些處理邏輯
        if (!$action) {
            echo "please input the action param!" . PHP_EOL;
            return null;
        }

        // 獲取 option 參數(shù)
        $optionArr = $argv[2] ?? [];

        switch ($action) {
            case 'echo_string':
                if ($optionArr) {
                    $strValue = explode('=', $optionArr);
                    echo $strValue[1] . PHP_EOL;
                } else {
                    echo 'this is test!' . PHP_EOL;
                }
                break;
            case 'echo_date':
                if ($optionArr) {
                    $strValue = explode('=', $optionArr);
                    echo "now is " . date('Y-m-d H:i:s') . ' ' . $strValue[1] . '!' . PHP_EOL;
                } else {
                    echo "now is " . date('Y-m-d H:i:s') . '!' . PHP_EOL;
                }
                break;
            case 'echo_logo':
                echo Utility::easySwooleLog();
                break;
            default:
                echo "the action {$action} is not existed!" . PHP_EOL;
        }
        return null;
    }

    public function help(CommandHelpInterface $commandHelp): CommandHelpInterface
    {
        // 添加 自定義action(action 名稱及描述)
        $commandHelp->addAction('echo_string', 'print the string');
        $commandHelp->addAction('echo_date', 'print the date');
        $commandHelp->addAction('echo_logo', 'print the logo');
        // 添加 自定義action 可選參數(shù)
        $commandHelp->addActionOpt('--str=str_value', 'the string to be printed ');
        return $commandHelp;
    }

    // 設(shè)置自定義命令描述
    public function desc(): string
    {
        return 'this is test command!';
    }
}

注冊(cè)自定義命令

bootstrap 事件 中注冊(cè)自定義命令。

修改項(xiàng)目根目錄的 bootstrap.php 文件,添加如下內(nèi)容實(shí)現(xiàn)注冊(cè)自定義命令:

<?php
//全局bootstrap事件
date_default_timezone_set('Asia/Shanghai');

# 【可選:調(diào)用 `initialize` 事件進(jìn)行初始化】
// EasySwoole\EasySwoole\Core::getInstance()->initialize();

\EasySwoole\Command\CommandManager::getInstance()->addCommand(new \App\Command\Test());

注意:如果用戶需要獲取配置文件的配置,需要先執(zhí)行 initialize 事件,調(diào)用 EasySwoole\EasySwoole\Core::getInstance()->initialize(); 進(jìn)行初始化。

bootstrap 事件3.2.5 新增的事件,它允許用戶在框架初始化之前執(zhí)行自定義事件。

執(zhí)行命令結(jié)果

$ php easyswoole.php test
please input the action param!

$ php easyswoole.php test -h
This is test command!
Usage:
  easyswoole test ACTION [--opts ...]
Actions:
  echo_string  print the string
  echo_date    print the date
  echo_logo    print the logo
Options:
  --str=str_value  the string to be printed 

$ php easyswoole.php test echo_string
this is test!

$ php easyswoole.php test echo_date
now is 2021-02-23 19:23:19!

$ php easyswoole.php test echo_logo
  ______                          _____                              _
 |  ____|                        / ____|                            | |
 | |__      __ _   ___   _   _  | (___   __      __   ___     ___   | |   ___
 |  __|    / _` | / __| | | | |  \___ \  \ \ /\ / /  / _ \   / _ \  | |  / _ \
 | |____  | (_| | \__ \ | |_| |  ____) |  \ V  V /  | (_) | | (_) | | | |  __/
 |______|  \__,_| |___/  \__, | |_____/    \_/\_/    \___/   \___/  |_|  \___|
                          __/ |
                         |___/

$ php easyswoole.php test echo_string --str="hello easyswoole"
hello easyswoole
主站蜘蛛池模板: 91在线观看免费高清 | 久操视频免费在线观看 | 伊人丁香 | 婷婷丁香色 | 中文字幕色站 | 日本h视频在线观看 | 欧美高清一区 | 日韩一本在线 | 影音先锋三级 | 欧美精品国产 | 国产在线精品一区 | 三级福利视频 | 欧美性猛交xxxx免费看 | 91视频免费观看网站 | 国产成人综合在线 | 在线观看成人免费视频 | 黄色一级大片在线免费看国产一 | 欧美aaa一级片 | 国产美女激情视频 | 91av在线免费 | 国产精品成人在线视频 | 日韩在线观看视频一区二区 | 国产精选一区 | 亚洲午夜视频在线 | 超碰91在线| 亚洲男人av | 亚洲美女一区二区三区 | 免费古装一级淫片潘金莲 | 亚洲区免费视频 | 丰满少妇一级 | 久久国产成人精品av | 亚洲在线免费视频 | 欧美一级片a | 日韩黄视频 | 色网站在线播放 | 国产精品亚洲精品 | 国产免费久久久 | 成人福利在线视频 | 国产精品一区二区视频 | 欧美专区第一页 | 麻豆明星ai换脸视频 |