Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / big_pipe / src / Render / BigPipeResponseAttachmentsProcessor.php
1 <?php
2
3 namespace Drupal\big_pipe\Render;
4
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;
16
17 /**
18  * Processes attachments of HTML responses with BigPipe enabled.
19  *
20  * @see \Drupal\Core\Render\HtmlResponseAttachmentsProcessor
21  * @see \Drupal\big_pipe\Render\BigPipe
22  */
23 class BigPipeResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {
24
25   /**
26    * The HTML response attachments processor service.
27    *
28    * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
29    */
30   protected $htmlResponseAttachmentsProcessor;
31
32   /**
33    * Constructs a BigPipeResponseAttachmentsProcessor object.
34    *
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
38    *   An 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
46    *   The request stack.
47    * @param \Drupal\Core\Render\RendererInterface $renderer
48    *   The renderer.
49    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
50    *   The module handler service.
51    */
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);
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function processAttachments(AttachmentsInterface $response) {
61     assert($response instanceof HtmlResponse);
62
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)
70     try {
71       $response = $this->renderPlaceholders($response);
72     }
73     catch (EnforcedResponseException $e) {
74       return $e->getResponse();
75     }
76
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']);
85     }
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']);
89     }
90     $html_response = clone $response;
91     $html_response->setAttachments($attachments);
92
93     // Call HtmlResponseAttachmentsProcessor to process all other attachments.
94     $processed_html_response = $this->htmlResponseAttachmentsProcessor->processAttachments($html_response);
95
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;
101     }
102     if (count($big_pipe_nojs_placeholders)) {
103       $attachments['big_pipe_nojs_placeholders'] = $big_pipe_nojs_placeholders;
104     }
105     $big_pipe_response->setAttachments($attachments);
106
107     return $big_pipe_response;
108   }
109
110 }