Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b742e97

Browse files
author
James Halsall
committed
[HttpKernel] Deprecate X-Status-Code for better alternative
This marks the X-Status-Code header method of setting a custom response status code in exception listeners for a better alternative. There is now a new method on the GetResponseForExceptionEvent that allows successful status codes in the response sent to the client.
1 parent 3521105 commit b742e97

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

UPGRADE-4.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ HttpKernel
132132
have your own `ControllerResolverInterface` implementation, you should
133133
inject an `ArgumentResolverInterface` instance.
134134

135+
* The `X-Status-Code` header method of setting a custom status code in the response
136+
when handling exceptions has been removed. There is now a new
137+
`GetResponseForExceptionEvent::setAllowSuccessfulResponse()` method instead, which
138+
will tell the Kernel to use the response code set on the event's response object.
139+
135140
Serializer
136141
----------
137142

src/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ class GetResponseForExceptionEvent extends GetResponseEvent
3636
*/
3737
private $exception;
3838

39+
/**
40+
* @var bool
41+
*/
42+
private $allowSuccessfulResponse;
43+
3944
public function __construct(HttpKernelInterface $kernel, Request $request, $requestType, \Exception $e)
4045
{
4146
parent::__construct($kernel, $request, $requestType);
4247

4348
$this->setException($e);
49+
$this->allowSuccessfulResponse = false;
4450
}
4551

4652
/**
@@ -64,4 +70,24 @@ public function setException(\Exception $exception)
6470
{
6571
$this->exception = $exception;
6672
}
73+
74+
/**
75+
* Mark the event as allowing a successful response code.
76+
*
77+
* @param bool $allowSuccessfulResponse
78+
*/
79+
public function setAllowSuccessfulResponse($allowSuccessfulResponse)
80+
{
81+
$this->allowSuccessfulResponse = $allowSuccessfulResponse;
82+
}
83+
84+
/**
85+
* Returns true if the event allows a successful response code.
86+
*
87+
* @return bool
88+
*/
89+
public function allowSuccessfulResponse()
90+
{
91+
return $this->allowSuccessfulResponse;
92+
}
6793
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ private function handleException(\Exception $e, $request, $type)
242242

243243
// the developer asked for a specific status code
244244
if ($response->headers->has('X-Status-Code')) {
245+
@trigger_error(sprintf('Using the X-Status-Code header is deprecated, use %s::setAllowSuccessfulResponse() instead.', GetResponseForExceptionEvent::class), E_USER_DEPRECATED);
246+
245247
$response->setStatusCode($response->headers->get('X-Status-Code'));
246248

247249
$response->headers->remove('X-Status-Code');
248-
} elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
250+
} elseif (!$event->allowSuccessfulResponse() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
249251
// ensure that we actually have an error response
250252
if ($e instanceof HttpExceptionInterface) {
251253
// keep the HTTP status code and headers
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\HttpKernel\Tests\Event;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16+
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;
17+
18+
class GetResponseForExceptionEventTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testAllowSuccessfulResponseIsFalseByDefault()
21+
{
22+
$event = new GetResponseForExceptionEvent(new TestHttpKernel(), new Request(), 1, new \Exception());
23+
24+
$this->assertFalse($event->allowSuccessfulResponse());
25+
}
26+
}

src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
1717
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1818
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
19+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1920
use Symfony\Component\HttpKernel\HttpKernel;
2021
use Symfony\Component\HttpKernel\HttpKernelInterface;
2122
use Symfony\Component\HttpKernel\KernelEvents;
@@ -112,7 +113,7 @@ public function testHandleHttpException()
112113
/**
113114
* @dataProvider getStatusCodes
114115
*/
115-
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
116+
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCodeViaXStatusCodeHeader($responseStatusCode, $expectedStatusCode)
116117
{
117118
$dispatcher = new EventDispatcher();
118119
$dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
@@ -136,6 +137,32 @@ public function getStatusCodes()
136137
);
137138
}
138139

140+
/**
141+
* @dataProvider getStatusCodes
142+
*/
143+
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
144+
{
145+
$dispatcher = new EventDispatcher();
146+
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) use ($expectedStatusCode) {
147+
$event->setAllowSuccessfulResponse(true);
148+
$event->setResponse(new Response('', $expectedStatusCode));
149+
});
150+
151+
$kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
152+
$response = $kernel->handle(new Request());
153+
154+
$this->assertEquals($expectedStatusCode, $response->getStatusCode());
155+
}
156+
157+
public function getSuccessfulStatusCodes()
158+
{
159+
return array(
160+
array(200),
161+
array(204),
162+
array(201),
163+
);
164+
}
165+
139166
public function testHandleWhenAListenerReturnsAResponse()
140167
{
141168
$dispatcher = new EventDispatcher();

0 commit comments

Comments
 (0)