1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\ErrorHandler; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait for handling errors on shutdown |
7
|
|
|
*/ |
8
|
|
|
trait HandleShutdownError |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var boolean |
12
|
|
|
*/ |
13
|
|
|
protected $registeredShutdown = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A string which reserves memory that can be used to log the error in case of an out of memory fatal error |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $reservedMemory; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Run the fatal error callback |
24
|
|
|
* |
25
|
|
|
* @param \Exception|\Error $error |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
abstract protected function callOnFatalError($error); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Wrapper method for `error_get_last` |
32
|
|
|
* |
33
|
|
|
* @return array|null |
34
|
|
|
*/ |
35
|
|
|
abstract protected function errorGetLast(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Wrapper method for `register_shutdown_function` |
39
|
|
|
* |
40
|
|
|
* @param callable $callback |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
abstract protected function registerShutdownFunction($callback); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Log an error or exception |
47
|
|
|
* |
48
|
|
|
* @param \Exception|\Error $error |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
abstract public function log($error); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the types of errors that will be logged |
55
|
|
|
* |
56
|
|
|
* @return int Binary set of E_* constants |
57
|
|
|
*/ |
58
|
|
|
abstract public function getLoggedErrorTypes(); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Reserve memory for shutdown function in case of out of memory |
63
|
|
|
*/ |
64
|
32 |
|
protected function reserveMemory() |
65
|
|
|
{ |
66
|
32 |
|
$this->reservedMemory = str_repeat(' ', 10 * 1024); |
67
|
32 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register the shutdown function |
71
|
|
|
*/ |
72
|
32 |
|
protected function initShutdownFunction() |
73
|
|
|
{ |
74
|
32 |
|
if (!$this->registeredShutdown) { |
75
|
32 |
|
$this->registerShutdownFunction([$this, 'shutdownFunction']) ?: false; |
76
|
32 |
|
$this->registeredShutdown = true; |
77
|
|
|
|
78
|
32 |
|
$this->reserveMemory(); |
79
|
|
|
} |
80
|
32 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Called when the script has ends |
84
|
|
|
* @ignore |
85
|
|
|
*/ |
86
|
16 |
|
public function shutdownFunction() |
87
|
|
|
{ |
88
|
16 |
|
$this->reservedMemory = null; |
89
|
|
|
|
90
|
16 |
|
$err = $this->errorGetLast(); |
91
|
16 |
|
$unhandled = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR; |
92
|
|
|
|
93
|
16 |
|
if (empty($err) || !($err['type'] & $unhandled)) { |
94
|
6 |
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
10 |
|
$error = new \ErrorException($err['message'], 0, $err['type'], $err['file'], $err['line']); |
98
|
|
|
|
99
|
10 |
|
if ($err['type'] & $this->getLoggedErrorTypes()) { |
100
|
4 |
|
$this->log($error); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
$this->callOnFatalError($error); |
104
|
10 |
|
} |
105
|
|
|
} |
106
|
|
|
|