Issue #2497693 by marvin_B8, joshi.rohit100, borisson_, Crell: Add PSR-7 to Symfony Response View listener
parent
9e9c0acdd9
commit
3349d702b4
|
@ -890,6 +890,11 @@ services:
|
|||
class: Drupal\Core\EventSubscriber\RouteMethodSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
psr_response_view_subscriber:
|
||||
class: Drupal\Core\EventSubscriber\PsrResponseSubscriber
|
||||
arguments: ['@psr7.http_foundation_factory']
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
|
||||
# Main content view subscriber plus the renderers it uses.
|
||||
main_content_view_subscriber:
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\EventSubscriber\PsrResponseSubscriber.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\EventSubscriber;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
/**
|
||||
* Response subscriber for handling PSR-7 responses.
|
||||
*/
|
||||
class PsrResponseSubscriber implements EventSubscriberInterface {
|
||||
|
||||
/**
|
||||
* The httpFoundation factory.
|
||||
*
|
||||
* @var \Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface
|
||||
*/
|
||||
protected $httpFoundationFactory;
|
||||
|
||||
/**
|
||||
* Constructs a new PathRootsSubscriber instance.
|
||||
*
|
||||
* @param \Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface $http_foundation_factory
|
||||
* The httpFoundation factory.
|
||||
*/
|
||||
public function __construct(HttpFoundationFactoryInterface $http_foundation_factory) {
|
||||
$this->httpFoundationFactory = $http_foundation_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a PSR-7 response to a Symfony response.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
|
||||
* The Event to process.
|
||||
*/
|
||||
public function onKernelView(GetResponseForControllerResultEvent $event) {
|
||||
$controller_result = $event->getControllerResult();
|
||||
|
||||
if ($controller_result instanceof ResponseInterface) {
|
||||
$event->setResponse($this->httpFoundationFactory->createResponse($controller_result));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
$events[KernelEvents::VIEW][] = ['onKernelView'];
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
|
@ -199,6 +199,15 @@ class RouterTest extends WebTestBase {
|
|||
$this->assertText('Route not matched.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a PSR-7 response works.
|
||||
*/
|
||||
public function testRouterResponsePsr7() {
|
||||
$this->drupalGet('/router_test/test23');
|
||||
$this->assertResponse(200);
|
||||
$this->assertText('test23');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the user account on the DIC.
|
||||
*/
|
||||
|
|
|
@ -141,6 +141,13 @@ router_test.22:
|
|||
requirements:
|
||||
_role: 'anonymous'
|
||||
|
||||
router_test.23:
|
||||
path: '/router_test/test23'
|
||||
defaults:
|
||||
_controller: '\Drupal\router_test\TestControllers::test23'
|
||||
requirements:
|
||||
_access: 'TRUE'
|
||||
|
||||
router_test.hierarchy_parent:
|
||||
path: '/menu-test/parent'
|
||||
defaults:
|
||||
|
|
|
@ -12,6 +12,8 @@ use Drupal\Core\ParamConverter\ParamNotConvertedException;
|
|||
use Drupal\user\UserInterface;
|
||||
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Controller routines for testing the routing system.
|
||||
|
@ -102,6 +104,10 @@ class TestControllers {
|
|||
return new CacheableResponse('test21');
|
||||
}
|
||||
|
||||
public function test23() {
|
||||
return new HtmlResponse('test23');
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Core\EventSubscriber\PsrResponseSubscriberTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Core\EventSubscriber;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use \Drupal\Core\EventSubscriber\PsrResponseSubscriber;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Core\EventSubscriber\PsrResponseSubscriber
|
||||
* @group EventSubscriber
|
||||
*/
|
||||
class PsrResponseSubscriberTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The tested path root subscriber.
|
||||
*
|
||||
* @var \Drupal\Core\EventSubscriber\PsrResponseSubscriber
|
||||
*/
|
||||
protected $psrResponseSubscriber;
|
||||
|
||||
/**
|
||||
* The tested path root subscriber.
|
||||
*
|
||||
* @var \Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $httpFoundationFactoryMock;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$factory = $this->getMock('Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface', [], [], '', NULL);
|
||||
$factory
|
||||
->expects($this->any())
|
||||
->method('createResponse')
|
||||
->willReturn($this->getMock('Symfony\Component\HttpFoundation\Response'));
|
||||
|
||||
$this->httpFoundationFactoryMock = $factory;
|
||||
|
||||
$this->psrResponseSubscriber = new PsrResponseSubscriber($this->httpFoundationFactoryMock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests altering and finished event.
|
||||
*
|
||||
* @covers ::onKernelView
|
||||
*/
|
||||
public function testConvertsControllerResult() {
|
||||
$event = $this->createEventMock($this->getMock('Psr\Http\Message\ResponseInterface'));
|
||||
$event
|
||||
->expects($this->once())
|
||||
->method('setResponse')
|
||||
->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Response'));
|
||||
$this->psrResponseSubscriber->onKernelView($event);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests altering and finished event.
|
||||
*
|
||||
* @covers ::onKernelView
|
||||
*/
|
||||
public function testDoesNotConvertControllerResult() {
|
||||
$event = $this->createEventMock([]);
|
||||
$event
|
||||
->expects($this->never())
|
||||
->method('setResponse');
|
||||
$this->psrResponseSubscriber->onKernelView($event);
|
||||
$event = $this->createEventMock(NULL);
|
||||
$event
|
||||
->expects($this->never())
|
||||
->method('setResponse');
|
||||
$this->psrResponseSubscriber->onKernelView($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up an alias event that return $controllerResult.
|
||||
*
|
||||
* @param mixed $controller_result
|
||||
* The return Object.
|
||||
*
|
||||
* @return \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent|\PHPUnit_Framework_MockObject_MockObject
|
||||
* A mock object to test.
|
||||
*/
|
||||
protected function createEventMock($controller_result) {
|
||||
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent', [], [], '', NULL);
|
||||
$event
|
||||
->expects($this->once())
|
||||
->method('getControllerResult')
|
||||
->willReturn($controller_result);
|
||||
return $event;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue