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