1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Listener; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
11
|
|
|
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Philip Washington Sorst <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class KernelExceptionListener |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
private $paths; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
private $debug = false; |
27
|
|
|
|
28
|
96 |
|
public function __construct(array $paths) |
29
|
|
|
{ |
30
|
96 |
|
$this->paths = $paths; |
31
|
96 |
|
} |
32
|
|
|
|
33
|
14 |
|
public function onKernelException(GetResponseForExceptionEvent $event) |
34
|
|
|
{ |
35
|
14 |
|
$exception = $event->getException(); |
36
|
14 |
|
$request = $event->getRequest(); |
37
|
14 |
|
if (!$this->isInterceptionPath($request)) { |
38
|
8 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
$data = null; |
42
|
6 |
|
if ($this->debug) { |
43
|
|
|
$data = [ |
44
|
6 |
|
'class' => get_class($exception), |
45
|
6 |
|
'message' => $exception->getMessage(), |
46
|
6 |
|
'trace' => $exception->getTrace() |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
51
|
|
|
|
52
|
6 |
|
if ($exception instanceof InsufficientAuthenticationException) { |
53
|
|
|
$statusCode = Response::HTTP_UNAUTHORIZED; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
if ($exception instanceof BadCredentialsException) { |
57
|
|
|
$statusCode = Response::HTTP_UNAUTHORIZED; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
$response = new JsonResponse($data); |
61
|
|
|
|
62
|
6 |
|
if ($exception instanceof HttpExceptionInterface) { |
63
|
6 |
|
$statusCode = $exception->getStatusCode(); |
64
|
6 |
|
$response->setStatusCode($exception->getStatusCode()); |
65
|
6 |
|
$response->headers->add($exception->getHeaders()); |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$response->setStatusCode($statusCode); |
69
|
|
|
|
70
|
6 |
|
$event->setResponse($response); |
71
|
6 |
|
} |
72
|
|
|
|
73
|
14 |
|
private function isInterceptionPath(?Request $request): bool |
74
|
|
|
{ |
75
|
14 |
|
if (null === $request) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
14 |
|
foreach ($this->paths as $path) { |
80
|
6 |
|
if (0 === strpos($request->getPathInfo(), $path)) { |
81
|
6 |
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function isDebug(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->debug; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param bool $debug |
98
|
|
|
*/ |
99
|
96 |
|
public function setDebug(bool $debug) |
100
|
|
|
{ |
101
|
96 |
|
$this->debug = $debug; |
102
|
96 |
|
} |
103
|
|
|
} |
104
|
|
|
|