3 namespace Drupal\big_pipe_test\EventSubscriber;
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;
11 class BigPipeTestSubscriber implements EventSubscriberInterface {
14 * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
18 const CONTENT_TRIGGER_EXCEPTION = 'NOPE!NOPE!NOPE!';
21 * Triggers exception for embedded HTML/AJAX responses with certain content.
23 * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
25 * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
26 * The event to process.
30 public function onRespondTriggerException(FilterResponseEvent $event) {
31 $response = $event->getResponse();
33 if (!$response instanceof AttachmentsInterface) {
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!');
46 * Exposes all BigPipe placeholders (JS and no-JS) via headers for testing.
48 * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
49 * The event to process.
51 public function onRespondSetBigPipeDebugPlaceholderHeaders(FilterResponseEvent $event) {
52 $response = $event->getResponse();
53 if (!$response instanceof HtmlResponse) {
57 $attachments = $response->getAttachments();
59 $response->headers->set('BigPipe-Test-Placeholders', '<none>');
60 $response->headers->set('BigPipe-Test-No-Js-Placeholders', '<none>');
62 if (!empty($attachments['big_pipe_placeholders'])) {
63 $response->headers->set('BigPipe-Test-Placeholders', implode(' ', array_keys($attachments['big_pipe_placeholders'])));
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']))));
74 public static function getSubscribedEvents() {
75 // Run just before \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
76 $events[KernelEvents::RESPONSE][] = ['onRespondSetBigPipeDebugPlaceholderHeaders', -9999];
78 // Run just after \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
79 $events[KernelEvents::RESPONSE][] = ['onRespondTriggerException', -10001];