1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\ExceptionHandler; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use ReflectionFunction; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The handlers repository. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class HandlersRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The custom handlers reporting exceptions. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $reporters = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The custom handlers rendering exceptions. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $renderers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The custom handlers rendering exceptions in console. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $consoleRenderers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register a custom handler to report exceptions |
37
|
|
|
* |
38
|
|
|
* @param callable $reporter |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
12 |
|
public function addReporter(callable $reporter) |
42
|
|
|
{ |
43
|
12 |
|
return array_unshift($this->reporters, $reporter); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Register a custom handler to render exceptions |
48
|
|
|
* |
49
|
|
|
* @param callable $renderer |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
3 |
|
public function addRenderer(callable $renderer) |
53
|
|
|
{ |
54
|
3 |
|
return array_unshift($this->renderers, $renderer); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register a custom handler to render exceptions in console |
59
|
|
|
* |
60
|
|
|
* @param callable $renderer |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
3 |
|
public function addConsoleRenderer(callable $renderer) |
64
|
|
|
{ |
65
|
3 |
|
return array_unshift($this->consoleRenderers, $renderer); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve all reporters handling the given exception |
70
|
|
|
* |
71
|
|
|
* @param \Throwable $e |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
15 |
|
public function getReportersByException(Throwable $e) |
75
|
|
|
{ |
76
|
5 |
|
return array_filter($this->reporters, function (callable $handler) use ($e) { |
77
|
12 |
|
return $this->handlesException($handler, $e); |
78
|
15 |
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Determine whether the given handler can handle the provided exception |
83
|
|
|
* |
84
|
|
|
* @param callable $handler |
85
|
|
|
* @param \Throwable $e |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
18 |
|
protected function handlesException(callable $handler, Throwable $e) |
89
|
|
|
{ |
90
|
18 |
|
$reflection = new ReflectionFunction($handler); |
91
|
|
|
|
92
|
18 |
|
if (!$params = $reflection->getParameters()) { |
93
|
3 |
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
15 |
|
return $params[0]->getClass() ? $params[0]->getClass()->isInstance($e) : true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve all renderers handling the given exception |
101
|
|
|
* |
102
|
|
|
* @param \Throwable $e |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
6 |
|
public function getRenderersByException(Throwable $e) |
106
|
|
|
{ |
107
|
2 |
|
return array_filter($this->renderers, function (callable $handler) use ($e) { |
108
|
3 |
|
return $this->handlesException($handler, $e); |
109
|
6 |
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve all console renderers handling the given exception |
114
|
|
|
* |
115
|
|
|
* @param \Throwable $e |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
6 |
|
public function getConsoleRenderersByException(Throwable $e) |
119
|
|
|
{ |
120
|
2 |
|
return array_filter($this->consoleRenderers, function (callable $handler) use ($e) { |
121
|
3 |
|
return $this->handlesException($handler, $e); |
122
|
6 |
|
}); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|