HtmlResponseSubscriber.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Render\HtmlResponse;
  4. use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
  5. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * Response subscriber to handle HTML responses.
  10. */
  11. class HtmlResponseSubscriber implements EventSubscriberInterface {
  12. /**
  13. * The HTML response attachments processor service.
  14. *
  15. * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
  16. */
  17. protected $htmlResponseAttachmentsProcessor;
  18. /**
  19. * Constructs a HtmlResponseSubscriber object.
  20. *
  21. * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
  22. * The HTML response attachments processor service.
  23. */
  24. public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor) {
  25. $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
  26. }
  27. /**
  28. * Processes attachments for HtmlResponse responses.
  29. *
  30. * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
  31. * The event to process.
  32. */
  33. public function onRespond(FilterResponseEvent $event) {
  34. $response = $event->getResponse();
  35. if (!$response instanceof HtmlResponse) {
  36. return;
  37. }
  38. $event->setResponse($this->htmlResponseAttachmentsProcessor->processAttachments($response));
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function getSubscribedEvents() {
  44. $events[KernelEvents::RESPONSE][] = ['onRespond'];
  45. return $events;
  46. }
  47. }