1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Throwable; |
4
|
|
|
|
5
|
|
|
use Dazzle\Throwable\Error\FatalError; |
6
|
|
|
use Dazzle\Throwable\Error\NoticeError; |
7
|
|
|
use Dazzle\Throwable\Error\WarningError; |
8
|
|
|
|
9
|
|
|
abstract class ErrorHandler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
const E_UNSUPPORTED = 8; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
const E_ERROR = 4; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
const E_WARNING = 2; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
const E_NOTICE = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected static $errHandler = '\Dazzle\Throwable\Error::toString'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected static $excHandler = '\Dazzle\Throwable\Exception::toString'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Invoke default Error Handler. |
43
|
|
|
* |
44
|
|
|
* @param int $code |
45
|
|
|
* @param string $message |
46
|
|
|
* @param string $file |
47
|
|
|
* @param int $line |
48
|
|
|
* @throws FatalError |
49
|
|
|
* @throws NoticeError |
50
|
|
|
* @throws WarningError |
51
|
|
|
*/ |
52
|
16 |
|
public static function handleError($code, $message, $file, $line) |
53
|
|
|
{ |
54
|
16 |
|
$list = static::getSystemError($code); |
|
|
|
|
55
|
16 |
|
$name = $list[0]; |
|
|
|
|
56
|
16 |
|
$type = $list[1]; |
57
|
|
|
|
58
|
16 |
|
$message = "\"$message\" in $file:$line"; |
59
|
|
|
|
60
|
|
|
switch ($type) |
61
|
|
|
{ |
62
|
16 |
|
case static::E_NOTICE: throw new NoticeError($message); |
|
|
|
|
63
|
12 |
|
case static::E_WARNING: throw new WarningError($message); |
|
|
|
|
64
|
7 |
|
case static::E_ERROR: throw new FatalError($message); |
|
|
|
|
65
|
1 |
|
default: return; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Invoke default Shutdown Handler. |
71
|
|
|
* |
72
|
|
|
* @param bool $forceKill |
73
|
|
|
*/ |
74
|
|
|
public static function handleShutdown($forceKill = false) |
75
|
|
|
{ |
76
|
|
|
$err = error_get_last(); |
77
|
|
|
|
78
|
|
|
try |
79
|
|
|
{ |
80
|
|
|
static::handleError($err['type'], $err['message'], $err['file'], $err['line']); |
81
|
|
|
} |
82
|
|
|
catch (\Error $ex) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
echo call_user_func(static::$errHandler, $ex) . PHP_EOL; |
85
|
|
|
} |
86
|
|
|
catch (\Exception $ex) |
87
|
|
|
{ |
88
|
|
|
echo call_user_func(static::$excHandler, $ex) . PHP_EOL; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($forceKill) |
92
|
|
|
{ |
93
|
|
|
posix_kill(posix_getpid(), 9); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $type |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
16 |
|
private static function getSystemError($type) |
102
|
|
|
{ |
103
|
|
|
switch($type) |
104
|
|
|
{ |
105
|
16 |
|
case E_ERROR: // 1 // |
106
|
1 |
|
return [ 'E_ERROR', static::E_ERROR ]; |
107
|
|
|
|
108
|
15 |
|
case E_WARNING: // 2 // |
109
|
1 |
|
return [ 'E_WARNING', static::E_WARNING ]; |
110
|
|
|
|
111
|
14 |
|
case E_PARSE: // 4 // |
112
|
1 |
|
return [ 'E_PARSE', static::E_ERROR ]; |
113
|
|
|
|
114
|
13 |
|
case E_NOTICE: // 8 // |
115
|
1 |
|
return [ 'E_NOTICE', static::E_NOTICE ]; |
116
|
|
|
|
117
|
12 |
|
case E_CORE_ERROR: // 16 // |
118
|
1 |
|
return [ 'E_CORE_ERROR', static::E_ERROR ]; |
119
|
|
|
|
120
|
11 |
|
case E_CORE_WARNING: // 32 // |
121
|
1 |
|
return [ 'E_CORE_WARNING', static::E_WARNING ]; |
122
|
|
|
|
123
|
10 |
|
case E_COMPILE_ERROR: // 64 // |
124
|
1 |
|
return [ 'E_COMPILE_ERROR', static::E_ERROR ]; |
125
|
|
|
|
126
|
9 |
|
case E_COMPILE_WARNING: // 128 // |
127
|
1 |
|
return [ 'E_COMPILE_WARNING', static::E_WARNING ]; |
128
|
|
|
|
129
|
8 |
|
case E_USER_ERROR: // 256 // |
130
|
1 |
|
return [ 'E_USER_ERROR', static::E_ERROR ]; |
131
|
|
|
|
132
|
7 |
|
case E_USER_WARNING: // 512 // |
133
|
1 |
|
return [ 'E_USER_WARNING', static::E_WARNING ]; |
134
|
|
|
|
135
|
6 |
|
case E_USER_NOTICE: // 1024 // |
136
|
1 |
|
return [ 'E_USER_NOTICE', static::E_NOTICE ]; |
137
|
|
|
|
138
|
5 |
|
case E_STRICT: // 2048 // |
139
|
1 |
|
return [ 'E_STRICT', static::E_ERROR ]; |
140
|
|
|
|
141
|
4 |
|
case E_RECOVERABLE_ERROR: // 4096 // |
142
|
1 |
|
return [ 'E_RECOVERABLE_ERROR', static::E_WARNING ]; |
143
|
|
|
|
144
|
3 |
|
case E_DEPRECATED: // 8192 // |
145
|
1 |
|
return [ 'E_DEPRECATED', static::E_NOTICE ]; |
146
|
|
|
|
147
|
2 |
|
case E_USER_DEPRECATED: // 16384 // |
148
|
1 |
|
return [ 'E_USER_DEPRECATED', static::E_NOTICE ]; |
149
|
|
|
|
150
|
|
|
default: |
151
|
1 |
|
return [ 'E_UNKNOWN', static::E_UNSUPPORTED ]; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: