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