|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of coisa/error-handler. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Felipe Sayão Lobato Abreu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace CoiSA\ErrorHandler; |
|
15
|
|
|
|
|
16
|
|
|
use CoiSA\ErrorHandler\Handler\PhpErrorHandlerInterface; |
|
17
|
|
|
use CoiSA\ErrorHandler\Handler\PhpLastErrorShutdownHandler; |
|
18
|
|
|
use CoiSA\ErrorHandler\Handler\ShutdownHandlerInterface; |
|
19
|
|
|
use CoiSA\ErrorHandler\Handler\ThrowableHandlerInterface; |
|
20
|
|
|
use CoiSA\ErrorHandler\Handler\ThrowErrorExceptionPhpErrorHandler; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ErrorHandler |
|
24
|
|
|
* |
|
25
|
|
|
* @package CoiSA\ErrorHandler |
|
26
|
|
|
*/ |
|
27
|
|
|
class ErrorHandler implements |
|
28
|
|
|
ErrorHandlerInterface, |
|
29
|
|
|
ThrowableHandlerInterface, |
|
30
|
|
|
PhpErrorHandlerInterface, |
|
31
|
|
|
ShutdownHandlerInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var PhpErrorHandlerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $phpErrorHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ThrowableHandlerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $trowableHandler; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ShutdownHandlerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $shutdownHandler; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $isRegistered = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* ErrorHandler constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ThrowableHandlerInterface $throwableHandler |
|
57
|
|
|
* @param null|PhpErrorHandlerInterface $phpErrorHandler |
|
58
|
|
|
* @param null|ShutdownHandlerInterface $shutdownHandler |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public function __construct( |
|
61
|
|
|
ThrowableHandlerInterface $throwableHandler, |
|
62
|
|
|
PhpErrorHandlerInterface $phpErrorHandler = null, |
|
63
|
|
|
ShutdownHandlerInterface $shutdownHandler = null |
|
64
|
|
|
) { |
|
65
|
9 |
|
$this->trowableHandler = $throwableHandler; |
|
66
|
9 |
|
$this->phpErrorHandler = $phpErrorHandler ?? new ThrowErrorExceptionPhpErrorHandler(); |
|
67
|
9 |
|
$this->shutdownHandler = $shutdownHandler ?? new PhpLastErrorShutdownHandler($this); |
|
68
|
9 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Register error handler |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public function register(): void |
|
74
|
|
|
{ |
|
75
|
7 |
|
if ($this->isRegistered) { |
|
76
|
1 |
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
7 |
|
\set_error_handler([$this, 'handlePhpError']); |
|
80
|
7 |
|
\set_exception_handler([$this, 'handleThrowable']); |
|
81
|
7 |
|
\register_shutdown_function([$this, 'handleShutdown']); |
|
82
|
|
|
|
|
83
|
7 |
|
$this->isRegistered = true; |
|
84
|
7 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Unregister error-handler |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function unregister(): void |
|
90
|
|
|
{ |
|
91
|
2 |
|
if (false === $this->isRegistered) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
\restore_error_handler(); |
|
96
|
2 |
|
\restore_exception_handler(); |
|
97
|
|
|
|
|
98
|
2 |
|
$this->isRegistered = false; |
|
99
|
2 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param \Throwable $throwable |
|
103
|
|
|
*/ |
|
104
|
4 |
|
public function handleThrowable(\Throwable $throwable): void |
|
105
|
|
|
{ |
|
106
|
4 |
|
$this->trowableHandler->handleThrowable($throwable); |
|
107
|
4 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int $code |
|
111
|
|
|
* @param string $message |
|
112
|
|
|
* @param string $filename |
|
113
|
|
|
* @param int $line |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \ErrorException |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function handlePhpError(int $code, string $message, string $filename, int $line): void |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
2 |
|
$this->phpErrorHandler->handlePhpError($code, $message, $filename, $line); |
|
121
|
1 |
|
} catch (\ErrorException $throwable) { |
|
122
|
1 |
|
$this->handleThrowable($throwable); |
|
123
|
|
|
} |
|
124
|
2 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Handle shutdown if registered |
|
128
|
|
|
*/ |
|
129
|
|
|
public function handleShutdown(): void |
|
130
|
|
|
{ |
|
131
|
|
|
if (false === $this->isRegistered) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
try { |
|
136
|
|
|
$this->shutdownHandler->handleShutdown(); |
|
137
|
|
|
} catch (\Throwable $throwable) { |
|
138
|
|
|
$this->handleThrowable($throwable); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|