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
|
|
|
*/ |
27
|
|
|
abstract protected function callOnFatalError($error); |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Wrapper method for `error_get_last` |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
abstract protected function errorGetLast(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Wrapper method for `register_shutdown_function` |
38
|
|
|
* |
39
|
|
|
* @param callable $callback |
40
|
|
|
*/ |
41
|
|
|
abstract protected function registerShutdownFunction($callback); |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Log an error or exception |
45
|
|
|
* |
46
|
|
|
* @param \Exception|\Error $error |
47
|
|
|
*/ |
48
|
|
|
abstract public function log($error); |
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Reserve memory for shutdown function in case of out of memory |
53
|
|
|
*/ |
54
|
32 |
|
protected function reserveMemory() |
55
|
|
|
{ |
56
|
32 |
|
$this->reservedMemory = str_repeat(' ', 10 * 1024); |
57
|
32 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register the shutdown function |
61
|
|
|
*/ |
62
|
32 |
|
protected function initShutdownFunction() |
63
|
|
|
{ |
64
|
32 |
|
if (!$this->registeredShutdown) { |
65
|
32 |
|
$this->registerShutdownFunction([$this, 'shutdownFunction']) ?: false; |
66
|
32 |
|
$this->registeredShutdown = true; |
67
|
|
|
|
68
|
32 |
|
$this->reserveMemory(); |
69
|
16 |
|
} |
70
|
32 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Called when the script has ends |
74
|
|
|
* @ignore |
75
|
|
|
*/ |
76
|
16 |
|
public function shutdownFunction() |
77
|
|
|
{ |
78
|
16 |
|
$this->reservedMemory = null; |
79
|
|
|
|
80
|
16 |
|
$err = $this->errorGetLast(); |
81
|
16 |
|
$unhandled = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR; |
82
|
|
|
|
83
|
16 |
|
if (!$err || !($err['type'] & $unhandled)) { |
|
|
|
|
84
|
6 |
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
$error = new \ErrorException($err['message'], 0, $err['type'], $err['file'], $err['line']); |
88
|
|
|
|
89
|
10 |
|
if ($err['type'] & $this->logErrorTypes) { |
|
|
|
|
90
|
4 |
|
$this->log($error); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
10 |
|
$this->callOnFatalError($error); |
94
|
10 |
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.