1
|
|
|
<?php |
2
|
|
|
namespace Fyuze\Error; |
3
|
|
|
|
4
|
|
|
use Closure; |
5
|
|
|
use Exception; |
6
|
|
|
use ErrorException; |
7
|
|
|
use Fyuze\Error\Handlers\Exception as ExceptionHandler; |
8
|
|
|
|
9
|
|
|
class ErrorHandler implements ErrorHandling |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $handlers = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @codeCoverageIgnore |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
register_shutdown_function($this->handleFatalError()); |
22
|
|
|
set_error_handler($this->setErrorHandler()); |
23
|
|
|
set_exception_handler($this->setExceptionHandler()); |
24
|
|
|
|
25
|
|
|
$this->register('Exception', function (Exception $exception) { |
26
|
|
|
$handler = new ExceptionHandler($exception); |
27
|
|
|
$handler->display(); |
28
|
|
|
}); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $exception |
33
|
|
|
* @param Closure $handler |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
11 |
|
public function register($exception, Closure $handler) |
37
|
|
|
{ |
38
|
11 |
|
array_unshift($this->handlers, [$exception, $handler]); |
39
|
|
|
|
40
|
11 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Exception $exception |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
5 |
|
public function handle(Exception $exception) |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
|
52
|
|
|
$handlers = array_filter($this->handlers, function ($handler) use ($exception) { |
53
|
5 |
|
return $exception instanceof $handler[0]; |
54
|
5 |
|
}); |
55
|
|
|
|
56
|
5 |
|
foreach ($handlers as $handler) { |
57
|
5 |
|
$error = $handler[1]($exception); |
58
|
|
|
|
59
|
4 |
|
if ($error !== null) { |
60
|
1 |
|
break; |
61
|
|
|
} |
62
|
4 |
|
} |
63
|
|
|
|
64
|
5 |
|
} catch (Exception $e) { |
65
|
|
|
|
66
|
1 |
|
echo sprintf('[%d] %s in %s', $e->getLine(), $e->getMessage(), $e->getFile()); |
67
|
|
|
} |
68
|
5 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Closure |
72
|
|
|
*/ |
73
|
11 |
|
protected function setExceptionHandler() |
74
|
|
|
{ |
75
|
|
|
return function ($exception) { |
76
|
1 |
|
$this->handle($exception); |
77
|
11 |
|
}; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Closure |
82
|
|
|
* @throws ErrorException |
83
|
|
|
*/ |
84
|
11 |
|
protected function setErrorHandler() |
85
|
|
|
{ |
86
|
|
|
return function ($severity, $message, $file, $line) { |
87
|
2 |
|
if (error_reporting() && $severity) { |
88
|
2 |
|
throw new ErrorException($message, 0, $severity, $file, $line); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
11 |
|
}; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Closure |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
protected function handleFatalError() |
100
|
|
|
{ |
101
|
|
|
return function () { |
102
|
|
|
$error = error_get_last(); |
103
|
|
|
|
104
|
|
|
if ($error['type'] === E_ERROR) { |
105
|
|
|
$this->handle(new ErrorException($error['message'])); |
106
|
|
|
} |
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|