bootstrap事件

bootstrap 允许在 框架未初始化之前,允许其他初始化业务

该事件是在3.2.5版本之后新增

在安装之后产生的easyswoole启动脚本文件中:

将会自动判断应用根目录下是否有bootstrap.php文件,如果有则加载。

所以我们可以在应用根目录下创建该文件,并执行自己想要的初始化业务代码:如注册命令行支持、全局通用函数等功能。

  1. <?php
  2. use EasySwoole\EasySwoole\Command\CommandRunner;
  3. defined('IN_PHAR') or define('IN_PHAR', boolval(\Phar::running(false)));
  4. defined('RUNNING_ROOT') or define('RUNNING_ROOT', realpath(getcwd()));
  5. defined('EASYSWOOLE_ROOT') or define('EASYSWOOLE_ROOT', IN_PHAR ? \Phar::running() : realpath(getcwd()));
  6. $file = EASYSWOOLE_ROOT.'/vendor/autoload.php';
  7. if (file_exists($file)) {
  8. require $file;
  9. }else{
  10. die("include composer autoload.php fail\n");
  11. }
  12. if(file_exists(EASYSWOOLE_ROOT.'/bootstrap.php')){
  13. require_once EASYSWOOLE_ROOT.'/bootstrap.php';
  14. }
  15. $args = $argv;
  16. //trim first command
  17. array_shift($args);
  18. $ret = CommandRunner::getInstance()->run($args);
  19. if(!empty($ret)){
  20. echo $ret."\n";
  21. }

::: warning 如果你是旧版升级新版,需要删除/easyswoole 文件

然后重新php ./vendor/easyswoole/easyswoole/bin/easyswoole install 安装(报错或者其他原因请重新看框架安装章节 执行安装步骤)

即可使用bootstrap事件 :::

启动前调用协程API

  1. use Swoole\Coroutine\Scheduler;
  2. $scheduler = new Scheduler();
  3. $scheduler->add(function() {
  4. /* 调用协程API */
  5. });
  6. $scheduler->start();
  7. //清除全部定时器
  8. \Swoole\Timer::clearAll();