easyswoole/easy-whoops

Whoops provider for easySwoole framework 3.x

3.1.0 2021-01-20 13:28 UTC

This package is auto-updated.

Last update: 2024-12-21 10:26:03 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads

支持协程的错误美化组件,采用独立进程渲染来规避协程安全问题,让你的应用拥有一个友好的错误提示页面!

效果预览

Whoops!

安装类库

composer require easyswoole/easy-whoops=3.x

接管异常

在全局事件 EasySwooleEvent 中注册以下内容

<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Whoops\Handler\CallbackHandler;
use EasySwoole\Whoops\Handler\PrettyPageHandler;
use EasySwoole\Whoops\Run;
use EasySwoole\Component\Di;

class EasySwooleEvent implements Event
{
    /**
     * 框架初始化
     */
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
        $whoops = new Run();
        $whoops->pushHandler(new PrettyPageHandler);  // 输出一个漂亮的页面
        $whoops->pushHandler(new CallbackHandler(function ($exception, $inspector, $run, $handle) {
            // 可以推进多个Handle 支持回调做更多后续处理
        }));
        $whoops->register();
        
        // 收到请求时
        Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_ON_REQUEST, function (Request $request, Response $response): bool {
            // 拦截请求
            Run::attachRequest($request, $response);
            return true;
        });
        
        // 请求结束时
        Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_AFTER_REQUEST, function (Request $request, Response $response): void {
            // TODO: Implement afterAction() method.
        });
    }

    /**
     * 主服务启动前
     * @param EventRegister $register
     */
    public static function mainServerCreate(EventRegister $register)
    {
        Run::attachTemplateRender(ServerManager::getInstance()->getSwooleServer());
    }
    
}