annotate core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Psr\Http\Message\ResponseInterface;
Chris@0 6
Chris@0 7 use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
Chris@0 8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 9 use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
Chris@0 10 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Response subscriber for handling PSR-7 responses.
Chris@0 14 */
Chris@0 15 class PsrResponseSubscriber implements EventSubscriberInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The httpFoundation factory.
Chris@0 19 *
Chris@0 20 * @var \Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface
Chris@0 21 */
Chris@0 22 protected $httpFoundationFactory;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Constructs a new PathRootsSubscriber instance.
Chris@0 26 *
Chris@0 27 * @param \Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface $http_foundation_factory
Chris@0 28 * The httpFoundation factory.
Chris@0 29 */
Chris@0 30 public function __construct(HttpFoundationFactoryInterface $http_foundation_factory) {
Chris@0 31 $this->httpFoundationFactory = $http_foundation_factory;
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Converts a PSR-7 response to a Symfony response.
Chris@0 36 *
Chris@0 37 * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
Chris@0 38 * The Event to process.
Chris@0 39 */
Chris@0 40 public function onKernelView(GetResponseForControllerResultEvent $event) {
Chris@0 41 $controller_result = $event->getControllerResult();
Chris@0 42
Chris@0 43 if ($controller_result instanceof ResponseInterface) {
Chris@0 44 $event->setResponse($this->httpFoundationFactory->createResponse($controller_result));
Chris@0 45 }
Chris@0 46
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * {@inheritdoc}
Chris@0 51 */
Chris@0 52 public static function getSubscribedEvents() {
Chris@0 53 $events[KernelEvents::VIEW][] = ['onKernelView'];
Chris@0 54 return $events;
Chris@0 55 }
Chris@0 56
Chris@0 57 }