Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75d9d7c

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 statuscode in exception listeners as deprecated. Instead there is now a new methodon the GetResponseForExceptionEvent that allows successful status codes inthe response sent to the client.
1 parent3521105 commit75d9d7c

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
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
publicfunction__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+
publicfunctionsetAllowSuccessfulResponse($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+
publicfunctionallowSuccessfulResponse()
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 ($einstanceof 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 <fabien@symfony.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+
namespaceSymfony\Component\HttpKernel\Tests\Event;
13+
14+
useSymfony\Component\HttpFoundation\Request;
15+
useSymfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
16+
useSymfony\Component\HttpKernel\Tests\TestHttpKernel;
17+
18+
class GetResponseForExceptionEventTestextends \PHPUnit_Framework_TestCase
19+
{
20+
publicfunctiontestAllowSuccessfulResponseIsFalseByDefault()
21+
{
22+
$event =newGetResponseForExceptionEvent(newTestHttpKernel(),newRequest(),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
useSymfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
1717
useSymfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1818
useSymfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
19+
useSymfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1920
useSymfony\Component\HttpKernel\HttpKernel;
2021
useSymfony\Component\HttpKernel\HttpKernelInterface;
2122
useSymfony\Component\HttpKernel\KernelEvents;
@@ -112,7 +113,7 @@ public function testHandleHttpException()
112113
/**
113114
* @dataProvider getStatusCodes
114115
*/
115-
publicfunctiontestHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode,$expectedStatusCode)
116+
publicfunctiontestLegacyHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode,$expectedStatusCode)
116117
{
117118
$dispatcher =newEventDispatcher();
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+
publicfunctiontestHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
144+
{
145+
$dispatcher =newEventDispatcher();
146+
$dispatcher->addListener(KernelEvents::EXCEPTION,function (GetResponseForExceptionEvent$event)use ($expectedStatusCode) {
147+
$event->setAllowSuccessfulResponse(true);
148+
$event->setResponse(newResponse('',$expectedStatusCode));
149+
});
150+
151+
$kernel =$this->getHttpKernel($dispatcher,function () {thrownew \RuntimeException(); });
152+
$response =$kernel->handle(newRequest());
153+
154+
$this->assertEquals($expectedStatusCode,$response->getStatusCode());
155+
}
156+
157+
publicfunctiongetSuccessfulStatusCodes()
158+
{
159+
returnarray(
160+
array(200),
161+
array(204),
162+
array(201),
163+
);
164+
}
165+
139166
publicfunctiontestHandleWhenAListenerReturnsAResponse()
140167
{
141168
$dispatcher =newEventDispatcher();

‎src/Symfony/Component/Security/Http/EntryPoint/FormAuthenticationEntryPoint.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function start(Request $request, AuthenticationException $authException =
5454

5555
$response =$this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
5656
if (200 ===$response->getStatusCode()) {
57-
$response->headers->set('X-Status-Code',401);
57+
$response->setStatusCode(401);
5858
}
5959

6060
return$response;

‎src/Symfony/Component/Security/Http/Tests/EntryPoint/FormAuthenticationEntryPointTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ public function testStartWithUseForward()
6363
$entryPointResponse =$entryPoint->start($request);
6464

6565
$this->assertEquals($response,$entryPointResponse);
66-
$this->assertEquals(401,$entryPointResponse->headers->get('X-Status-Code'));
66+
$this->assertEquals(401,$entryPointResponse->getStatusCode());
6767
}
6868
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp