Whoops

Easyswoole 提供了Whoops驱动,用于开发阶段,友好的排除HTTP业务的错误与异常。

Whoops - 图1

切勿用于生产阶段,否则造成代码泄露EasySwoole不负任何责任!!!

组件要求

  • easyswoole/component: ^2.0
  • easyswoole/spl: ^1.1
  • easyswoole/template: ^1.0
  • easyswoole/utility: ^1.0
  • psr/log: ^1.0.1

安装方法

composer require easyswoole/easy-whoops=3.x

仓库地址

easyswoole/easy-whoops=3.x

基本使用

直接在 EasySwoole 全局事件中进行注册

  1. <?php
  2. namespace EasySwoole\EasySwoole;
  3. use EasySwoole\EasySwoole\AbstractInterface\Event;
  4. use EasySwoole\EasySwoole\Swoole\EventRegister;
  5. class EasySwooleEvent implements Event
  6. {
  7. public static function initialize()
  8. {
  9. // TODO: Implement initialize() method.
  10. date_default_timezone_set('Asia/Shanghai');
  11. \EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_ON_REQUEST, function (\EasySwoole\Http\Request $request, \EasySwoole\Http\Response $response): bool {
  12. // 拦截请求
  13. if (\EasySwoole\EasySwoole\Core::getInstance()->runMode() == 'dev') {
  14. \EasySwoole\Whoops\Run::attachRequest($request, $response);
  15. }
  16. return true;
  17. });
  18. if (\EasySwoole\EasySwoole\Core::getInstance()->runMode() == 'dev') {
  19. $whoops = new \EasySwoole\Whoops\Run();
  20. $whoops->pushHandler(new \EasySwoole\Whoops\Handler\PrettyPageHandler()); // 输出一个漂亮的页面
  21. $whoops->pushHandler(new \EasySwoole\Whoops\Handler\CallbackHandler(function ($exception, $inspector, $run, $handle) {
  22. // 可以推进多个Handle 支持回调做更多后续处理
  23. }));
  24. $whoops->register();
  25. }
  26. }
  27. public static function mainServerCreate(EventRegister $register)
  28. {
  29. if (\EasySwoole\EasySwoole\Core::getInstance()->runMode() == 'dev') {
  30. \EasySwoole\Whoops\Run::attachTemplateRender(ServerManager::getInstance()->getSwooleServer());
  31. }
  32. }
  33. }

经过上面配置完成之后,就可以在框架抛出异常时,输出一个漂亮的异常页面。