HtmlViewSubscriber.php 2.94 KB
Newer Older
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
58
59
60
61
62
63
64
65
66
67
<?php

/**
 * @file
 * Contains \Drupal\Core\EventSubscriber\HtmlViewSubscriber.
 */

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Page\HtmlFragment;
use Drupal\Core\Page\HtmlPage;
use Drupal\Core\Page\HtmlPageRendererInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;

/**
 * Main subscriber for Html-page responses.
 */
class HtmlViewSubscriber implements EventSubscriberInterface {

  /**
   * The page rendering service.
   *
   * @var \Drupal\Core\Page\HtmlPageRendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new HtmlViewSubscriber.
   *
   * @param \Drupal\Core\Page\HtmlPageRendererInterface $renderer
   *   The page rendering service.
   */
  public function __construct(HtmlPageRendererInterface $renderer) {
    $this->renderer = $renderer;
  }

  /**
   * Converts an HtmlFragment into an HtmlPage.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
   *   The Event to process.
   */
  public function onHtmlFragment(GetResponseForControllerResultEvent $event) {
    $fragment = $event->getControllerResult();
    if ($fragment instanceof HtmlFragment && !$fragment instanceof HtmlPage) {
      $page = $this->renderer->render($fragment);
      $event->setControllerResult($page);
    }
  }

  /**
   * Renders an HtmlPage object to a Response.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
   *   The Event to process.
   */
  public function onHtmlPage(GetResponseForControllerResultEvent $event) {
    $page = $event->getControllerResult();
    if ($page instanceof HtmlPage) {
      // In case renderPage() returns NULL due to an error cast it to a string
      // so as to not cause issues with Response. This also allows renderPage
      // to return an object implementing __toString(), but that is not
      // recommended.
      $response = new Response((string) $this->renderer->renderPage($page), $page->getStatusCode());
68
69
70
71
72
73
74
75
76
77
78
79
      if ($tags = $page->getCacheTags()) {
        $response->headers->set('cache_tags', serialize($tags));
      }
      if ($keys = $page->getCacheKeys()) {
        $response->headers->set('cache_keys', serialize($keys));
      }
      if ($bin = $page->getCacheBin()) {
        $response->headers->set('cache_bin', $bin);
      }
      if ($max_age = $page->getCacheMaxAge()) {
        $response->headers->set('cache_max_age', $max_age);
      }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      $event->setResponse($response);
    }
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::VIEW][] = array('onHtmlFragment', 100);
    $events[KernelEvents::VIEW][] = array('onHtmlPage', 50);

    return $events;
  }

}