1 | <?php |
||
9 | class ErrorHandler implements ErrorHandling |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $handlers = []; |
||
15 | |||
16 | /** |
||
17 | * @codeCoverageIgnore |
||
18 | */ |
||
19 | public function __construct() |
||
30 | |||
31 | /** |
||
32 | * @param $exception |
||
33 | * @param Closure $handler |
||
34 | * @return $this |
||
35 | */ |
||
36 | 11 | public function register($exception, Closure $handler) |
|
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() |
|
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() |
||
109 | } |
||
110 |