Skip to content

Commit 845ecf6

Browse files
committed
added a way to populate Response headers in the core.request event
1 parent fb00539 commit 845ecf6

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/HttpKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(ContainerInterface $container, ControllerResolverInt
2323
{
2424
$this->container = $container;
2525
$this->resolver = $controllerResolver;
26+
$this->responseHeaders = array();
2627
}
2728

2829
public function setEventDispatcher(EventDispatcherInterface $dispatcher)

src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ public function replace(array $headers = array())
4646
}
4747
}
4848

49+
/**
50+
* Merges a ResponseHeaderBag instance into the current one.
51+
*
52+
* @param ResponseHeaderBag $headers A ResponseHeaderBag instance
53+
*/
54+
public function merge(ResponseHeaderBag $headers)
55+
{
56+
foreach ($headers->all() as $key => $value) {
57+
$this->set($key, $value, true);
58+
}
59+
60+
foreach ($headers->getCookies() as $cookie) {
61+
$this->setCookie($cookie);
62+
}
63+
}
64+
4965
/**
5066
* {@inheritdoc}
5167
*/

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2021

2122
/**
2223
* HttpKernel notifies events to convert a Request object to a Response one.
@@ -27,6 +28,7 @@ class HttpKernel implements HttpKernelInterface
2728
{
2829
protected $dispatcher;
2930
protected $resolver;
31+
protected $responseHeaders;
3032

3133
/**
3234
* Constructor
@@ -38,6 +40,7 @@ public function __construct(EventDispatcherInterface $dispatcher, ControllerReso
3840
{
3941
$this->dispatcher = $dispatcher;
4042
$this->resolver = $resolver;
43+
$this->responseHeaders = array();
4144
}
4245

4346
/**
@@ -80,8 +83,10 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
8083
*/
8184
protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
8285
{
86+
array_push($this->responseHeaders, $headers = new ResponseHeaderBag());
87+
8388
// request
84-
$event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
89+
$event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request, 'headers' => $headers));
8590
$response = $this->dispatcher->notifyUntil($event);
8691
if ($event->isProcessed()) {
8792
return $this->filterResponse($response, $request, 'A "core.request" listener returned a non response object.', $type);
@@ -135,7 +140,7 @@ protected function filterResponse($response, $request, $message, $type)
135140
throw new \RuntimeException($message);
136141
}
137142

138-
$response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
143+
$response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request, 'headers' => array_pop($this->responseHeaders))), $response);
139144

140145
if (!$response instanceof Response) {
141146
throw new \RuntimeException('A "core.response" listener returned a non response object.');

src/Symfony/Component/HttpKernel/ResponseListener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function __construct($charset)
3838
*/
3939
public function filter(EventInterface $event, Response $response)
4040
{
41-
if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
42-
return $response;
43-
}
41+
$response->headers->merge($event->get('headers'));
4442

4543
if (null === $response->getCharset()) {
4644
$response->setCharset($this->charset);

tests/Symfony/Tests/Component/HttpKernel/ResponseListenerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
use Symfony\Component\EventDispatcher\Event;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
1920
use Symfony\Component\HttpKernel\HttpKernelInterface;
2021

2122
class ResponseListenerTest extends \PHPUnit_Framework_TestCase
2223
{
2324
public function testFilterDoesNothingForSubRequests()
2425
{
25-
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
26+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST, 'request' => Request::create('/'), 'headers' => new ResponseHeaderBag()));
2627
$this->getDispatcher()->filter($event, $response = new Response('foo'));
2728

2829
$this->assertEquals('', $response->headers->get('content-type'));
2930
}
3031

3132
public function testFilterDoesNothingIfContentTypeIsSet()
3233
{
33-
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
34+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/'), 'headers' => new ResponseHeaderBag()));
3435
$response = new Response('foo');
3536
$response->headers->set('Content-Type', 'text/plain');
3637
$this->getDispatcher()->filter($event, $response);
@@ -40,7 +41,7 @@ public function testFilterDoesNothingIfContentTypeIsSet()
4041

4142
public function testFilterDoesNothingIfRequestFormatIsNotDefined()
4243
{
43-
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/')));
44+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/'), 'headers' => new ResponseHeaderBag()));
4445
$this->getDispatcher()->filter($event, $response = new Response('foo'));
4546

4647
$this->assertEquals('', $response->headers->get('content-type'));
@@ -50,7 +51,7 @@ public function testFilterSetContentType()
5051
{
5152
$request = Request::create('/');
5253
$request->setRequestFormat('json');
53-
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request));
54+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request, 'headers' => new ResponseHeaderBag()));
5455
$this->getDispatcher()->filter($event, $response = new Response('foo'));
5556

5657
$this->assertEquals('application/json', $response->headers->get('content-type'));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy