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 FOS\RestBundle\Util\ExceptionValueMap; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ResponseStatusCodeListener implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
private $exceptionValueMap; |
27
|
|
|
private $responseStatusCode; |
28
|
|
|
|
29
|
5 |
|
public function __construct(ExceptionValueMap $exceptionValueMap) |
30
|
|
|
{ |
31
|
5 |
|
$this->exceptionValueMap = $exceptionValueMap; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
public static function getSubscribedEvents(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
KernelEvents::EXCEPTION => 'getResponseStatusCodeFromThrowable', |
38
|
|
|
KernelEvents::RESPONSE => 'setResponseStatusCode', |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ExceptionEvent $event |
44
|
|
|
*/ |
45
|
5 |
|
public function getResponseStatusCodeFromThrowable($event): void |
46
|
|
|
{ |
47
|
5 |
|
if (!$event->isMasterRequest()) { |
48
|
1 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
$request = $event->getRequest(); |
52
|
|
|
|
53
|
5 |
|
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { |
54
|
1 |
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
if (method_exists($event, 'getThrowable')) { |
58
|
4 |
|
$throwable = $event->getThrowable(); |
59
|
|
|
} else { |
60
|
|
|
$throwable = $event->getException(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
$statusCode = $this->exceptionValueMap->resolveThrowable($throwable); |
64
|
|
|
|
65
|
4 |
|
if (is_int($statusCode)) { |
66
|
3 |
|
$this->responseStatusCode = $statusCode; |
67
|
|
|
} |
68
|
4 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ResponseEvent $event |
72
|
|
|
*/ |
73
|
5 |
|
public function setResponseStatusCode($event): void |
74
|
|
|
{ |
75
|
5 |
|
if (!$event->isMasterRequest()) { |
76
|
1 |
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
if (null !== $this->responseStatusCode) { |
80
|
3 |
|
$event->getResponse()->setStatusCode($this->responseStatusCode); |
81
|
|
|
|
82
|
3 |
|
$this->responseStatusCode = null; |
83
|
|
|
} |
84
|
5 |
|
} |
85
|
|
|
} |
86
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.