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 bag. |
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 |
|
$reporter($e); |
53
|
3 |
|
} |
54
|
|
|
|
55
|
3 |
|
$this->handler->report($e); |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Register a custom handler to report exceptions. |
60
|
|
|
* |
61
|
|
|
* @author Andrea Marco Sartori |
62
|
|
|
* @param \Closure $reporter |
63
|
|
|
* @return integer |
64
|
|
|
*/ |
65
|
3 |
|
public function reporter(Closure $reporter) |
66
|
|
|
{ |
67
|
3 |
|
return $this->handlers->addReporter($reporter); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Render an exception into an HTTP response. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Http\Request $request |
74
|
|
|
* @param \Exception $e |
75
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
76
|
|
|
*/ |
77
|
3 |
|
public function render($request, Exception $e) |
78
|
|
|
{ |
79
|
3 |
|
foreach ($this->handlers->getRenderersFor($e) as $renderer) { |
80
|
3 |
|
if ($render = $renderer($e, $request)) { |
81
|
|
|
return $render; |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
3 |
|
return $this->handler->render($request, $e); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Register a custom handler to render exceptions. |
90
|
|
|
* |
91
|
|
|
* @author Andrea Marco Sartori |
92
|
|
|
* @param \Closure $renderer |
93
|
|
|
* @return integer |
94
|
|
|
*/ |
95
|
3 |
|
public function renderer(Closure $renderer) |
96
|
|
|
{ |
97
|
3 |
|
return $this->handlers->addRenderer($renderer); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Render an exception to the console. |
102
|
|
|
* |
103
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
104
|
|
|
* @param \Exception $e |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
3 |
|
public function renderForConsole($output, Exception $e) |
108
|
|
|
{ |
109
|
3 |
|
foreach ($this->handlers->getConsoleRenderersFor($e) as $renderer) { |
110
|
3 |
|
$renderer($e, $output); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
3 |
|
$this->handler->renderForConsole($output, $e); |
114
|
3 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Register a custom handler to render exceptions to the console. |
118
|
|
|
* |
119
|
|
|
* @author Andrea Marco Sartori |
120
|
|
|
* @param \Closure $renderer |
121
|
|
|
* @return integer |
122
|
|
|
*/ |
123
|
3 |
|
public function consoleRenderer(Closure $renderer) |
124
|
|
|
{ |
125
|
3 |
|
return $this->handlers->addConsoleRenderer($renderer); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.