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