自定義命令
EasySwoole 3.3.x
默認自帶有 5 個命令,如下所示:
php easyswoole.php help 命令幫助
php easyswoole.php install 安裝(需要在./vendor/easyswoole/easyswoole/bin/easyswoole 文件中調用)
php easyswoole.php start 啟動
php easyswoole.php stop 停止(需要守護進程)
php easyswoole.php reload 熱重啟(需要守護進程)
默認命令詳細內容可查看 服務管理
定義命令
通過實現 \EasySwoole\EasySwoole\Command\CommandInterface
接口,用戶可自定義命令:
需要實現的接口如下:
<?php
namespace EasySwoole\EasySwoole\Command;
interface CommandInterface
{
public function commandName():string;
public function exec(array $args):?string ;
public function help(array $args):?string ;
}
新建文件 App/Command/Test.php
:
<?php
namespace App\Command;
use EasySwoole\EasySwoole\Command\CommandInterface;
use EasySwoole\EasySwoole\Command\Utility;
class Test implements CommandInterface
{
public function commandName(): string
{
return 'test';
}
public function exec(array $args): ?string
{
//打印參數,打印測試值
var_dump($args);
echo 'test'.PHP_EOL;
return null;
}
public function help(array $args): ?string
{
//輸出logo
$logo = Utility::easySwooleLog();
return $logo."this is test";
}
}
注冊自定義命令
查看 boostrap事件
在項目根目錄中新增 bootstrap.php
文件,添加內容如下,實現注冊自定義命令:
<?php
\EasySwoole\EasySwoole\Command\CommandContainer::getInstance()->set(new \App\Command\Test());
bootstrap
是 3.2.5
新增的事件,它允許用戶在框架初始化之前執行自定義事件。
執行命令及執行結果
$ php easyswoole.php test
array(0) {
}
test
$ php easyswoole.php test 123 456
array(2) {
[0]=>
string(3) "123"
[1]=>
string(3) "456"
}
test
$ php easyswoole.php help test
______ _____ _
| ____| / ____| | |
| |__ __ _ ___ _ _ | (___ __ __ ___ ___ | | ___
| __| / _` | / __| | | | | \___ \ \ \ /\ / / / _ \ / _ \ | | / _ \
| |____ | (_| | \__ \ | |_| | ____) | \ V V / | (_) | | (_) | | | | __/
|______| \__,_| |___/ \__, | |_____/ \_/\_/ \___/ \___/ |_| \___|
__/ |
|___/
this is test