8a3e1f2c1931437477ebd7c88df63cfd9e35c207
[yaffs-website] / web / core / modules / big_pipe / tests / modules / big_pipe_test / src / EventSubscriber / BigPipeTestSubscriber.php
1 <?php
2
3 namespace Drupal\big_pipe_test\EventSubscriber;
4
5 use Drupal\Core\Render\AttachmentsInterface;
6 use Drupal\Core\Render\HtmlResponse;
7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11 class BigPipeTestSubscriber implements EventSubscriberInterface {
12
13   /**
14    * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
15    *
16    * @var string
17    */
18   const CONTENT_TRIGGER_EXCEPTION = 'NOPE!NOPE!NOPE!';
19
20   /**
21    * Triggers exception for embedded HTML/AJAX responses with certain content.
22    *
23    * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
24    *
25    * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
26    *   The event to process.
27    *
28    * @throws \Exception
29    */
30   public function onRespondTriggerException(FilterResponseEvent $event) {
31     $response = $event->getResponse();
32
33     if (!$response instanceof AttachmentsInterface) {
34       return;
35     }
36
37     $attachments = $response->getAttachments();
38     if (!isset($attachments['big_pipe_placeholders']) && !isset($attachments['big_pipe_nojs_placeholders'])) {
39       if (strpos($response->getContent(), static::CONTENT_TRIGGER_EXCEPTION) !== FALSE) {
40         throw new \Exception('Oh noes!');
41       }
42     }
43   }
44
45   /**
46    * Exposes all BigPipe placeholders (JS and no-JS) via headers for testing.
47    *
48    * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
49    *   The event to process.
50    */
51   public function onRespondSetBigPipeDebugPlaceholderHeaders(FilterResponseEvent $event) {
52     $response = $event->getResponse();
53     if (!$response instanceof HtmlResponse) {
54       return;
55     }
56
57     $attachments = $response->getAttachments();
58
59     $response->headers->set('BigPipe-Test-Placeholders', '<none>');
60     $response->headers->set('BigPipe-Test-No-Js-Placeholders', '<none>');
61
62     if (!empty($attachments['big_pipe_placeholders'])) {
63       $response->headers->set('BigPipe-Test-Placeholders', implode(' ', array_keys($attachments['big_pipe_placeholders'])));
64     }
65
66     if (!empty($attachments['big_pipe_nojs_placeholders'])) {
67       $response->headers->set('BigPipe-Test-No-Js-Placeholders', implode(' ', array_map('rawurlencode', array_keys($attachments['big_pipe_nojs_placeholders']))));
68     }
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public static function getSubscribedEvents() {
75     // Run just before \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
76     $events[KernelEvents::RESPONSE][] = ['onRespondSetBigPipeDebugPlaceholderHeaders', -9999];
77
78     // Run just after \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
79     $events[KernelEvents::RESPONSE][] = ['onRespondTriggerException', -10001];
80
81     return $events;
82   }
83
84 }