Commit 1221e7e5 authored by catch's avatar catch
Browse files

Issue #3397173 by Spokje, smustgrave: Convert both BookNavigationCacheContext...

Issue #3397173 by Spokje, smustgrave: Convert both BookNavigationCacheContext and MenuActiveTrailsCacheContext to use lazy services

(cherry picked from commit e3ba2f7f5d0a115fae4334ee98d1e9c833a4a3dc)
parent 728f592f
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -150,8 +150,7 @@ services:
      - { name: cache.context }
  cache_context.route.menu_active_trails:
    class: Drupal\Core\Cache\Context\MenuActiveTrailsCacheContext
    calls:
      - [setContainer, ['@service_container']]
    arguments: ['@menu.active_trail']
    tags:
      - { name: cache.context }

@@ -785,6 +784,7 @@ services:
    arguments: ['@plugin.manager.menu.link', '@current_route_match', '@cache.menu', '@lock']
    tags:
      - { name: needs_destruction }
    lazy: true
  Drupal\Core\Menu\MenuActiveTrailInterface: '@menu.active_trail'
  menu.parent_form_selector:
    class: Drupal\Core\Menu\MenuParentFormSelector
+15 −9
Original line number Diff line number Diff line
@@ -3,18 +3,25 @@
namespace Drupal\Core\Cache\Context;

use Drupal\Core\Cache\CacheableMetadata;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Drupal\Core\Menu\MenuActiveTrailInterface;

/**
 * Defines the MenuActiveTrailsCacheContext service.
 *
 * This class is container-aware to avoid initializing the 'menu.active_trails'
 * service (and its dependencies) when it is not necessary.
 */
class MenuActiveTrailsCacheContext implements CalculatedCacheContextInterface, ContainerAwareInterface {
class MenuActiveTrailsCacheContext implements CalculatedCacheContextInterface {

  use ContainerAwareTrait;
  /**
   * Constructs a MenuActiveTrailsCacheContext object.
   *
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface|null $menuActiveTrailService
   *   The menu active trail service.
   */
  public function __construct(protected ?MenuActiveTrailInterface $menuActiveTrailService = NULL) {
    if ($this->menuActiveTrailService === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $menuActiveTrailService argument is deprecated in drupal:10.2.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3397515', E_USER_DEPRECATED);
      $this->menuActiveTrailService = \Drupal::service('menu.active_trail');
    }
  }

  /**
   * {@inheritdoc}
@@ -31,8 +38,7 @@ public function getContext($menu_name = NULL) {
      throw new \LogicException('No menu name provided for menu.active_trails cache context.');
    }

    $active_trail = $this->container->get('menu.active_trail')
      ->getActiveTrailIds($menu_name);
    $active_trail = $this->menuActiveTrailService->getActiveTrailIds($menu_name);
    return 'menu_trail.' . $menu_name . '|' . implode('|', $active_trail);
  }

+144 −0
Original line number Diff line number Diff line
<?php
// phpcs:ignoreFile

/**
 * This file was generated via php core/scripts/generate-proxy-class.php 'Drupal\Core\Menu\MenuActiveTrail' "core/lib/Drupal/Core".
 */

namespace Drupal\Core\ProxyClass\Menu {

    /**
     * Provides a proxy class for \Drupal\Core\Menu\MenuActiveTrail.
     *
     * @see \Drupal\Component\ProxyBuilder
     */
    class MenuActiveTrail implements \Drupal\Core\Cache\CacheCollectorInterface, \Drupal\Core\DestructableInterface, \Drupal\Core\Menu\MenuActiveTrailInterface
    {

        use \Drupal\Core\DependencyInjection\DependencySerializationTrait;

        /**
         * The id of the original proxied service.
         *
         * @var string
         */
        protected $drupalProxyOriginalServiceId;

        /**
         * The real proxied service, after it was lazy loaded.
         *
         * @var \Drupal\Core\Menu\MenuActiveTrail
         */
        protected $service;

        /**
         * The service container.
         *
         * @var \Symfony\Component\DependencyInjection\ContainerInterface
         */
        protected $container;

        /**
         * Constructs a ProxyClass Drupal proxy object.
         *
         * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
         *   The container.
         * @param string $drupal_proxy_original_service_id
         *   The service ID of the original service.
         */
        public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
        {
            $this->container = $container;
            $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
        }

