|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Debug; |
|
4
|
|
|
|
|
5
|
|
|
use ErrorException; |
|
6
|
|
|
use Throwable; |
|
7
|
|
|
use Venta\Contracts\Debug\ErrorHandler as ErrorHandlerContract; |
|
8
|
|
|
use Venta\Contracts\Debug\ErrorRenderer; |
|
9
|
|
|
use Venta\Contracts\Debug\ErrorReporterAggregate; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ErrorHandler |
|
13
|
|
|
* |
|
14
|
|
|
* @package Venta\Debug |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ErrorHandler implements ErrorHandlerContract |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Controls whether error handler can throw exceptions. |
|
20
|
|
|
* E.g. shutdown handler cannot produce new exceptions. |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $canThrowException = true; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The error renderer. |
|
28
|
|
|
* |
|
29
|
|
|
* @var ErrorRenderer |
|
30
|
|
|
*/ |
|
31
|
|
|
private $renderer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The error reporter aggregate. |
|
35
|
|
|
* |
|
36
|
|
|
* @var ErrorReporterAggregate |
|
37
|
|
|
*/ |
|
38
|
|
|
private $reporters; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ErrorHandler constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ErrorRenderer $renderer |
|
44
|
|
|
* @param ErrorReporterAggregate $reporters |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function __construct(ErrorRenderer $renderer, ErrorReporterAggregate $reporters) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->renderer = $renderer; |
|
49
|
1 |
|
$this->reporters = $reporters; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
* @throws ErrorException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function handleError(int $severity, string $message, string $filename, int $line): bool |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->isSevereEnough($severity)) { |
|
59
|
|
|
// Pass error to the next handler. |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$exception = new ErrorException($message, 0, $severity, $filename, $line); |
|
64
|
|
|
if ($this->canThrowException) { |
|
65
|
|
|
throw $exception; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->handleThrowable($exception); |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function handleShutdown() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->canThrowException = false; |
|
79
|
|
|
$error = error_get_last(); |
|
80
|
|
|
if (!empty($error) && ($error['type'] & self::FATAL)) { |
|
81
|
|
|
// Fatal error is not passed to handleError, we need to pass it manually. |
|
82
|
|
|
$this->handleError($error['type'], $error['message'], $error['file'], $error['line']); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritDoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function handleThrowable(Throwable $e) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->reporters->report($e); |
|
92
|
|
|
$this->renderer->render($e); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Checks if the error is severe enough and must be handled. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $severity |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
private function isSevereEnough(int $severity): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return boolval(error_reporting() & $severity); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: