You are here

class BigPipeResponseAttachmentsProcessor in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php \Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor
  2. 10 core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php \Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor

Processes attachments of HTML responses with BigPipe enabled.

Hierarchy

Expanded class hierarchy of BigPipeResponseAttachmentsProcessor

See also

\Drupal\Core\Render\HtmlResponseAttachmentsProcessor

\Drupal\big_pipe\Render\BigPipe

1 file declares its use of BigPipeResponseAttachmentsProcessor
BigPipeResponseAttachmentsProcessorTest.php in core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php
1 string reference to 'BigPipeResponseAttachmentsProcessor'
big_pipe.services.yml in core/modules/big_pipe/big_pipe.services.yml
core/modules/big_pipe/big_pipe.services.yml
1 service uses BigPipeResponseAttachmentsProcessor
html_response.attachments_processor.big_pipe in core/modules/big_pipe/big_pipe.services.yml
\Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor

File

core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php, line 23

Namespace

Drupal\big_pipe\Render
View source
class BigPipeResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {

  /**
   * The HTML response attachments processor service.
   *
   * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
   */
  protected $htmlResponseAttachmentsProcessor;

  /**
   * Constructs a BigPipeResponseAttachmentsProcessor object.
   *
   * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
   *   The HTML response attachments processor service.
   * @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
   *   An asset resolver.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
   *   The CSS asset collection renderer.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $js_collection_renderer
   *   The JS asset collection renderer.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */
  public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor, AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler) {
    $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
    parent::__construct($asset_resolver, $config_factory, $css_collection_renderer, $js_collection_renderer, $request_stack, $renderer, $module_handler);
  }

  /**
   * {@inheritdoc}
   */
  public function processAttachments(AttachmentsInterface $response) {
    assert($response instanceof HtmlResponse);

    // First, render the actual placeholders; this will cause the BigPipe
    // placeholder strategy to generate BigPipe placeholders. We need those to
    // exist already so that we can extract BigPipe placeholders. This is hence
    // a bit of unfortunate but necessary duplication.
    // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
    // (Note this is copied verbatim from
    // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments)
    try {
      $response = $this
        ->renderPlaceholders($response);
    } catch (EnforcedResponseException $e) {
      return $e
        ->getResponse();
    }

    // Extract BigPipe placeholders; HtmlResponseAttachmentsProcessor does not
    // know (nor need to know) how to process those.
    $attachments = $response
      ->getAttachments();
    $big_pipe_placeholders = [];
    $big_pipe_nojs_placeholders = [];
    if (isset($attachments['big_pipe_placeholders'])) {
      $big_pipe_placeholders = $attachments['big_pipe_placeholders'];
      unset($attachments['big_pipe_placeholders']);
    }
    if (isset($attachments['big_pipe_nojs_placeholders'])) {
      $big_pipe_nojs_placeholders = $attachments['big_pipe_nojs_placeholders'];
      unset($attachments['big_pipe_nojs_placeholders']);
    }
    $html_response = clone $response;
    $html_response
      ->setAttachments($attachments);

    // Call HtmlResponseAttachmentsProcessor to process all other attachments.
    $processed_html_response = $this->htmlResponseAttachmentsProcessor
      ->processAttachments($html_response);

    // Restore BigPipe placeholders.
    $attachments = $processed_html_response
      ->getAttachments();
    $big_pipe_response = clone $processed_html_response;
    if (count($big_pipe_placeholders)) {
      $attachments['big_pipe_placeholders'] = $big_pipe_placeholders;
    }
    if (count($big_pipe_nojs_placeholders)) {
      $attachments['big_pipe_nojs_placeholders'] = $big_pipe_nojs_placeholders;
    }
    $big_pipe_response
      ->setAttachments($attachments);
    return $big_pipe_response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BigPipeResponseAttachmentsProcessor::$htmlResponseAttachmentsProcessor protected property The HTML response attachments processor service.
BigPipeResponseAttachmentsProcessor::processAttachments public function Processes the attachments of a response that has attachments. Overrides HtmlResponseAttachmentsProcessor::processAttachments
BigPipeResponseAttachmentsProcessor::__construct public function Constructs a BigPipeResponseAttachmentsProcessor object. Overrides HtmlResponseAttachmentsProcessor::__construct
HtmlResponseAttachmentsProcessor::$assetResolver protected property The asset resolver service.
HtmlResponseAttachmentsProcessor::$config protected property A config object for the system performance configuration.
HtmlResponseAttachmentsProcessor::$cssCollectionRenderer protected property The CSS asset collection renderer service.
HtmlResponseAttachmentsProcessor::$jsCollectionRenderer protected property The JS asset collection renderer service.
HtmlResponseAttachmentsProcessor::$moduleHandler protected property The module handler service.
HtmlResponseAttachmentsProcessor::$renderer protected property The renderer.
HtmlResponseAttachmentsProcessor::$requestStack protected property The request stack.
HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes public static function Formats an attribute string for an HTTP header.
HtmlResponseAttachmentsProcessor::processAssetLibraries protected function Processes asset libraries into render arrays.
HtmlResponseAttachmentsProcessor::processFeed protected function Transform a 'feed' attachment into an 'html_head_link' attachment.
HtmlResponseAttachmentsProcessor::processHtmlHead protected function Ensure proper key/data order and defaults for renderable head items.
HtmlResponseAttachmentsProcessor::processHtmlHeadLink protected function Transform a html_head_link array into html_head and http_header arrays.
HtmlResponseAttachmentsProcessor::renderHtmlResponseAttachmentPlaceholders protected function Renders HTML response attachment placeholders.
HtmlResponseAttachmentsProcessor::renderPlaceholders protected function Renders placeholders (#attached['placeholders']).
HtmlResponseAttachmentsProcessor::setHeaders protected function Sets headers on a response object.