1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\ExceptionHandler; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Decorator for the Laravel default exception handler. |
11
|
|
|
* |
12
|
|
|
* @author Andrea Marco Sartori |
13
|
|
|
*/ |
14
|
|
|
class Decorator implements ExceptionHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @author Andrea Marco Sartori |
18
|
|
|
* @var Illuminate\Contracts\Debug\ExceptionHandler $handler Laravel default exception handler. |
19
|
|
|
*/ |
20
|
|
|
protected $handler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Andrea Marco Sartori |
24
|
|
|
* @var Cerbero\ExceptionHandler\Repository $handlers Custom exception handlers repository. |
25
|
|
|
*/ |
26
|
|
|
protected $handlers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set the dependencies. |
30
|
|
|
* |
31
|
|
|
* @author Andrea Marco Sartori |
32
|
|
|
* @param Illuminate\Contracts\Debug\ExceptionHandler $handler |
33
|
|
|
* @param Cerbero\ExceptionHandler\Repository $handlers |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
21 |
|
public function __construct(ExceptionHandler $handler, Repository $handlers) |
37
|
|
|
{ |
38
|
21 |
|
$this->handler = $handler; |
|
|
|
|
39
|
|
|
|
40
|
21 |
|
$this->handlers = $handlers; |
|
|
|
|
41
|
21 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Report or log an exception. |
45
|
|
|
* |
46
|
|
|
* @param \Exception $e |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
3 |
|
public function report(Exception $e) |
50
|
|
|
{ |
51
|
3 |
|
foreach ($this->handlers->getReportersFor($e) as $reporter) { |
52
|
3 |
|
if ($report = $reporter($e)) { |
53
|
|
|
return $report; |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
3 |
|
$this->handler->report($e); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register a custom handler to report exceptions. |
62
|
|
|
* |
63
|
|
|
* @author Andrea Marco Sartori |
64
|
|
|
* @param \Closure $reporter |
65
|
|
|
* @return integer |
66
|
|
|
*/ |
67
|
3 |
|
public function reporter(Closure $reporter) |
68
|
|
|
{ |
69
|
3 |
|
return $this->handlers->addReporter($reporter); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Render an exception into an HTTP response. |
74
|
|
|
* |
75
|
|
|
* @param \Illuminate\Http\Request $request |
76
|
|
|
* @param \Exception $e |
77
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
78
|
|
|
*/ |
79
|
3 |
|
public function render($request, Exception $e) |
80
|
|
|
{ |
81
|
3 |
|
foreach ($this->handlers->getRenderersFor($e) as $renderer) { |
82
|
3 |
|
if ($render = $renderer($e, $request)) { |
83
|
|
|
return $render; |
84
|
|
|
} |
85
|
3 |
|
} |
86
|
|
|
|
87
|
3 |
|
return $this->handler->render($request, $e); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Register a custom handler to render exceptions. |
92
|
|
|
* |
93
|
|
|
* @author Andrea Marco Sartori |
94
|
|
|
* @param \Closure $renderer |
95
|
|
|
* @return integer |
96
|
|
|
*/ |
97
|
3 |
|
public function renderer(Closure $renderer) |
98
|
|
|
{ |
99
|
3 |
|
return $this->handlers->addRenderer($renderer); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Render an exception to the console. |
104
|
|
|
* |
105
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
106
|
|
|
* @param \Exception $e |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
3 |
|
public function renderForConsole($output, Exception $e) |
110
|
|
|
{ |
111
|
3 |
|
foreach ($this->handlers->getConsoleRenderersFor($e) as $renderer) { |
112
|
3 |
|
if ($render = $renderer($e, $output)) { |
113
|
|
|
return $render; |
114
|
|
|
} |
115
|
3 |
|
} |
116
|
|
|
|
117
|
3 |
|
$this->handler->renderForConsole($output, $e); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Register a custom handler to render exceptions to the console. |
122
|
|
|
* |
123
|
|
|
* @author Andrea Marco Sartori |
124
|
|
|
* @param \Closure $renderer |
125
|
|
|
* @return integer |
126
|
|
|
*/ |
127
|
3 |
|
public function consoleRenderer(Closure $renderer) |
128
|
|
|
{ |
129
|
3 |
|
return $this->handlers->addConsoleRenderer($renderer); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.