BigPipeResponseAttachmentsProcessor.php in Drupal 8        
                          
                  
                        
  
  
  
  
File
  core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php
  
    View source  
  <?php
namespace Drupal\big_pipe\Render;
use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\EnforcedResponseException;
use Drupal\Core\Render\AttachmentsInterface;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class BigPipeResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {
  
  protected $htmlResponseAttachmentsProcessor;
  
  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);
  }
  
  public function processAttachments(AttachmentsInterface $response) {
    assert($response instanceof HtmlResponse);
    
    try {
      $response = $this
        ->renderPlaceholders($response);
    } catch (EnforcedResponseException $e) {
      return $e
        ->getResponse();
    }
    
    $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);
    
    $processed_html_response = $this->htmlResponseAttachmentsProcessor
      ->processAttachments($html_response);
    
    $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;
  }
}