        /**
         * Lazy loads the real service from the container.
         *
         * @return object
         *   Returns the constructed real service.
         */
        protected function lazyLoadItself()
        {
            if (!isset($this->service)) {
                $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
            }

            return $this->service;
        }

        /**
         * {@inheritdoc}
         */
        public function getActiveTrailIds($menu_name)
        {
            return $this->lazyLoadItself()->getActiveTrailIds($menu_name);
        }

        /**
         * {@inheritdoc}
         */
        public function getActiveLink($menu_name = NULL)
        {
            return $this->lazyLoadItself()->getActiveLink($menu_name);
        }

        /**
         * {@inheritdoc}
         */
        public function has($key)
        {
            return $this->lazyLoadItself()->has($key);
        }

        /**
         * {@inheritdoc}
         */
        public function get($key)
        {
            return $this->lazyLoadItself()->get($key);
        }

        /**
         * {@inheritdoc}
         */
        public function set($key, $value)
        {
            return $this->lazyLoadItself()->set($key, $value);
        }

        /**
         * {@inheritdoc}
         */
        public function delete($key)
        {
            return $this->lazyLoadItself()->delete($key);
        }

        /**
         * {@inheritdoc}
         */
        public function reset()
        {
            return $this->lazyLoadItself()->reset();
        }

        /**
         * {@inheritdoc}
         */
        public function clear()
        {
            return $this->lazyLoadItself()->clear();
        }

        /**
         * {@inheritdoc}
         */
        public function destruct()
        {
            return $this->lazyLoadItself()->destruct();
        }

    }

}
+2 −3
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ services:
  book.manager:
    class: Drupal\book\BookManager
    arguments: ['@entity_type.manager', '@string_translation', '@config.factory', '@book.outline_storage', '@renderer', '@language_manager', '@entity.repository', '@book.backend_chained_cache', '@book.memory_cache']
    lazy: true
  Drupal\book\BookManagerInterface: '@book.manager'
  book.outline:
    class: Drupal\book\BookOutline
@@ -29,9 +30,7 @@ services:
      - { name: access_check, applies_to: _access_book_removable }
  cache_context.route.book_navigation:
    class: Drupal\book\Cache\BookNavigationCacheContext
    arguments: ['@current_route_match']
    calls:
      - [setContainer, ['@service_container']]
    arguments: ['@current_route_match', '@book.manager']
    tags:
      - { name: cache.context}

+10 −10
Original line number Diff line number Diff line
@@ -2,12 +2,11 @@

namespace Drupal\book\Cache;

use Drupal\book\BookManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

/**
 * Defines the book navigation cache context service.
@@ -17,13 +16,8 @@
 * This allows for book navigation location-aware caching. It depends on:
 * - whether the current route represents a book node at all
 * - and if so, where in the book hierarchy we are
 *
 * This class is container-aware to avoid initializing the 'book.manager'
 * service when it is not necessary.
 */
class BookNavigationCacheContext implements CacheContextInterface, ContainerAwareInterface {

  use ContainerAwareTrait;
class BookNavigationCacheContext implements CacheContextInterface {

  /**
   * The current route match.
@@ -37,9 +31,15 @@ class BookNavigationCacheContext implements CacheContextInterface, ContainerAwar
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\book\BookManagerInterface|null $bookManagerService
   *   The book manager service.
   */
  public function __construct(RouteMatchInterface $route_match) {
  public function __construct(RouteMatchInterface $route_match, public ?BookManagerInterface $bookManagerService = NULL) {
    $this->routeMatch = $route_match;
    if ($this->bookManagerService === NULL) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $bookManagerService argument is deprecated in drupal:10.2.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3397515', E_USER_DEPRECATED);
      $this->bookManagerService = \Drupal::service('book.manager');
    }
  }

  /**
@@ -66,7 +66,7 @@ public function getContext() {
    }

    // If we're looking at a book node, get the trail for that node.
    $active_trail = $this->container->get('book.manager')
    $active_trail = $this->bookManagerService
      ->getActiveTrailIds($node->book['bid'], $node->book);
    return implode('|', $active_trail);
  }
Loading