1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @license MIT |
4
|
|
|
*/ |
5
|
|
|
namespace Pivasic\Bundle\Debug; |
6
|
|
|
|
7
|
|
|
use Pivasic\Bundle\Template\Native; |
8
|
|
|
use Pivasic\Core\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Handle exception or fatal error and output it. |
12
|
|
|
* |
13
|
|
|
* Class Wrap |
14
|
|
|
* @package Pivasic\Bundle\Debug |
15
|
|
|
*/ |
16
|
|
|
class Wrap |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public static function isEnabled() |
22
|
|
|
{ |
23
|
|
|
return self::$debugEnabled; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $path |
28
|
|
|
*/ |
29
|
|
|
public static function setPackageRoot($path) |
30
|
|
|
{ |
31
|
|
|
self::$packageRoot = $path; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Register error handle. |
36
|
|
|
*/ |
37
|
|
|
public static function register() |
38
|
|
|
{ |
39
|
|
|
self::$debugEnabled = true; |
40
|
|
|
set_error_handler('Dspbee\Bundle\Debug\Wrap::render'); |
41
|
|
|
register_shutdown_function(['Dspbee\Bundle\Debug\Wrap', 'handleFatal']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handle fatal error. |
46
|
|
|
*/ |
47
|
|
|
public static function handleFatal() |
48
|
|
|
{ |
49
|
|
|
$error = error_get_last(); |
50
|
|
|
if(null !== $error) { |
51
|
|
|
self::render($error["type"], $error["message"], $error["file"], $error["line"]); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Handle exception. |
57
|
|
|
* |
58
|
|
|
* @param \Throwable $e |
59
|
|
|
*/ |
60
|
|
|
public static function handleException(\Throwable $e) |
61
|
|
|
{ |
62
|
|
|
self::render($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine(), null, $e->getTrace()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Send response. |
67
|
|
|
* |
68
|
|
|
* @param string $code |
69
|
|
|
* @param string $message |
70
|
|
|
* @param string $file |
71
|
|
|
* @param string $line |
72
|
|
|
* @param null $context |
73
|
|
|
* @param array|null $backtrace |
74
|
|
|
*/ |
75
|
|
|
public static function render($code, $message, $file, $line, $context = null, array $backtrace = []) |
76
|
|
|
{ |
77
|
|
|
if (ob_get_length()) { |
78
|
|
|
ob_clean(); |
79
|
|
|
} |
80
|
|
|
$data = [ |
81
|
|
|
'message' => $message, |
82
|
|
|
'code' => $code, |
83
|
|
|
'file' => $file, |
84
|
|
|
'line' => $line, |
85
|
|
|
'trace' => $backtrace, |
86
|
|
|
'context' => $context |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
if (!empty($data['trace'])) { |
90
|
|
|
foreach ($data['trace'] as $k => $item) { |
91
|
|
|
if (isset($item['type'])) { |
92
|
|
|
switch ($item['type']) { |
93
|
|
|
case '->': |
94
|
|
|
$data['trace'][$k]['type'] = 'method'; |
95
|
|
|
break; |
96
|
|
|
case '::': |
97
|
|
|
$data['trace'][$k]['type'] = 'static method'; |
98
|
|
|
break; |
99
|
|
|
default: |
100
|
|
|
$data['trace'][$k]['type'] = 'function'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$template = new Native(self::$packageRoot); |
107
|
|
|
$response = new Response(); |
108
|
|
|
$response->setStatusCode(418); |
109
|
|
|
$response->setContent($template->getContent('catch.html.php', $data)); |
110
|
|
|
$response->send(); |
111
|
|
|
exit; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private static $debugEnabled = false; |
115
|
|
|
private static $packageRoot = ''; |
116
|
|
|
} |