summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-07-27 14:10:36 +0100
committerAlex Pott2015-07-27 14:10:36 +0100
commit3349d702b478715d2b85054f44b93683e28e18e3 (patch)
tree5fd70c182d988bfda09e6870d5ea2d46cc1aeee4
parent9e9c0acdd9d994b860cb979859466a67fa98c77d (diff)
Issue #2497693 by marvin_B8, joshi.rohit100, borisson_, Crell: Add PSR-7 to Symfony Response View listener
-rw-r--r--core/core.services.yml5
-rw-r--r--core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php62
-rw-r--r--core/modules/system/src/Tests/Routing/RouterTest.php9
-rw-r--r--core/modules/system/tests/modules/router_test_directory/router_test.routing.yml7
-rw-r--r--core/modules/system/tests/modules/router_test_directory/src/TestControllers.php6
-rw-r--r--core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php99
6 files changed, 188 insertions, 0 deletions
diff --git a/core/core.services.yml b/core/core.services.yml
index e2939e9..f330708 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -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:
diff --git a/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php
new file mode 100644
index 0000000..eb0818c
--- /dev/null
+++ b/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php
@@ -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;
+ }
+
+}
diff --git a/core/modules/system/src/Tests/Routing/RouterTest.php b/core/modules/system/src/Tests/Routing/RouterTest.php
index b945676..207049f 100644
--- a/core/modules/system/src/Tests/Routing/RouterTest.php
+++ b/core/modules/system/src/Tests/Routing/RouterTest.php
@@ -200,6 +200,15 @@ class RouterTest extends WebTestBase {
}
/**
+ * 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.
*/
public function testUserAccount() {
diff --git a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml
index 5debfc2..36a7017 100644
--- a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml
+++ b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml
@@ -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:
diff --git a/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php b/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php
index 614c9a8..f3ff517 100644
--- a/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php
+++ b/core/modules/system/tests/modules/router_test_directory/src/TestControllers.php
@@ -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.
*
diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php
new file mode 100644
index 0000000..2e3552e
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/EventSubscriber/PsrResponseSubscriberTest.php
@@ -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;
+ }
+
+}