|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Routers; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\Mvc\Splash\Controllers\Http500HandlerInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Mouf\Mvc\Splash\Services\SplashUtils; |
|
11
|
|
|
use Zend\Stratigility\ErrorMiddlewareInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This router transforms exceptions into HTTP 500 pages, based on the configured error controller. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Kevin Nguyen |
|
17
|
|
|
* @author David Négrier |
|
18
|
|
|
*/ |
|
19
|
|
|
class ExceptionRouter implements ErrorMiddlewareInterface |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The logger. |
|
23
|
|
|
* |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $log; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The controller that will display 500 errors. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Http500HandlerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $errorController; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Important |
|
37
|
|
|
* |
|
38
|
|
|
* @param HttpKernelInterface $router The default router (the router we will catch exceptions from). |
|
|
|
|
|
|
39
|
|
|
* @param LoggerInterface $log Logger to log errors. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Http500HandlerInterface $errorController, LoggerInterface $log = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->errorController = $errorController; |
|
44
|
|
|
$this->log = $log; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Actually handle the exception. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Exception $e |
|
51
|
|
|
* |
|
52
|
|
|
* @return ResponseInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private function handleException(\Exception $e, Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->log != null) { |
|
57
|
|
|
$this->log->error('Exception thrown inside a controller.', array( |
|
58
|
|
|
'exception' => $e, |
|
59
|
|
|
)); |
|
60
|
|
|
} else { |
|
61
|
|
|
// If no logger is set, let's log in PHP error_log |
|
62
|
|
|
error_log($e->getMessage().' - '.$e->getTraceAsString()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$response = SplashUtils::buildControllerResponse( |
|
66
|
|
|
function () use ($e, $request) { |
|
67
|
|
|
return $this->errorController->serverError($e, $request); |
|
68
|
|
|
} |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $response; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Process an incoming error, along with associated request and response. |
|
76
|
|
|
* |
|
77
|
|
|
* Accepts an error, a server-side request, and a response instance, and |
|
78
|
|
|
* does something with them; if further processing can be done, it can |
|
79
|
|
|
* delegate to `$out`. |
|
80
|
|
|
* |
|
81
|
|
|
* @see MiddlewareInterface |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $error |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @param Response $response |
|
86
|
|
|
* @param null|callable $out |
|
87
|
|
|
* |
|
88
|
|
|
* @return null|Response |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __invoke($error, Request $request, Response $response, callable $out = null) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->handleException($error, $request); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.