1
|
|
|
<?php /** MicroFatalError */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class FatalError |
7
|
|
|
* @package Micro\Base |
8
|
|
|
*/ |
9
|
|
|
class FatalError |
10
|
|
|
{ |
11
|
|
|
protected static $number = 0; |
12
|
|
|
protected static $message = ''; |
13
|
|
|
protected static $file = ''; |
14
|
|
|
protected static $line = 0; |
15
|
|
|
protected static $context = []; |
16
|
|
|
protected static $trace = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
public static function register() |
22
|
|
|
{ |
23
|
|
|
set_error_handler(['\Micro\Base\FatalError', 'handle']); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $number |
28
|
|
|
* @param string $message |
29
|
|
|
* @param string $file |
30
|
|
|
* @param int $line |
31
|
|
|
* @param array $context |
32
|
|
|
*/ |
33
|
|
|
public static function handle($number = 0, $message = '', $file = '', $line = 0, $context = []) |
34
|
|
|
{ |
35
|
|
|
self::$context = $context; |
36
|
|
|
self::$message = $message; |
37
|
|
|
self::$number = $number; |
38
|
|
|
self::$trace = debug_backtrace(); |
39
|
|
|
self::$file = $file; |
40
|
|
|
self::$line = $line; |
41
|
|
|
|
42
|
|
|
die('cli' === php_sapi_name() ? static::doCli() : static::doRun()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected static function doCli() |
49
|
|
|
{ |
50
|
|
|
return static::$number . ' - ' . static::$message . ' on ' . static::$file . ':' . static::$line; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected static function doRun() |
57
|
|
|
{ |
58
|
|
|
$str = '<div class="error" style="width: 100%;">'; |
59
|
|
|
$str .= '<h2>FatalError ' . static::$number . ' - ' . static::$message . ' on ' . static::$file . ':' . static::$line . '</h2>'; |
60
|
|
|
|
61
|
|
|
$str .= '<table width="100%" style="width: 100%">'; |
62
|
|
|
$str .= '<tr>'; |
63
|
|
|
$str .= '<th width="100px">Context</th>'; |
64
|
|
|
$str .= '<td style="vertical-align: top; height: 300px">'; |
65
|
|
|
$str .= '<textarea disabled style="width:100%; height: 100%">' . print_r(static::$context, true) . '</textarea>'; |
66
|
|
|
$str .= '</td>'; |
67
|
|
|
$str .= '</tr>'; |
68
|
|
|
$str .= '<tr>'; |
69
|
|
|
$str .= '<th width="100px">Debug trace</th>'; |
70
|
|
|
$str .= '<td style="vertical-align: top; height: 300px">'; |
71
|
|
|
$str .= '<textarea disabled style="width: 100%; height: 100%">' . print_r(static::$trace, true) . '</textarea>'; |
72
|
|
|
$str .= '</td>'; |
73
|
|
|
$str .= '</tr>'; |
74
|
|
|
$str .= '</table>'; |
75
|
|
|
$str .= '</div>'; |
76
|
|
|
|
77
|
|
|
return $str; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.