Chris@0: bigPipe = $big_pipe; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds markers to the response necessary for the BigPipe render strategy. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onRespondEarly(FilterResponseEvent $event) { Chris@0: $response = $event->getResponse(); Chris@0: if (!$response instanceof HtmlResponse) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Wrap the scripts_bottom placeholder with a marker before and after, Chris@0: // because \Drupal\big_pipe\Render\BigPipe needs to be able to extract that Chris@0: // markup if there are no-JS BigPipe placeholders. Chris@0: // @see \Drupal\big_pipe\Render\BigPipe::sendPreBody() Chris@0: $attachments = $response->getAttachments(); Chris@0: if (isset($attachments['html_response_attachment_placeholders']['scripts_bottom'])) { Chris@0: $scripts_bottom_placeholder = $attachments['html_response_attachment_placeholders']['scripts_bottom']; Chris@0: $content = $response->getContent(); Chris@0: $content = str_replace($scripts_bottom_placeholder, '' . $scripts_bottom_placeholder . '', $content); Chris@0: $response->setContent($content); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Transforms a HtmlResponse to a BigPipeResponse. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onRespond(FilterResponseEvent $event) { Chris@0: $response = $event->getResponse(); Chris@0: if (!$response instanceof HtmlResponse) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $attachments = $response->getAttachments(); Chris@0: Chris@0: // If there are no no-JS BigPipe placeholders, unwrap the scripts_bottom Chris@0: // markup. Chris@0: // @see onRespondEarly() Chris@0: // @see \Drupal\big_pipe\Render\BigPipe::sendPreBody() Chris@0: if (empty($attachments['big_pipe_nojs_placeholders'])) { Chris@0: $content = $response->getContent(); Chris@0: $content = str_replace('', '', $content); Chris@0: $response->setContent($content); Chris@0: } Chris@0: Chris@0: // If there are neither BigPipe placeholders nor no-JS BigPipe placeholders, Chris@0: // there isn't anything dynamic in this response, and we can return early: Chris@0: // there is no point in sending this response using BigPipe. Chris@0: if (empty($attachments['big_pipe_placeholders']) && empty($attachments['big_pipe_nojs_placeholders'])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $big_pipe_response = new BigPipeResponse($response); Chris@0: $big_pipe_response->setBigPipeService($this->getBigPipeService($event)); Chris@0: $event->setResponse($big_pipe_response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the BigPipe service to use to send the current response. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event Chris@0: * A response event. Chris@0: * Chris@0: * @return \Drupal\big_pipe\Render\BigPipe Chris@0: * The BigPipe service. Chris@0: */ Chris@0: protected function getBigPipeService(FilterResponseEvent $event) { Chris@0: return $this->bigPipe; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: // Run after HtmlResponsePlaceholderStrategySubscriber (priority 5), i.e. Chris@0: // after BigPipeStrategy has been applied, but before normal (priority 0) Chris@0: // response subscribers have been applied, because by then it'll be too late Chris@0: // to transform it into a BigPipeResponse. Chris@0: $events[KernelEvents::RESPONSE][] = ['onRespondEarly', 3]; Chris@0: Chris@0: // Run as the last possible subscriber. Chris@0: $events[KernelEvents::RESPONSE][] = ['onRespond', -10000]; Chris@0: Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }