1
|
|
|
<?php |
2
|
|
|
namespace suda\application; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use phpDocumentor\Reflection\File; |
6
|
|
|
use suda\framework\debug\DebugObject; |
7
|
|
|
use suda\framework\filesystem\FileSystem; |
8
|
|
|
use suda\framework\Request; |
9
|
|
|
use suda\framework\Response; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DebugDumper |
14
|
|
|
* @package suda\application |
15
|
|
|
*/ |
16
|
|
|
class DebugDumper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* 应用 |
20
|
|
|
* |
21
|
|
|
* @var Application |
22
|
|
|
*/ |
23
|
|
|
protected $application; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Request |
27
|
|
|
*/ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Response |
32
|
|
|
*/ |
33
|
|
|
protected $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* DebugDumper constructor. |
37
|
|
|
* @param Application $application |
38
|
|
|
* @param Request $request |
39
|
|
|
* @param Response $response |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Application $application, Request $request, Response $response) |
42
|
|
|
{ |
43
|
|
|
$this->application = $application; |
44
|
|
|
$this->request = $request; |
45
|
|
|
$this->response = $response; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 注册错误处理函数 |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function register() |
53
|
|
|
{ |
54
|
|
|
set_exception_handler([$this,'uncaughtException']); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 异常托管 |
60
|
|
|
* |
61
|
|
|
* @param Throwable $exception |
62
|
|
|
* @return void |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function uncaughtException($exception) |
66
|
|
|
{ |
67
|
|
|
$this->application->debug()->addIgnorePath(__FILE__); |
68
|
|
|
$this->application->debug()->uncaughtException($exception); |
69
|
|
|
$this->dumpThrowable($exception); |
70
|
|
|
if ($this->response->isSend() === false) { |
71
|
|
|
$this->response->sendContent($exception); |
72
|
|
|
$this->response->end(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Throwable $throwable |
78
|
|
|
*/ |
79
|
|
|
public function dumpThrowable($throwable) |
80
|
|
|
{ |
81
|
|
|
$dumper = [ |
82
|
|
|
'time' => time(), |
83
|
|
|
'throwable' => $throwable, |
84
|
|
|
'context' => [ |
85
|
|
|
'application' => $this->application, |
86
|
|
|
'request' => $this->request, |
87
|
|
|
'response' => $this->response, |
88
|
|
|
], |
89
|
|
|
'backtrace' => $throwable->getTrace(), |
90
|
|
|
]; |
91
|
|
|
$dumpPath = $this->application->getDataPath().'/logs/dump'; |
92
|
|
|
$exceptionHash = md5($throwable->getFile().$throwable->getLine().$throwable->getCode()); |
93
|
|
|
$path = $dumpPath.'/'.microtime(true).'.'.substr($exceptionHash, 0, 8).'.json'; |
94
|
|
|
FileSystem::make($dumpPath); |
95
|
|
|
FileSystem::put($path, json_encode( |
96
|
|
|
new DebugObject($dumper), |
97
|
|
|
JSON_PRETTY_PRINT |
98
|
|
|
| JSON_UNESCAPED_UNICODE |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|