|
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(string $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('Pivasic\Bundle\Debug\Wrap::render'); |
|
41
|
|
|
register_shutdown_function(['Pivasic\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(), get_class($e)); |
|
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 $backtrace |
|
74
|
|
|
* @param string $className |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function render(string $code, string $message, string $file, string $line, $context = null, array $backtrace = [], string $className = '') |
|
77
|
|
|
{ |
|
78
|
|
|
if (ob_get_length()) { |
|
79
|
|
|
ob_clean(); |
|
80
|
|
|
} |
|
81
|
|
|
$data = [ |
|
82
|
|
|
'message' => 'From ' . $className . ' ' . $message, |
|
83
|
|
|
'code' => $code, |
|
84
|
|
|
'file' => $file, |
|
85
|
|
|
'line' => $line, |
|
86
|
|
|
'trace' => $backtrace, |
|
87
|
|
|
'context' => $context |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
if (!empty($data['trace'])) { |
|
91
|
|
|
foreach ($data['trace'] as $k => $item) { |
|
92
|
|
|
if (isset($item['type'])) { |
|
93
|
|
|
switch ($item['type']) { |
|
94
|
|
|
case '->': |
|
95
|
|
|
$data['trace'][$k]['type'] = 'method'; |
|
96
|
|
|
break; |
|
97
|
|
|
case '::': |
|
98
|
|
|
$data['trace'][$k]['type'] = 'static method'; |
|
99
|
|
|
break; |
|
100
|
|
|
default: |
|
101
|
|
|
$data['trace'][$k]['type'] = 'function'; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$template = new Native(self::$packageRoot, '', false); |
|
108
|
|
|
$content = $template->getContent('catch', $data); |
|
109
|
|
|
$response = new Response(); |
|
110
|
|
|
$response->setStatusCode(418); |
|
111
|
|
|
$response->setContent($content); |
|
112
|
|
|
$response->send(); |
|
113
|
|
|
print_r($data); |
|
114
|
|
|
exit; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private static $debugEnabled = false; |
|
118
|
|
|
private static $packageRoot = ''; |
|
119
|
|
|
} |