Version 1
[yaffs-website] / web / modules / contrib / http2_server_push / src / Render / HtmlResponseAttachmentsProcessor.php
1 <?php
2
3 namespace Drupal\http2_server_push\Render;
4
5 use Drupal\Core\Render\AttachmentsInterface;
6 use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
7 use Symfony\Component\HttpFoundation\RequestStack;
8
9 /**
10  * Decorates the HTML response attachments processor service, adds Server Push.
11  *
12  * @see \Drupal\http2_server_push\Asset\CssCollectionRenderer
13  * @see \Drupal\http2_server_push\Asset\JsCollectionRenderer
14  */
15 class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface {
16
17   /**
18    * The decorated HTML response attachments processor service.
19    *
20    * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
21    */
22   protected $htmlResponseAttachmentsProcessor;
23
24   /**
25    * The request stack.
26    *
27    * @var \Symfony\Component\HttpFoundation\RequestStack
28    */
29   protected $requestStack;
30
31   /**
32    * Constructs a HtmlResponseAttachmentsProcessor object.
33    *
34    * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
35    *   The decorated HTML response attachments processor service.
36    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
37    *   The request stack.
38    */
39   public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor, RequestStack $request_stack) {
40     $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
41     $this->requestStack = $request_stack;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function processAttachments(AttachmentsInterface $response) {
48     $response = $this->htmlResponseAttachmentsProcessor->processAttachments($response);
49
50     $request = $this->requestStack->getCurrentRequest();
51     if ($request->attributes->has('http2_server_push_link_headers')) {
52       $link_headers = $request->attributes->get('http2_server_push_link_headers');
53       $response->headers->set('Link', $link_headers, FALSE);
54     }
55
56     return $response;
57   }
58
59 }