栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

Error.php

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Error.php

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 麦当苗儿

// +----------------------------------------------------------------------

 

namespace think;

 

use thinkconsoleOutput as ConsoleOutput;// 支持 控制台 输出

use thinkexceptionErrorException;// 错误 异常

use thinkexceptionHandle;// 手柄 处理 助手

use thinkexceptionThrowableError;// 可抛出 错误

// namespace

// 使用 各种 错误处理机制

class Error

{// 公共 错误 异常

   

    public static function register()

    {// 注册 全部错误,

        error_reporting(E_ALL);// 设置 错误 级别

        set_error_handler([__CLASS__, 'appError']);// 设置 错误处理方式

        set_exception_handler([__CLASS__, 'appException']);// 设置异常 处理

        register_shutdown_function([__CLASS__, 'appShutdown']);// 注册关闭 函数

    }

 

   

    public static function appException($e)

    {// Exception Handler 错误处理 代码

        if (!$e instanceof Exception) {

            $e = new ThrowableError($e);// 如果 发送 过来的不是 异常,包装一下,然后 抛出

        }

 

        self::getExceptionHandler()->report($e);// 获取处理 助手,然后 进行 $e 发送

        if (IS_CLI) {// 是否 命令行 模式

            self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);// 渲染问题

        } else {

            self::getExceptionHandler()->render($e)->send();// 发送 渲染

        }

    }

 

   

    public static function appError($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])

    {//

        $exception = new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);

        if (error_reporting() & $errno) {

            // 将错误信息托管至 thinkexceptionErrorException

            throw $exception;

        } else {

            self::getExceptionHandler()->report($exception);

        }// 展示错误

    }

 

   

    public static function appShutdown()

    {

        if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {

            // 将错误信息托管至thinkErrorException

            $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);

 

            self::appException($exception);

        }// 转包 错误 日志

 

        // 写入日志

        Log::save();// 有了自动加载,简直就是上了一个新台阶啊!哈哈

    }

 

   

    protected static function isFatal($type)

    {

        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);

    }// 检测是否致命错误

 

   

    public static function getExceptionHandler()

    {

        static $handle;

        if (!$handle) {

            // 异常处理handle

            $class = Config::get('exception_handle');// Config::get("")

            if ($class && class_exists($class) && is_subclass_of($class, "\think\exception\Handle")) {

                $handle = new $class;// 找到对应的 类处理

            } else {

                $handle = new Handle;// 生成 对应的 Handle

            }

        }

        return $handle;

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号