core/core.services.yml | 7 +- .../Drupal/Core/Render/PlaceholderGenerator.php | 92 +++++++++++++++++++++ .../Core/Render/PlaceholderGeneratorInterface.php | 47 +++++++++++ .../Core/Render/PlaceholderingRenderCache.php | 80 +++---------------- core/lib/Drupal/Core/Render/Renderer.php | 93 +++------------------- .../Drupal/Tests/Core/Render/RendererTestBase.php | 13 ++- 6 files changed, 178 insertions(+), 154 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index dfa13d1..2ca91ad 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1484,12 +1484,15 @@ services: tags: - { name: mime_type_guesser } lazy: true + render_placeholder_generator: + class: Drupal\Core\Render\PlaceholderGenerator + arguments: ['%renderer.config%'] render_cache: class: Drupal\Core\Render\PlaceholderingRenderCache - arguments: ['@request_stack', '@cache_factory', '@cache_contexts_manager', '%renderer.config%'] + arguments: ['@request_stack', '@cache_factory', '@cache_contexts_manager', '@render_placeholder_generator'] renderer: class: Drupal\Core\Render\Renderer - arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@render_cache', '@request_stack', '%renderer.config%'] + arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@render_placeholder_generator', '@render_cache', '@request_stack', '%renderer.config%'] early_rendering_controller_wrapper_subscriber: class: Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber arguments: ['@controller_resolver', '@renderer'] diff --git a/core/lib/Drupal/Core/Render/PlaceholderGenerator.php b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php new file mode 100644 index 0000000..eeeaf24 --- /dev/null +++ b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php @@ -0,0 +1,92 @@ +rendererConfig = $renderer_config; + } + + /** + * {@inheritdoc} + */ + public function shouldAutomaticallyPlaceholder(array $element) { + $conditions = $this->rendererConfig['auto_placeholder_conditions']; + + // Auto-placeholder if max-age is at or below the configured threshold. + if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) { + return TRUE; + } + + // Auto-placeholder if a high-cardinality cache context is set. + if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) { + return TRUE; + } + + // Auto-placeholder if a high-invalidation frequency cache tag is set. + if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) { + return TRUE; + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function createPlaceholder(array $element) { + $placeholder_render_array = array_intersect_key($element, [ + // Placeholders are replaced with markup by executing the associated + // #lazy_builder callback, which generates a render array, and which the + // Renderer will render and replace the placeholder with. + '#lazy_builder' => TRUE, + // The cacheability metadata for the placeholder. The rendered result of + // the placeholder may itself be cached, if [#cache][keys] are specified. + '#cache' => TRUE, + ]); + + // Generate placeholder markup. Note that the only requirement is that this + // is unique markup that isn't easily guessable. The #lazy_builder callback + // and its arguments are put in the placeholder markup solely to simplify + // debugging. + $attributes = new Attribute(); + $attributes['callback'] = $placeholder_render_array['#lazy_builder'][0]; + $attributes['arguments'] = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]); + $attributes['token'] = hash('sha1', serialize($placeholder_render_array)); + $placeholder_markup = SafeMarkup::format('', ['@attributes' => $attributes]); + + // Build the placeholder element to return. + $placeholder_element = []; + $placeholder_element['#markup'] = $placeholder_markup; + $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array; + return $placeholder_element; + } + +} diff --git a/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php new file mode 100644 index 0000000..eb96673 --- /dev/null +++ b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php @@ -0,0 +1,47 @@ +rendererConfig = $renderer_config; + $this->placeholderGenerator = $placeholder_generator; } /** @@ -92,7 +92,7 @@ public function get(array $elements) { return FALSE; } else { - if ($this->canCreatePlaceholder($elements) && $this->shouldAutomaticallyPlaceholder($cached_element)) { + if ($this->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) { return $this->createPlaceholder($elements); } @@ -138,48 +138,17 @@ public function set(array &$elements, array $pre_bubbling_elements) { $this->expireOverride = FALSE; } - if ($this->canCreatePlaceholder($pre_bubbling_elements) && $this->shouldAutomaticallyPlaceholder($elements)) { + if ($this->canCreatePlaceholder($pre_bubbling_elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { // Overwrite $elements with a placeholder. The Renderer (which called this // method) will update the context with the bubbleable metadata of the // overwritten $elements. - $elements = $this->createPlaceholder($pre_bubbling_elements); + $elements = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements); } return $result; } /** - * Whether the given render array should be automatically placeholdered. - * - * @param array $element - * The render array whose cacheability to analyze. - * - * @return bool - * Whether the given render array's cacheability meets the placeholdering - * conditions. - */ - protected function shouldAutomaticallyPlaceholder(array $element) { - $conditions = $this->rendererConfig['auto_placeholder_conditions']; - - // Auto-placeholder if max-age is at or below the configured threshold. - if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) { - return TRUE; - } - - // Auto-placeholder if a high-cardinality cache context is set. - if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) { - return TRUE; - } - - // Auto-placeholder if a high-invalidation frequency cache tag is set. - if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) { - return TRUE; - } - - return FALSE; - } - - /** * {@inheritdoc} */ protected function maxAgeToExpire($max_age) { @@ -209,33 +178,4 @@ protected function canCreatePlaceholder(array $element) { (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE); } - // @todo refactor - protected function createPlaceholder($element) { - $placeholder_render_array = array_intersect_key($element, [ - // Placeholders are replaced with markup by executing the associated - // #lazy_builder callback, which generates a render array, and which the - // Renderer will render and replace the placeholder with. - '#lazy_builder' => TRUE, - // The cacheability metadata for the placeholder. The rendered result of - // the placeholder may itself be cached, if [#cache][keys] are specified. - '#cache' => TRUE, - ]); - - // Generate placeholder markup. Note that the only requirement is that this - // is unique markup that isn't easily guessable. The #lazy_builder callback - // and its arguments are put in the placeholder markup solely to simplify - // debugging. - $attributes = new Attribute(); - $attributes['callback'] = $placeholder_render_array['#lazy_builder'][0]; - $attributes['arguments'] = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]); - $attributes['token'] = hash('sha1', serialize($placeholder_render_array)); - $placeholder_markup = SafeMarkup::format('', ['@attributes' => $attributes]); - - // Build the placeholder element to return. - $placeholder_element = []; - $placeholder_element['#markup'] = $placeholder_markup; - $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array; - return $placeholder_element; - } - } diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index d5ccd25..0981a98 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -8,13 +8,11 @@ namespace Drupal\Core\Render; use Drupal\Component\Utility\SafeMarkup; -use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Controller\ControllerResolverInterface; -use Drupal\Core\Template\Attribute; use Drupal\Core\Theme\ThemeManagerInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -45,6 +43,13 @@ class Renderer implements RendererInterface { protected $elementInfo; /** + * The placeholder generator. + * + * @var \Drupal\Core\Render\PlaceholderGeneratorInterface + */ + protected $placeholderGenerator; + + /** * The render cache service. * * @var \Drupal\Core\Render\RenderCacheInterface @@ -98,6 +103,8 @@ class Renderer implements RendererInterface { * The theme manager. * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info * The element info. + * @param \Drupal\Core\Render\PlaceholderGeneratorInterface $placeholder_generator + * The placeholder generator. * @param \Drupal\Core\Render\RenderCacheInterface $render_cache * The render cache service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack @@ -105,10 +112,11 @@ class Renderer implements RendererInterface { * @param array $renderer_config * The renderer configuration array. */ - public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info, RenderCacheInterface $render_cache, RequestStack $request_stack, array $renderer_config) { + public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info, PlaceholderGeneratorInterface $placeholder_generator, RenderCacheInterface $render_cache, RequestStack $request_stack, array $renderer_config) { $this->controllerResolver = $controller_resolver; $this->theme = $theme; $this->elementInfo = $element_info; + $this->placeholderGenerator = $placeholder_generator; $this->renderCache = $render_cache; $this->rendererConfig = $renderer_config; $this->requestStack = $request_stack; @@ -344,7 +352,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) { } } // Determine whether to do auto-placeholdering. - if (isset($elements['#lazy_builder']) && (!isset($elements['#create_placeholder']) || $elements['#create_placeholder'] !== FALSE) && $this->shouldAutomaticallyPlaceholder($elements)) { + if (isset($elements['#lazy_builder']) && (!isset($elements['#create_placeholder']) || $elements['#create_placeholder'] !== FALSE) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { $elements['#create_placeholder'] = TRUE; } // If instructed to create a placeholder, and a #lazy_builder callback is @@ -354,7 +362,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) { if (!isset($elements['#lazy_builder'])) { throw new \LogicException('When #create_placeholder is set, a #lazy_builder callback must be present as well.'); } - $elements = $this->createPlaceholder($elements); + $elements = $this->placeholderGenerator->createPlaceholder($elements); } // Build the element if it is still empty. if (isset($elements['#lazy_builder'])) { @@ -652,81 +660,6 @@ protected function replacePlaceholders(array &$elements) { } /** - * Whether the given render array should be automatically placeholdered. - * - * @param array $element - * The render array whose cacheability to analyze. - * - * @return bool - * Whether the given render array's cacheability meets the placeholdering - * conditions. - */ - protected function shouldAutomaticallyPlaceholder(array $element) { - $conditions = $this->rendererConfig['auto_placeholder_conditions']; - - // Auto-placeholder if max-age is at or below the configured threshold. - if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) { - return TRUE; - } - - // Auto-placeholder if a high-cardinality cache context is set. - if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) { - return TRUE; - } - - // Auto-placeholder if a high-invalidation frequency cache tag is set. - if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) { - return TRUE; - } - - return FALSE; - } - - /** - * Turns this element into a placeholder. - * - * Placeholdering allows us to avoid "poor cacheability contamination": this - * maps the current render array to one that only has #markup and #attached, - * and #attached contains a placeholder with this element's prior cacheability - * metadata. In other words: this placeholder is perfectly cacheable, the - * placeholder replacement logic effectively cordons off poor cacheability. - * - * @param array $element - * The render array to create a placeholder for. - * - * @return array - * Render array with placeholder markup and the attached placeholder - * replacement metadata. - */ - protected function createPlaceholder(array $element) { - $placeholder_render_array = array_intersect_key($element, [ - // Placeholders are replaced with markup by executing the associated - // #lazy_builder callback, which generates a render array, and which the - // Renderer will render and replace the placeholder with. - '#lazy_builder' => TRUE, - // The cacheability metadata for the placeholder. The rendered result of - // the placeholder may itself be cached, if [#cache][keys] are specified. - '#cache' => TRUE, - ]); - - // Generate placeholder markup. Note that the only requirement is that this - // is unique markup that isn't easily guessable. The #lazy_builder callback - // and its arguments are put in the placeholder markup solely to simplify - // debugging. - $attributes = new Attribute(); - $attributes['callback'] = $placeholder_render_array['#lazy_builder'][0]; - $attributes['arguments'] = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]); - $attributes['token'] = hash('sha1', serialize($placeholder_render_array)); - $placeholder_markup = SafeMarkup::format('', ['@attributes' => $attributes]); - - // Build the placeholder element to return. - $placeholder_element = []; - $placeholder_element['#markup'] = $placeholder_markup; - $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array; - return $placeholder_element; - } - - /** * {@inheritdoc} */ public function mergeBubbleableMetadata(array $a, array $b) { diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index ed89e5e..e4c9ffe 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -12,6 +12,7 @@ use Drupal\Core\Cache\Context\ContextCacheKeys; use Drupal\Core\Cache\MemoryBackend; use Drupal\Core\Render\Element; +use Drupal\Core\Render\PlaceholderGenerator; use Drupal\Core\Render\PlaceholderingRenderCache; use Drupal\Core\Render\Renderer; use Drupal\Tests\UnitTestCase; @@ -39,6 +40,13 @@ class RendererTestBase extends UnitTestCase { protected $renderCache; /** + * The tested placeholder generator. + * + * @var \Drupal\Core\Render\PlaceholderGenerator + */ + protected $placeholderGenerator; + + /** * @var \Symfony\Component\HttpFoundation\RequestStack */ protected $requestStack; @@ -142,8 +150,9 @@ protected function setUp() { } return new ContextCacheKeys($keys, new CacheableMetadata()); }); - $this->renderCache = new PlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->rendererConfig); - $this->renderer = new Renderer($this->controllerResolver, $this->themeManager, $this->elementInfo, $this->renderCache, $this->requestStack, $this->rendererConfig); + $this->placeholderGenerator = new PlaceholderGenerator($this->rendererConfig); + $this->renderCache = new PlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->placeholderGenerator); + $this->renderer = new Renderer($this->controllerResolver, $this->themeManager, $this->elementInfo, $this->placeholderGenerator, $this->renderCache, $this->requestStack, $this->rendererConfig); $container = new ContainerBuilder(); $container->set('cache_contexts_manager', $this->cacheContextsManager);