Version 1
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / HtmlResponsePlaceholderStrategySubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Drupal\Core\Render\HtmlResponse;
6 use Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface;
7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11 /**
12  * HTML response subscriber to allow for different placeholder strategies.
13  *
14  * This allows core and contrib to coordinate how to render placeholders;
15  * e.g. an EsiRenderStrategy could replace the placeholders with ESI tags,
16  * while e.g. a BigPipeRenderStrategy could store the placeholders in a
17  * BigPipe service and render them after the main content has been sent to
18  * the client.
19  */
20 class HtmlResponsePlaceholderStrategySubscriber implements EventSubscriberInterface {
21
22   /**
23    * The placeholder strategy to use.
24    *
25    * @var \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface
26    */
27   protected $placeholderStrategy;
28
29   /**
30    * Constructs a HtmlResponsePlaceholderStrategySubscriber object.
31    *
32    * @param \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface $placeholder_strategy
33    *   The placeholder strategy to use.
34    */
35   public function __construct(PlaceholderStrategyInterface $placeholder_strategy) {
36     $this->placeholderStrategy = $placeholder_strategy;
37   }
38
39   /**
40    * Processes placeholders for HTML responses.
41    *
42    * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
43    *   The event to process.
44    */
45   public function onRespond(FilterResponseEvent $event) {
46     $response = $event->getResponse();
47     if (!$response instanceof HtmlResponse) {
48       return;
49     }
50
51     $attachments = $response->getAttachments();
52     if (empty($attachments['placeholders'])) {
53       return;
54     }
55
56     $attachments['placeholders'] = $this->placeholderStrategy->processPlaceholders($attachments['placeholders']);
57
58     $response->setAttachments($attachments);
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function getSubscribedEvents() {
65     // Run shortly before HtmlResponseSubscriber.
66     $events[KernelEvents::RESPONSE][] = ['onRespond', 5];
67     return $events;
68   }
69
70 }