3 namespace Drupal\big_pipe\Render;
5 use Drupal\Core\Asset\AssetCollectionRendererInterface;
6 use Drupal\Core\Asset\AssetResolverInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Form\EnforcedResponseException;
10 use Drupal\Core\Render\AttachmentsInterface;
11 use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
12 use Drupal\Core\Render\HtmlResponse;
13 use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
14 use Drupal\Core\Render\RendererInterface;
15 use Symfony\Component\HttpFoundation\RequestStack;
18 * Processes attachments of HTML responses with BigPipe enabled.
20 * @see \Drupal\Core\Render\HtmlResponseAttachmentsProcessor
21 * @see \Drupal\big_pipe\Render\BigPipe
23 class BigPipeResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {
26 * The HTML response attachments processor service.
28 * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
30 protected $htmlResponseAttachmentsProcessor;
33 * Constructs a BigPipeResponseAttachmentsProcessor object.
35 * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
36 * The HTML response attachments processor service.
37 * @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
39 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
40 * A config factory for retrieving required config objects.
41 * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
42 * The CSS asset collection renderer.
43 * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $js_collection_renderer
44 * The JS asset collection renderer.
45 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
47 * @param \Drupal\Core\Render\RendererInterface $renderer
49 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
50 * The module handler service.
52 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) {
53 $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
54 parent::__construct($asset_resolver, $config_factory, $css_collection_renderer, $js_collection_renderer, $request_stack, $renderer, $module_handler);
60 public function processAttachments(AttachmentsInterface $response) {
61 assert($response instanceof HtmlResponse);
63 // First, render the actual placeholders; this will cause the BigPipe
64 // placeholder strategy to generate BigPipe placeholders. We need those to
65 // exist already so that we can extract BigPipe placeholders. This is hence
66 // a bit of unfortunate but necessary duplication.
67 // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
68 // (Note this is copied verbatim from
69 // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments)
71 $response = $this->renderPlaceholders($response);
73 catch (EnforcedResponseException $e) {
74 return $e->getResponse();
77 // Extract BigPipe placeholders; HtmlResponseAttachmentsProcessor does not
78 // know (nor need to know) how to process those.
79 $attachments = $response->getAttachments();
80 $big_pipe_placeholders = [];
81 $big_pipe_nojs_placeholders = [];
82 if (isset($attachments['big_pipe_placeholders'])) {
83 $big_pipe_placeholders = $attachments['big_pipe_placeholders'];
84 unset($attachments['big_pipe_placeholders']);
86 if (isset($attachments['big_pipe_nojs_placeholders'])) {
87 $big_pipe_nojs_placeholders = $attachments['big_pipe_nojs_placeholders'];
88 unset($attachments['big_pipe_nojs_placeholders']);
90 $html_response = clone $response;
91 $html_response->setAttachments($attachments);
93 // Call HtmlResponseAttachmentsProcessor to process all other attachments.
94 $processed_html_response = $this->htmlResponseAttachmentsProcessor->processAttachments($html_response);
96 // Restore BigPipe placeholders.
97 $attachments = $processed_html_response->getAttachments();
98 $big_pipe_response = clone $processed_html_response;
99 if (count($big_pipe_placeholders)) {
100 $attachments['big_pipe_placeholders'] = $big_pipe_placeholders;
102 if (count($big_pipe_nojs_placeholders)) {
103 $attachments['big_pipe_nojs_placeholders'] = $big_pipe_nojs_placeholders;
105 $big_pipe_response->setAttachments($attachments);
107 return $big_pipe_response;