|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: 麦当苗儿 <[email protected]> <http://zjzit.cn> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think; |
|
14
|
|
|
|
|
15
|
|
|
use think\console\Output as ConsoleOutput; |
|
16
|
|
|
use think\exception\ErrorException; |
|
17
|
|
|
use think\exception\Handle; |
|
18
|
|
|
use Throwable; |
|
19
|
|
|
|
|
20
|
|
|
class Error |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** @var App */ |
|
|
|
|
|
|
23
|
|
|
protected $app; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* 注册异常处理 |
|
27
|
|
|
* @access public |
|
28
|
|
|
* @param App $app |
|
|
|
|
|
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function register(App $app) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->app = $app; |
|
34
|
|
|
error_reporting(E_ALL); |
|
35
|
|
|
set_error_handler([$this, 'appError']); |
|
36
|
|
|
set_exception_handler([$this, 'appException']); |
|
37
|
|
|
register_shutdown_function([$this, 'appShutdown']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Exception Handler |
|
42
|
|
|
* @access public |
|
43
|
|
|
* @param \Throwable $e |
|
|
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
|
|
45
|
|
|
public function appException(Throwable $e): void |
|
46
|
|
|
{ |
|
47
|
|
|
$handler = $this->getExceptionHandler(); |
|
48
|
|
|
|
|
49
|
|
|
$handler->report($e); |
|
50
|
|
|
|
|
51
|
|
|
if (PHP_SAPI == 'cli') { |
|
52
|
|
|
$handler->renderForConsole(new ConsoleOutput, $e); |
|
53
|
|
|
} else { |
|
54
|
|
|
$handler->render($e)->send(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Error Handler |
|
60
|
|
|
* @access public |
|
61
|
|
|
* @param integer $errno 错误编号 |
|
62
|
|
|
* @param string $errstr 详细错误信息 |
|
63
|
|
|
* @param string $errfile 出错的文件 |
|
64
|
|
|
* @param integer $errline 出错行号 |
|
65
|
|
|
* @throws ErrorException |
|
66
|
|
|
*/ |
|
|
|
|
|
|
67
|
|
|
public function appError(int $errno, string $errstr, string $errfile = '', int $errline = 0): void |
|
68
|
|
|
{ |
|
69
|
|
|
$exception = new ErrorException($errno, $errstr, $errfile, $errline); |
|
70
|
|
|
|
|
71
|
|
|
if (error_reporting() & $errno) { |
|
72
|
|
|
// 将错误信息托管至 think\exception\ErrorException |
|
73
|
|
|
throw $exception; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Shutdown Handler |
|
79
|
|
|
* @access public |
|
80
|
|
|
*/ |
|
|
|
|
|
|
81
|
|
|
public function appShutdown(): void |
|
82
|
|
|
{ |
|
83
|
|
|
if (!is_null($error = error_get_last()) && $this->isFatal($error['type'])) { |
|
|
|
|
|
|
84
|
|
|
// 将错误信息托管至think\ErrorException |
|
85
|
|
|
$exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']); |
|
86
|
|
|
|
|
87
|
|
|
$this->appException($exception); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// 写入日志 |
|
91
|
|
|
$this->app->make(Log::class)->save(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* 确定错误类型是否致命 |
|
96
|
|
|
* |
|
97
|
|
|
* @access protected |
|
98
|
|
|
* @param int $type |
|
|
|
|
|
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function isFatal(int $type): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get an instance of the exception handler. |
|
108
|
|
|
* |
|
109
|
|
|
* @access protected |
|
110
|
|
|
* @return Handle |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getExceptionHandler() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->app->make(Handle::class); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|