From 5833d313f676fa1dbabdc3c378c4d66324ae61be Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sat, 30 Mar 2024 17:07:38 +0000 Subject: [PATCH] Issue #3436468 by longwave, smustgrave, alexpott: AjaxResponseSubscriber should lazily instantiate AjaxResponseAttachmentsProcessor --- core/core.services.yml | 2 +- .../AjaxResponseSubscriber.php | 22 +++++++------------ .../KernelTests/Core/Ajax/CommandsTest.php | 2 +- .../Tests/Core/Ajax/AjaxResponseTest.php | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index cc76c1dfd909..4e6b575dcc3c 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1200,7 +1200,7 @@ services: arguments: ['@resolver_manager.entity'] ajax_response.subscriber: class: Drupal\Core\EventSubscriber\AjaxResponseSubscriber - arguments: ['@ajax_response.attachments_processor'] + autowire: true form_ajax_subscriber: class: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber arguments: ['@form_ajax_response_builder', '@string_translation', '@messenger'] diff --git a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php index 9869f7288269..8e37afbcdf9c 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php @@ -4,7 +4,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Ajax\AjaxResponse; -use Drupal\Core\Render\AttachmentsResponseProcessorInterface; +use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; @@ -15,22 +15,16 @@ */ class AjaxResponseSubscriber implements EventSubscriberInterface { - /** - * The AJAX response attachments processor service. - * - * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface - */ - protected $ajaxResponseAttachmentsProcessor; - /** * Constructs an AjaxResponseSubscriber object. * - * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $ajax_response_attachments_processor - * The AJAX response attachments processor service. + * @param \Closure $processorClosure + * The AJAX response attachments processor service, wrapped in a closure. */ - public function __construct(AttachmentsResponseProcessorInterface $ajax_response_attachments_processor) { - $this->ajaxResponseAttachmentsProcessor = $ajax_response_attachments_processor; - } + public function __construct( + #[AutowireServiceClosure('ajax_response.attachments_processor')] + protected \Closure $processorClosure + ) {} /** * Request parameter to indicate that a request is a Drupal Ajax request. @@ -59,7 +53,7 @@ public function onRequest(RequestEvent $event) { public function onResponse(ResponseEvent $event) { $response = $event->getResponse(); if ($response instanceof AjaxResponse) { - $this->ajaxResponseAttachmentsProcessor->processAttachments($response); + ($this->processorClosure)()->processAttachments($response); // IE 9 does not support XHR 2 (http://caniuse.com/#feat=xhr2), so // for that browser, jquery.form submits requests containing a file upload diff --git a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php index b4480f160bb6..7dda4f4bc265 100644 --- a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php @@ -39,7 +39,7 @@ public function testAttachedSettings() { ]); $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor'); - $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); + $subscriber = new AjaxResponseSubscriber(fn() => $ajax_response_attachments_processor); $event = new ResponseEvent( \Drupal::service('http_kernel'), new Request(), diff --git a/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php b/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php index 0033fd3b41ef..ac17d0765091 100644 --- a/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php +++ b/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php @@ -86,7 +86,7 @@ public function testPrepareResponseForIeFormRequestsWithFileUpload() { $response->headers->set('Content-Type', 'application/json; charset=utf-8'); $ajax_response_attachments_processor = $this->createMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface'); - $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); + $subscriber = new AjaxResponseSubscriber(fn() => $ajax_response_attachments_processor); $event = new ResponseEvent( $this->createMock('\Symfony\Component\HttpKernel\HttpKernelInterface'), $request, -- GitLab