|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\FOSRestBundle; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
17
|
|
|
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as HttpKernelExceptionListener; |
|
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ExceptionListener. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ener-Getick <[email protected]> |
|
25
|
|
|
* |
|
26
|
|
|
* @internal |
|
27
|
|
|
*/ |
|
28
|
|
|
class ExceptionListener extends HttpKernelExceptionListener |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
8 |
|
*/ |
|
33
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
|
34
|
8 |
|
{ |
|
35
|
|
|
$request = $event->getRequest(); |
|
36
|
8 |
|
|
|
37
|
|
|
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
8 |
|
|
|
41
|
8 |
|
parent::onKernelException($event); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
8 |
|
*/ |
|
47
|
|
|
public static function getSubscribedEvents() |
|
48
|
|
|
{ |
|
49
|
8 |
|
return array( |
|
50
|
8 |
|
KernelEvents::EXCEPTION => array('onKernelException', -100), |
|
51
|
8 |
|
); |
|
52
|
8 |
|
} |
|
53
|
8 |
|
|
|
54
|
8 |
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
8 |
|
*/ |
|
57
|
|
|
protected function duplicateRequest(\Exception $exception, Request $request) |
|
58
|
|
|
{ |
|
59
|
|
|
$attributes = array( |
|
60
|
|
|
'_controller' => $this->controller, |
|
61
|
|
|
'exception' => $exception, |
|
62
|
|
|
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, |
|
63
|
|
|
); |
|
64
|
|
|
$request = $request->duplicate(null, null, $attributes); |
|
65
|
|
|
$request->setMethod('GET'); |
|
66
|
|
|
|
|
67
|
|
|
return $request; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|