Chris@0: — this contains BigPipe Chris@0: * placeholders for the personalized parts of the page. Hence this sends the Chris@0: * non-personalized parts of the page. Let's call it The Skeleton. Chris@0: * 2. N chunks: a '; Chris@0: Chris@0: /** Chris@0: * The BigPipe placeholder replacements stop signal. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: const STOP_SIGNAL = ''; Chris@0: Chris@0: /** Chris@0: * The renderer. Chris@0: * Chris@0: * @var \Drupal\Core\Render\RendererInterface Chris@0: */ Chris@0: protected $renderer; Chris@0: Chris@0: /** Chris@0: * The session. Chris@0: * Chris@0: * @var \Symfony\Component\HttpFoundation\Session\SessionInterface Chris@0: */ Chris@0: protected $session; Chris@0: Chris@0: /** Chris@0: * The request stack. Chris@0: * Chris@0: * @var \Symfony\Component\HttpFoundation\RequestStack Chris@0: */ Chris@0: protected $requestStack; Chris@0: Chris@0: /** Chris@0: * The HTTP kernel. Chris@0: * Chris@0: * @var \Symfony\Component\HttpKernel\HttpKernelInterface Chris@0: */ Chris@0: protected $httpKernel; Chris@0: Chris@0: /** Chris@0: * The event dispatcher. Chris@0: * Chris@0: * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface Chris@0: */ Chris@0: protected $eventDispatcher; Chris@0: Chris@0: /** Chris@0: * The config factory. Chris@0: * Chris@0: * @var \Drupal\Core\Config\ConfigFactoryInterface Chris@0: */ Chris@0: protected $configFactory; Chris@0: Chris@0: /** Chris@0: * Constructs a new BigPipe class. Chris@0: * Chris@0: * @param \Drupal\Core\Render\RendererInterface $renderer Chris@0: * The renderer. Chris@0: * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session Chris@0: * The session. Chris@0: * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack Chris@0: * The request stack. Chris@0: * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel Chris@0: * The HTTP kernel. Chris@0: * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher Chris@0: * The event dispatcher. Chris@0: * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory Chris@0: * The config factory. Chris@0: */ Chris@0: public function __construct(RendererInterface $renderer, SessionInterface $session, RequestStack $request_stack, HttpKernelInterface $http_kernel, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) { Chris@0: $this->renderer = $renderer; Chris@0: $this->session = $session; Chris@0: $this->requestStack = $request_stack; Chris@0: $this->httpKernel = $http_kernel; Chris@0: $this->eventDispatcher = $event_dispatcher; Chris@0: $this->configFactory = $config_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs tasks before sending content (and rendering placeholders). Chris@0: */ Chris@0: protected function performPreSendTasks() { Chris@0: // The content in the placeholders may depend on the session, and by the Chris@0: // time the response is sent (see index.php), the session is already Chris@0: // closed. Reopen it for the duration that we are rendering placeholders. Chris@0: $this->session->start(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs tasks after sending content (and rendering placeholders). Chris@0: */ Chris@0: protected function performPostSendTasks() { Chris@0: // Close the session again. Chris@0: $this->session->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends a chunk. Chris@0: * Chris@0: * @param string|\Drupal\Core\Render\HtmlResponse $chunk Chris@0: * The string or response to append. String if there's no cacheability Chris@0: * metadata or attachments to merge. Chris@0: */ Chris@0: protected function sendChunk($chunk) { Chris@0: assert(is_string($chunk) || $chunk instanceof HtmlResponse); Chris@0: if ($chunk instanceof HtmlResponse) { Chris@0: print $chunk->getContent(); Chris@0: } Chris@0: else { Chris@0: print $chunk; Chris@0: } Chris@0: flush(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends an HTML response in chunks using the BigPipe technique. Chris@0: * Chris@0: * @param \Drupal\big_pipe\Render\BigPipeResponse $response Chris@0: * The BigPipe response to send. Chris@0: * Chris@0: * @internal Chris@0: * This method should only be invoked by Chris@0: * \Drupal\big_pipe\Render\BigPipeResponse, which is itself an internal Chris@0: * class. Chris@0: */ Chris@0: public function sendContent(BigPipeResponse $response) { Chris@0: $content = $response->getContent(); Chris@0: $attachments = $response->getAttachments(); Chris@0: Chris@0: // First, gather the BigPipe placeholders that must be replaced. Chris@0: $placeholders = isset($attachments['big_pipe_placeholders']) ? $attachments['big_pipe_placeholders'] : []; Chris@0: $nojs_placeholders = isset($attachments['big_pipe_nojs_placeholders']) ? $attachments['big_pipe_nojs_placeholders'] : []; Chris@0: Chris@0: // BigPipe sends responses using "Transfer-Encoding: chunked". To avoid Chris@0: // sending already-sent assets, it is necessary to track cumulative assets Chris@0: // from all previously rendered/sent chunks. Chris@0: // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41 Chris@0: $cumulative_assets = AttachedAssets::createFromRenderArray(['#attached' => $attachments]); Chris@0: $cumulative_assets->setAlreadyLoadedLibraries($attachments['library']); Chris@0: Chris@0: $this->performPreSendTasks(); Chris@0: Chris@0: // Find the closing tag and get the strings before and after. But be Chris@0: // careful to use the latest occurrence of the string "", to ensure Chris@0: // that strings in inline JavaScript or CDATA sections aren't used instead. Chris@0: $parts = explode('', $content); Chris@0: $post_body = array_pop($parts); Chris@0: $pre_body = implode('', $parts); Chris@0: Chris@0: $this->sendPreBody($pre_body, $nojs_placeholders, $cumulative_assets); Chris@0: $this->sendPlaceholders($placeholders, $this->getPlaceholderOrder($pre_body, $placeholders), $cumulative_assets); Chris@0: $this->sendPostBody($post_body); Chris@0: Chris@0: $this->performPostSendTasks(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends everything until just before . Chris@0: * Chris@0: * @param string $pre_body Chris@0: * The HTML response's content until the closing tag. Chris@0: * @param array $no_js_placeholders Chris@0: * The no-JS BigPipe placeholders. Chris@0: * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets Chris@0: * The cumulative assets sent so far; to be updated while rendering no-JS Chris@0: * BigPipe placeholders. Chris@0: */ Chris@0: protected function sendPreBody($pre_body, array $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) { Chris@0: // If there are no no-JS BigPipe placeholders, we can send the pre- Chris@0: // part of the page immediately. Chris@0: if (empty($no_js_placeholders)) { Chris@0: $this->sendChunk($pre_body); Chris@0: return; Chris@0: } Chris@0: Chris@0: // Extract the scripts_bottom markup: the no-JS BigPipe placeholders that we Chris@0: // will render may attach additional asset libraries, and if so, it will be Chris@0: // necessary to re-render scripts_bottom. Chris@0: list($pre_scripts_bottom, $scripts_bottom, $post_scripts_bottom) = explode('', $pre_body, 3); Chris@0: $cumulative_assets_initial = clone $cumulative_assets; Chris@0: Chris@0: $this->sendNoJsPlaceholders($pre_scripts_bottom . $post_scripts_bottom, $no_js_placeholders, $cumulative_assets); Chris@0: Chris@0: // If additional asset libraries or drupalSettings were attached by any of Chris@0: // the placeholders, then we need to re-render scripts_bottom. Chris@0: if ($cumulative_assets_initial != $cumulative_assets) { Chris@0: // Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent Chris@0: // before the HTML they're associated with. Chris@0: // @see \Drupal\Core\Render\HtmlResponseSubscriber Chris@0: // @see template_preprocess_html() Chris@0: $js_bottom_placeholder = ''; Chris@0: Chris@0: $html_response = new HtmlResponse(); Chris@0: $html_response->setContent([ Chris@0: '#markup' => BigPipeMarkup::create($js_bottom_placeholder), Chris@0: '#attached' => [ Chris@0: 'drupalSettings' => $cumulative_assets->getSettings(), Chris@0: 'library' => $cumulative_assets->getAlreadyLoadedLibraries(), Chris@0: 'html_response_attachment_placeholders' => [ Chris@0: 'scripts_bottom' => $js_bottom_placeholder, Chris@0: ], Chris@0: ], Chris@0: ]); Chris@0: $html_response->getCacheableMetadata()->setCacheMaxAge(0); Chris@0: Chris@0: // Push a fake request with the asset libraries loaded so far and dispatch Chris@0: // KernelEvents::RESPONSE event. This results in the attachments for the Chris@0: // HTML response being processed by HtmlResponseAttachmentsProcessor and Chris@0: // hence the HTML to load the bottom JavaScript can be rendered. Chris@0: $fake_request = $this->requestStack->getMasterRequest()->duplicate(); Chris@0: $html_response = $this->filterEmbeddedResponse($fake_request, $html_response); Chris@0: $scripts_bottom = $html_response->getContent(); Chris@0: } Chris@0: Chris@0: $this->sendChunk($scripts_bottom); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends no-JS BigPipe placeholders' replacements as embedded HTML responses. Chris@0: * Chris@0: * @param string $html Chris@0: * HTML markup. Chris@0: * @param array $no_js_placeholders Chris@0: * Associative array; the no-JS BigPipe placeholders. Keys are the BigPipe Chris@0: * selectors. Chris@0: * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets Chris@0: * The cumulative assets sent so far; to be updated while rendering no-JS Chris@0: * BigPipe placeholders. Chris@0: * Chris@0: * @throws \Exception Chris@0: * If an exception is thrown during the rendering of a placeholder, it is Chris@0: * caught to allow the other placeholders to still be replaced. But when Chris@0: * error logging is configured to be verbose, the exception is rethrown to Chris@0: * simplify debugging. Chris@0: */ Chris@0: protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) { Chris@0: // Split the HTML on every no-JS placeholder string. Chris@0: $prepare_for_preg_split = function ($placeholder_string) { Chris@0: return '(' . preg_quote($placeholder_string, '/') . ')'; Chris@0: }; Chris@0: $preg_placeholder_strings = array_map($prepare_for_preg_split, array_keys($no_js_placeholders)); Chris@0: $fragments = preg_split('/' . implode('|', $preg_placeholder_strings) . '/', $html, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); Chris@0: Chris@0: // Determine how many occurrences there are of each no-JS placeholder. Chris@0: $placeholder_occurrences = array_count_values(array_intersect($fragments, array_keys($no_js_placeholders))); Chris@0: Chris@0: // Set up a variable to store the content of placeholders that have multiple Chris@0: // occurrences. Chris@0: $multi_occurrence_placeholders_content = []; Chris@0: Chris@0: foreach ($fragments as $fragment) { Chris@0: // If the fragment isn't one of the no-JS placeholders, it is the HTML in Chris@0: // between placeholders and it must be printed & flushed immediately. The Chris@0: // rest of the logic in the loop handles the placeholders. Chris@0: if (!isset($no_js_placeholders[$fragment])) { Chris@0: $this->sendChunk($fragment); Chris@0: continue; Chris@0: } Chris@0: Chris@0: // If there are multiple occurrences of this particular placeholder, and Chris@0: // this is the second occurrence, we can skip all calculations and just Chris@0: // send the same content. Chris@0: if ($placeholder_occurrences[$fragment] > 1 && isset($multi_occurrence_placeholders_content[$fragment])) { Chris@0: $this->sendChunk($multi_occurrence_placeholders_content[$fragment]); Chris@0: continue; Chris@0: } Chris@0: Chris@0: $placeholder = $fragment; Chris@14: assert(isset($no_js_placeholders[$placeholder])); Chris@0: $token = Crypt::randomBytesBase64(55); Chris@0: Chris@0: // Render the placeholder, but include the cumulative settings assets, so Chris@0: // we can calculate the overall settings for the entire page. Chris@0: $placeholder_plus_cumulative_settings = [ Chris@0: 'placeholder' => $no_js_placeholders[$placeholder], Chris@0: 'cumulative_settings_' . $token => [ Chris@0: '#attached' => [ Chris@0: 'drupalSettings' => $cumulative_assets->getSettings(), Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: try { Chris@0: $elements = $this->renderPlaceholder($placeholder, $placeholder_plus_cumulative_settings); Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) { Chris@0: throw $e; Chris@0: } Chris@0: else { Chris@0: trigger_error($e, E_USER_ERROR); Chris@0: continue; Chris@0: } Chris@0: } Chris@0: Chris@0: // Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent Chris@0: // before the HTML they're associated with. In other words: ensure the Chris@0: // critical assets for this placeholder's markup are loaded first. Chris@0: // @see \Drupal\Core\Render\HtmlResponseSubscriber Chris@0: // @see template_preprocess_html() Chris@0: $css_placeholder = ''; Chris@0: $js_placeholder = ''; Chris@0: $elements['#markup'] = BigPipeMarkup::create($css_placeholder . $js_placeholder . (string) $elements['#markup']); Chris@0: $elements['#attached']['html_response_attachment_placeholders']['styles'] = $css_placeholder; Chris@0: $elements['#attached']['html_response_attachment_placeholders']['scripts'] = $js_placeholder; Chris@0: Chris@0: $html_response = new HtmlResponse(); Chris@0: $html_response->setContent($elements); Chris@0: $html_response->getCacheableMetadata()->setCacheMaxAge(0); Chris@0: Chris@0: // Push a fake request with the asset libraries loaded so far and dispatch Chris@0: // KernelEvents::RESPONSE event. This results in the attachments for the Chris@0: // HTML response being processed by HtmlResponseAttachmentsProcessor and Chris@0: // hence: Chris@0: // - the HTML to load the CSS can be rendered. Chris@0: // - the HTML to load the JS (at the top) can be rendered. Chris@0: $fake_request = $this->requestStack->getMasterRequest()->duplicate(); Chris@0: $fake_request->request->set('ajax_page_state', ['libraries' => implode(',', $cumulative_assets->getAlreadyLoadedLibraries())]); Chris@0: try { Chris@0: $html_response = $this->filterEmbeddedResponse($fake_request, $html_response); Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) { Chris@0: throw $e; Chris@0: } Chris@0: else { Chris@0: trigger_error($e, E_USER_ERROR); Chris@0: continue; Chris@0: } Chris@0: } Chris@0: Chris@0: // Send this embedded HTML response. Chris@0: $this->sendChunk($html_response); Chris@0: Chris@0: // Another placeholder was rendered and sent, track the set of asset Chris@0: // libraries sent so far. Any new settings also need to be tracked, so Chris@0: // they can be sent in ::sendPreBody(). Chris@0: $cumulative_assets->setAlreadyLoadedLibraries(array_merge($cumulative_assets->getAlreadyLoadedLibraries(), $html_response->getAttachments()['library'])); Chris@0: $cumulative_assets->setSettings($html_response->getAttachments()['drupalSettings']); Chris@0: Chris@0: // If there are multiple occurrences of this particular placeholder, track Chris@0: // the content that was sent, so we can skip all calculations for the next Chris@0: // occurrence. Chris@0: if ($placeholder_occurrences[$fragment] > 1) { Chris@0: $multi_occurrence_placeholders_content[$fragment] = $html_response->getContent(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends BigPipe placeholders' replacements as embedded AJAX responses. Chris@0: * Chris@0: * @param array $placeholders Chris@0: * Associative array; the BigPipe placeholders. Keys are the BigPipe Chris@0: * placeholder IDs. Chris@0: * @param array $placeholder_order Chris@0: * Indexed array; the order in which the BigPipe placeholders must be sent. Chris@0: * Values are the BigPipe placeholder IDs. (These values correspond to keys Chris@0: * in $placeholders.) Chris@0: * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets Chris@0: * The cumulative assets sent so far; to be updated while rendering BigPipe Chris@0: * placeholders. Chris@0: * Chris@0: * @throws \Exception Chris@0: * If an exception is thrown during the rendering of a placeholder, it is Chris@0: * caught to allow the other placeholders to still be replaced. But when Chris@0: * error logging is configured to be verbose, the exception is rethrown to Chris@0: * simplify debugging. Chris@0: */ Chris@0: protected function sendPlaceholders(array $placeholders, array $placeholder_order, AttachedAssetsInterface $cumulative_assets) { Chris@0: // Return early if there are no BigPipe placeholders to send. Chris@0: if (empty($placeholders)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Send the start signal. Chris@0: $this->sendChunk("\n" . static::START_SIGNAL . "\n"); Chris@0: Chris@0: // A BigPipe response consists of a HTML response plus multiple embedded Chris@0: // AJAX responses. To process the attachments of those AJAX responses, we Chris@0: // need a fake request that is identical to the master request, but with Chris@0: // one change: it must have the right Accept header, otherwise the work- Chris@0: // around for a bug in IE9 will cause not JSON, but