summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/EventSubscriber/PsrResponseSubscriber.php
blob: 85c788aa3099bb0aea904632bd20d11747696e32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

namespace Drupal\Core\EventSubscriber;

use Psr\Http\Message\ResponseInterface;

use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
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\ViewEvent $event
   *   The Event to process.
   */
  public function onKernelView(ViewEvent $event) {
    $controller_result = $event->getControllerResult();

    if ($controller_result instanceof ResponseInterface) {
      $event->setResponse($this->httpFoundationFactory->createResponse($controller_result));
    }

  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events[KernelEvents::VIEW][] = ['onKernelView'];
    return $events;
  }

}