You are here

class BigPipeSessionless in Sessionless BigPipe 8

Same name and namespace in other branches
  1. 2.x src/Render/BigPipeSessionless.php \Drupal\big_pipe_sessionless\Render\BigPipeSessionless

The sessionless BigPipe service.

Hierarchy

Expanded class hierarchy of BigPipeSessionless

1 file declares its use of BigPipeSessionless
HtmlResponseBigPipeSessionlessSubscriber.php in src/EventSubscriber/HtmlResponseBigPipeSessionlessSubscriber.php
1 string reference to 'BigPipeSessionless'
big_pipe_sessionless.services.yml in ./big_pipe_sessionless.services.yml
big_pipe_sessionless.services.yml
1 service uses BigPipeSessionless
big_pipe_sessionless in ./big_pipe_sessionless.services.yml
Drupal\big_pipe_sessionless\Render\BigPipeSessionless

File

src/Render/BigPipeSessionless.php, line 13

Namespace

Drupal\big_pipe_sessionless\Render
View source
class BigPipeSessionless extends BigPipe {

  /**
   * The final HTML response.
   *
   * Contains replaced placeholders. Its cacheability metadata and attachments
   * are only for the placeholders.
   *
   * @var \Drupal\Core\Render\HtmlResponse
   *
   * @see \Drupal\big_pipe\Render\BigPipeResponse::getOriginalHtmlResponse()
   * @see ::sendContent()
   */
  protected $finalHtmlResponse;

  /**
   * The PageCache middleware.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $pageCacheMiddleware;

  /**
   * Sets the PageCache middleware.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $page_cache_middleware
   *   The page cache middleware.
   */
  public function setPageCacheMiddleware(HttpKernelInterface $page_cache_middleware) {
    $this->pageCacheMiddleware = $page_cache_middleware;
  }

  /**
   * {@inheritdoc}
   */
  protected function performPreSendTasks() {

    // Nothing to do.
  }

  /**
   * {@inheritdoc}
   */
  protected function performPostSendTasks() {

    // Nothing to do.
  }

  /**
   * {@inheritdoc}
   */
  public function sendContent(BigPipeResponse $response) {
    $this->finalHtmlResponse = new HtmlResponse();
    parent::sendContent($response);
    $this
      ->primePageCache($response);

    // Don't keep around any state.
    $this->finalHtmlResponse = NULL;
  }

  /**
   * {@inheritdoc}
   */
  protected function sendChunk($chunk) {

    // First: send.
    parent::sendChunk($chunk);

    // Then: track every sent chunk.
    // @see ::sendContent()
    if ($chunk instanceof HtmlResponse) {
      $this->finalHtmlResponse
        ->setContent($this->finalHtmlResponse
        ->getContent() . $chunk
        ->getContent());
      $this->finalHtmlResponse
        ->addCacheableDependency($chunk
        ->getCacheableMetadata());
      $this->finalHtmlResponse
        ->addAttachments($chunk
        ->getAttachments());
    }
    else {
      $this->finalHtmlResponse
        ->setContent($this->finalHtmlResponse
        ->getContent() . $chunk);
    }
  }

  /**
   * Primes the Page Cache based on the streamed response.
   *
   * @param \Drupal\big_pipe\Render\BigPipeResponse $response
   *   The BigPipe response that was sent.
   */
  protected function primePageCache(BigPipeResponse $response) {

    // Start with the original HTML response, so we have the appropriate meta-
    // data like headers, HTTP version, and so on.
    $streamed_response = $response
      ->getOriginalHtmlResponse();

    // Override content with final HTML content (with replaced placeholders).
    $streamed_response
      ->setContent($this->finalHtmlResponse
      ->getContent());

    // Add any additional cacheability metadata for rendered placeholders.
    $streamed_response
      ->addCacheableDependency($this->finalHtmlResponse
      ->getCacheableMetadata());

    // Overwrite with final attachments (overwrite, not add, because attachments
    // need to be processed, and once the response is sent, they are processed;
    // if we would not overwrite, then we'd reprocess them).
    // @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface
    $streamed_response
      ->setAttachments($this->finalHtmlResponse
      ->getAttachments());

    // Dispatch the KernelEvents::RESPONSE event, to let those event subscribers
    // do what they need to do. This is f.e. necessary for the cache tags header
    // for debugging purposes and for modules that integrate with reverse
    // proxies that support cache tags.
    $fake_request = $this->requestStack
      ->getMasterRequest()
      ->duplicate();
    $streamed_response = $this
      ->filterResponse($fake_request, HttpKernelInterface::MASTER_REQUEST, $streamed_response);

    // Prime Page Cache.
    // @see \Drupal\big_pipe_sessionless\StackMiddleware\BigPipeSessionlessPageCache
    $this->pageCacheMiddleware
      ->_storeResponse($this->requestStack
      ->getCurrentRequest(), $streamed_response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BigPipe::$configFactory protected property The config factory.
BigPipe::$eventDispatcher protected property The event dispatcher.
BigPipe::$httpKernel protected property The HTTP kernel.
BigPipe::$renderer protected property The renderer.
BigPipe::$requestStack protected property The request stack.
BigPipe::$session protected property The session.
BigPipe::filterEmbeddedResponse protected function Filters the given embedded response, using the cumulative AJAX page state.
BigPipe::filterResponse protected function Filters the given response.
BigPipe::getPlaceholderOrder protected function Gets the BigPipe placeholder order.
BigPipe::renderPlaceholder protected function Renders a placeholder, and just that placeholder.
BigPipe::sendNoJsPlaceholders protected function Sends no-JS BigPipe placeholders' replacements as embedded HTML responses.
BigPipe::sendPlaceholders protected function Sends BigPipe placeholders' replacements as embedded AJAX responses.
BigPipe::sendPostBody protected function Sends </body> and everything after it.
BigPipe::sendPreBody protected function Sends everything until just before </body>.
BigPipe::splitHtmlOnPlaceholders private static function Splits a HTML string into fragments.
BigPipe::START_SIGNAL constant The BigPipe placeholder replacements start signal.
BigPipe::STOP_SIGNAL constant The BigPipe placeholder replacements stop signal.
BigPipe::__construct public function Constructs a new BigPipe class.
BigPipeSessionless::$finalHtmlResponse protected property The final HTML response.
BigPipeSessionless::$pageCacheMiddleware protected property The PageCache middleware.
BigPipeSessionless::performPostSendTasks protected function Performs tasks after sending content (and rendering placeholders). Overrides BigPipe::performPostSendTasks
BigPipeSessionless::performPreSendTasks protected function Performs tasks before sending content (and rendering placeholders). Overrides BigPipe::performPreSendTasks
BigPipeSessionless::primePageCache protected function Primes the Page Cache based on the streamed response.
BigPipeSessionless::sendChunk protected function Sends a chunk. Overrides BigPipe::sendChunk
BigPipeSessionless::sendContent public function Sends an HTML response in chunks using the BigPipe technique. Overrides BigPipe::sendContent
BigPipeSessionless::setPageCacheMiddleware public function Sets the PageCache middleware.