[TOC]
* * * * *
## 1 錯誤處理文件源代碼(thinkphp\library\think\Error.php)
~~~
public static function register()
{
set_error_handler([__CLASS__, 'appError']);
set_exception_handler([__CLASS__, 'appException']);
register_shutdown_function([__CLASS__, 'appShutdown']);
}
~~~
~~~
public static function appException($exception)
{
if (!(APP_DEBUG || IS_API)) {
$error_page = Config::get('error_page');
if (!empty($error_page)) {
header("Location: {$error_page}");
}
}
if (APP_DEBUG) {
$data = [
'name' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace(),
'code' => self::getCode($exception),
'source' => self::getSourceCode($exception),
'datas' => self::getExtendData($exception),
'tables' => [
'GET Data' => $_GET,
'POST Data' => $_POST,
'Files' => $_FILES,
'Cookies' => $_COOKIE,
'Session' => isset($_SESSION) ? $_SESSION : [],
'Server/Request Data' => $_SERVER,
'Environment Variables' => $_ENV,
'ThinkPHP Constants' => self::getTPConst(),
],
];
$err_msg = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
} else {
$data = [
'code' => $exception->getCode(),
'message' => Config::get('show_error_msg') ? $exception->getMessage() : Config::get('error_message'),
];
$err_msg = "[{$data['code']}]{$data['message']}";
}
Log::record($err_msg, 'error');
self::output($exception, $data);
return true;
}
~~~
~~~
public static function appError($errno, $errstr, $errfile = null, $errline = 0, array $errcontext = [])
{
if ($errno & Config::get('exception_ignore_type')) {
Log::record("[{$errno}]{$errstr}[{$errfile}:{$errline}]", 'notice');
} else {
throw new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
return true;
}
}
~~~
~~~
public static function appShutdown()
{
Log::save();
if ($error = error_get_last()) {
$exception = new ErrorException(
$error['type'],
$error['message'],
$error['file'],
$error['line']
);
self::appException($exception);
return true;
}
return false;
}
~~~
~~~
public static function output($exception, array $vars)
{
if ($exception instanceof Exception) {
http_response_code($exception->getHttpStatus());
} else {
http_response_code(500);
}
$type = Config::get('default_return_type');
if (IS_API && 'html' != $type) {
APP_HOOK && Hook::listen('error_output', $data);
Response::send($data, $type, Config::get('response_return'));
} else {
extract($vars);
include Config::get('exception_tmpl');
}
}
~~~
~~~
private static function getCode($exception)
{
$code = $exception->getCode();
if (!$code && $exception instanceof ErrorException) {
$code = $exception->getSeverity();
}
return $code;
}
~~~
~~~
private static function getSourceCode($exception)
{
$line = $exception->getLine();
$first = ($line - 9 > 0) ? $line - 9 : 1;
try {
$contents = file($exception->getFile());
$source = [
'first' => $first,
'source' => array_slice($contents, $first - 1, 19),
];
} catch (Exception $e) {
$source = [];
}
return $source;
}
~~~
~~~
private static function getExtendData($exception)
{
$data = [];
if ($exception instanceof Exception) {
$data = $exception->getData();
}
return $data;
}
~~~
~~~
private static function getTPConst()
{
return [
'THINK_VERSION' => defined('THINK_VERSION') ? THINK_VERSION : 'undefined',
'THINK_PATH' => defined('THINK_PATH') ? THINK_PATH : 'undefined',
'LIB_PATH' => defined('LIB_PATH') ? LIB_PATH : 'undefined',
'EXTEND_PATH' => defined('EXTEND_PATH') ? EXTEND_PATH : 'undefined',
'MODE_PATH' => defined('MODE_PATH') ? MODE_PATH : 'undefined',
'CORE_PATH' => defined('CORE_PATH') ? CORE_PATH : 'undefined',
'ORG_PATH' => defined('ORG_PATH') ? ORG_PATH : 'undefined',
'TRAIT_PATH' => defined('TRAIT_PATH') ? TRAIT_PATH : 'undefined',
'APP_PATH' => defined('APP_PATH') ? APP_PATH : 'undefined',
'RUNTIME_PATH' => defined('RUNTIME_PATH') ? RUNTIME_PATH : 'undefined',
'DATA_PATH' => defined('DATA_PATH') ? DATA_PATH : 'undefined',
'LOG_PATH' => defined('LOG_PATH') ? LOG_PATH : 'undefined',
'CACHE_PATH' => defined('CACHE_PATH') ? CACHE_PATH : 'undefined',
'TEMP_PATH' => defined('TEMP_PATH') ? TEMP_PATH : 'undefined',
'VENDOR_PATH' => defined('VENDOR_PATH') ? VENDOR_PATH : 'undefined',
'MODULE_PATH' => defined('MODULE_PATH') ? MODULE_PATH : 'undefined',
'VIEW_PATH' => defined('VIEW_PATH') ? VIEW_PATH : 'undefined',
'APP_NAMESPACE' => defined('APP_NAMESPACE') ? APP_NAMESPACE : 'undefined',
'COMMON_MODULE' => defined('COMMON_MODULE') ? COMMON_MODULE : 'undefined',
'APP_MULTI_MODULE' => defined('APP_MULTI_MODULE') ? APP_MULTI_MODULE : 'undefined',
'MODULE_ALIAS' => defined('MODULE_ALIAS') ? MODULE_ALIAS : 'undefined',
'MODULE_NAME' => defined('MODULE_NAME') ? MODULE_NAME : 'undefined',
'CONTROLLER_NAME' => defined('CONTROLLER_NAME') ? CONTROLLER_NAME : 'undefined',
'ACTION_NAME' => defined('ACTION_NAME') ? ACTION_NAME : 'undefined',
'MODEL_LAYER' => defined('MODEL_LAYER') ? MODEL_LAYER : 'undefined',
'VIEW_LAYER' => defined('VIEW_LAYER') ? VIEW_LAYER : 'undefined',
'CONTROLLER_LAYER' => defined('CONTROLLER_LAYER') ? CONTROLLER_LAYER : 'undefined',
'APP_DEBUG' => defined('APP_DEBUG') ? APP_DEBUG : 'undefined',
'APP_HOOK' => defined('APP_HOOK') ? APP_HOOK : 'undefined',
'ENV_PREFIX' => defined('ENV_PREFIX') ? ENV_PREFIX : 'undefined',
'IS_API' => defined('IS_API') ? IS_API : 'undefined',
'APP_AUTO_BUILD' => defined('APP_AUTO_BUILD') ? APP_AUTO_BUILD : 'undefined',
'APP_AUTO_RUN' => defined('APP_AUTO_RUN') ? APP_AUTO_RUN : 'undefined',
'APP_MODE' => defined('APP_MODE') ? APP_MODE : 'undefined',
'REQUEST_METHOD' => defined('REQUEST_METHOD') ? REQUEST_METHOD : 'undefined',
'IS_CGI' => defined('IS_CGI') ? IS_CGI : 'undefined',
'IS_WIN' => defined('IS_WIN') ? IS_WIN : 'undefined',
'IS_CLI' => defined('IS_CLI') ? IS_CLI : 'undefined',
'IS_AJAX' => defined('IS_AJAX') ? IS_AJAX : 'undefined',
'IS_GET' => defined('IS_GET') ? IS_GET : 'undefined',
'IS_POST' => defined('IS_POST') ? IS_POST : 'undefined',
'IS_PUT' => defined('IS_PUT') ? IS_PUT : 'undefined',
'IS_DELETE' => defined('IS_DELETE') ? IS_DELETE : 'undefined',
'NOW_TIME' => defined('NOW_TIME') ? NOW_TIME : 'undefined',
'LANG_SET' => defined('LANG_SET') ? LANG_SET : 'undefined',
'EXT' => defined('EXT') ? EXT : 'undefined',
'DS' => defined('DS') ? DS : 'undefined',
'__INFO__' => defined('__INFO__') ? __INFO__ : 'undefined',
'__EXT__' => defined('__EXT__') ? __EXT__ : 'undefined',
];
}
~~~
## 2 文件分析
1 `public static function register(){}`
注冊框架的錯誤,異常,終止處理方法
關于php的異常,錯誤注冊見基礎原理的php的異常錯誤
2 `public static function appException($exception){}`
異常處理接口 appException()
3 `public static function appError($errno, $errstr, $errfile = null, $errline = 0, array $errcontext = []){}`
錯誤處理接口 appError()
4 `public static function appShutdown(){}`
終止處理接口 appShutdown()
5 `public static function output($exception, array $vars){}`
異常信息輸出 output()
這里包含異常模板文件exception_tmpl
6 `private static function getCode($exception){}`
獲取錯誤編碼 在上述異常處理接口調用
7 `private static function getSourceCode($exception){}`
獲取出錯文件內容 在上述異常處理接口調用
8 `private static function getExtendData($exception){}`
獲取異常擴展信息 在上述異常處理接口調用
9 `private static function getTPConst(){}`
獲取ThinPHP常量列表 在上述異常處理接口調用
## 3 使用方法
TODO:
## 4 總結
錯誤處理在框架運行出錯時的處理接口。
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)