diff --git a/core/core.services.yml b/core/core.services.yml index 453934b..414f573 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1048,7 +1048,7 @@ services: - { name: event_subscriber } main_content_renderer.html: class: Drupal\Core\Render\MainContent\HtmlRenderer - arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler', '@renderer', '@render_cache', '%renderer.config%'] + arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler', '@renderer', '@render_cache', '%renderer.config%', '@shutdown_registry'] tags: - { name: render.main_content_renderer, format: html } main_content_renderer.ajax: @@ -1088,7 +1088,7 @@ services: - { name: event_subscriber } bare_html_page_renderer: class: Drupal\Core\Render\BareHtmlPageRenderer - arguments: ['@renderer', '@html_response.attachments_processor'] + arguments: ['@renderer', '@html_response.attachments_processor', '@shutdown_registry'] lazy: true private_key: class: Drupal\Core\PrivateKey diff --git a/core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php b/core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php index 6c73a0d..2c28117 100644 --- a/core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php +++ b/core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php @@ -2,6 +2,8 @@ namespace Drupal\Core\Render; +use Drupal\Core\Utility\ShutDownRegistry; + /** * Default bare HTML page renderer. */ @@ -22,16 +24,26 @@ class BareHtmlPageRenderer implements BareHtmlPageRendererInterface { protected $htmlResponseAttachmentsProcessor; /** + * The shutdown registry service. + * + * @var \Drupal\Core\Utility\ShutDownRegistry + */ + protected $shutdownRegistry; + + /** * Constructs a new BareHtmlPageRenderer. * * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor * The HTML response attachments processor service. + * @param \Drupal\Core\Utility\ShutDownRegistry $shutdown_registry + * The shutdown registry. */ - public function __construct(RendererInterface $renderer, AttachmentsResponseProcessorInterface $html_response_attachments_processor) { + public function __construct(RendererInterface $renderer, AttachmentsResponseProcessorInterface $html_response_attachments_processor, ShutDownRegistry $shutdown_registry) { $this->renderer = $renderer; $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor; + $this->shutdownRegistry = $shutdown_registry; } /** @@ -65,7 +77,7 @@ public function renderBarePage(array $content, $title, $page_theme_property, arr system_page_attachments($html['page']); $this->renderer->renderRoot($html); - $response = new HtmlResponse(); + $response = new HtmlResponse('', 200, [], $this->shutdownRegistry->preventEarlyFlush()); $response->setContent($html); // Process attachments, because this does not go via the regular render // pipeline, but will be sent directly. diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php index 54c40f6..1796c95 100644 --- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php +++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php @@ -15,6 +15,7 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\Render\RenderEvents; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Utility\ShutDownRegistry; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; @@ -81,6 +82,13 @@ class HtmlRenderer implements MainContentRendererInterface { protected $rendererConfig; /** + * The shutdown registry service. + * + * @var \Drupal\Core\Utility\ShutDownRegistry + */ + protected $shutdownRegistry; + + /** * Constructs a new HtmlRenderer. * * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver @@ -97,8 +105,10 @@ class HtmlRenderer implements MainContentRendererInterface { * The render cache service. * @param array $renderer_config * The renderer configuration array. + * @param \Drupal\Core\Utility\ShutDownRegistry $shutdown_registry + * The shutdown registry. */ - public function __construct(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RenderCacheInterface $render_cache, array $renderer_config) { + public function __construct(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, RendererInterface $renderer, RenderCacheInterface $render_cache, array $renderer_config, ShutDownRegistry $shutdown_registry) { $this->titleResolver = $title_resolver; $this->displayVariantManager = $display_variant_manager; $this->eventDispatcher = $event_dispatcher; @@ -106,6 +116,7 @@ public function __construct(TitleResolverInterface $title_resolver, PluginManage $this->renderer = $renderer; $this->renderCache = $render_cache; $this->rendererConfig = $renderer_config; + $this->shutdownRegistry = $shutdown_registry; } /** @@ -161,9 +172,14 @@ public function renderResponse(array $main_content, Request $request, RouteMatch // entire render cache, regardless of the cache bin. $content['#cache']['tags'][] = 'rendered'; - $response = new HtmlResponse($content, 200, [ - 'Content-Type' => 'text/html; charset=UTF-8', - ]); + $response = new HtmlResponse( + $content, + 200, + [ + 'Content-Type' => 'text/html; charset=UTF-8', + ], + $this->shutdownRegistry->preventEarlyFlush() + ); return $response; } diff --git a/core/lib/Drupal/Core/Render/Response.php b/core/lib/Drupal/Core/Render/Response.php index 858c7f8..6013e67 100644 --- a/core/lib/Drupal/Core/Render/Response.php +++ b/core/lib/Drupal/Core/Render/Response.php @@ -13,13 +13,37 @@ class Response extends SymfonyResponse { /** + * Whether to prevent an early flush. + * + * @var bool + */ + protected $preventEarlyFlush; + + /** + * Response constructor. + * + * @param string $content + * The response content, see setContent(). + * @param int $status + * The response status code. + * @param array $headers + * An array of response headers. + * @param bool $prevent_early_flush + * Whether an early flush should be prevented. + */ + public function __construct($content = '', $status = 200, array $headers = [], $prevent_early_flush = FALSE) { + parent::__construct($content, $status, $headers); + $this->preventEarlyFlush = $prevent_early_flush; + } + + /** * {@inheritdoc} */ public function send() { $this->sendHeaders(); $this->sendContent(); - if (!\Drupal::service('shutdown_registry')->preventEarlyFlush() && function_exists('fastcgi_finish_request')) { + if (!$this->preventEarlyFlush && function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } elseif ('cli' !== PHP_SAPI) {