自定义命令

EasySwoole 有着默认的5个命令:

  1. php easyswoole help 命令帮助
  2. php easyswoole install 安装(需要在./vendor/easyswoole/easyswoole/bin/easyswoole 文件中调用)
  3. php easyswoole start 启动
  4. php easyswoole stop 停止(需要守护进程)
  5. php easyswoole reload 热重启(需要守护进程)

::: warning 默认命令详细内容可查看服务管理 :::

定义命令

通过实现EasySwoole\EasySwoole\Command\CommandInterface接口,可自定义命令:

  1. <?php
  2. public function commandName():string;
  3. public function exec(array $args):?string ;
  4. public function help(array $args):?string ;

新建文件 App/Command/Test.php:

  1. <?php
  2. namespace App\Command;
  3. use EasySwoole\EasySwoole\Command\CommandInterface;
  4. use EasySwoole\EasySwoole\Command\Utility;
  5. class Test implements CommandInterface
  6. {
  7. public function commandName(): string
  8. {
  9. return 'test';
  10. }
  11. public function exec(array $args): ?string
  12. {
  13. //打印参数,打印测试值
  14. var_dump($args);
  15. echo 'test'.PHP_EOL;
  16. return null;
  17. }
  18. public function help(array $args): ?string
  19. {
  20. //输出logo
  21. $logo = Utility::easySwooleLog();
  22. return $logo."this is test";
  23. }
  24. }

注入命令

::: tip 查看 boostrap事件 :::

新增/bootstrap.php文件:

  1. <?php
  2. \EasySwoole\EasySwoole\Command\CommandContainer::getInstance()->set(new \App\Command\Test());

::: warning bootstrap是3.2.5新增的事件,它允许用户在框架初始化之前执行自定义事件 :::

执行命令

  1. php easyswoole test
  2. array(0) {
  3. }
  4. test
  5. php easyswoole test 123 456
  6. array(2) {
  7. [0]=>
  8. string(3) "123"
  9. [1]=>
  10. string(3) "456"
  11. }
  12. test
  13. [root@localhost easyswoole-test]# php easyswoole help test
  14. ______ _____ _
  15. | ____| / ____| | |
  16. | |__ __ _ ___ _ _ | (___ __ __ ___ ___ | | ___
  17. | __| / _` | / __| | | | | \___ \ \ \ /\ / / / _ \ / _ \ | | / _ \
  18. | |____ | (_| | \__ \ | |_| | ____) | \ V V / | (_) | | (_) | | | | __/
  19. |______| \__,_| |___/ \__, | |_____/ \_/\_/ \___/ \___/ |_| \___|
  20. __/ |
  21. |___/
  22. this is test