title: bootstrap事件 meta:

  • name: description content: bootstrap事件
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole|swoole|bootstrap事件

bootstrapEvent

Bootstrap allows other businesses to be initialized before the framework is initialized

This event was added after version 3.2.5

In the easyswoole startup script file that is generated after installation:

The bootstrap.php file in the application root directory will be automatically determined,If there is,Load。

So we can create this file in the application root directory and execute the initialization business code we want:Such as register command line support, global general functions and other functions。

  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 f you are an old swoole upgrade, you will need to delete the ‘/easyswoole’ file.

Then again PHP/vendor/easyswoole/easyswoole/bin/easyswoole install installation(Report errors or other reasons.Please review the framework installation section to perform the installation steps)

can use the bootstrap event :::

Call the coroutine API before starting

  1. use Swoole\Coroutine\Scheduler;
  2. $scheduler = new Scheduler();
  3. $scheduler->add(function() {
  4. /* Call the coroutine API */
  5. });
  6. $scheduler->start();
  7. //Clear all timers
  8. \Swoole\Timer::clearAll();