Verified Commit 0802e699 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3443490 by smustgrave: Remove deprecated code from lib/Logger and lib/Menu

parent 58ed5a15
Loading
Loading
Loading
Loading
Loading
+4 −52
Original line number Diff line number Diff line
@@ -11,16 +11,6 @@
 */
class LoggerChannelFactory implements LoggerChannelFactoryInterface {

  /**
   * The request stack.
   */
  protected ?RequestStack $requestStack = NULL;

  /**
   * The current user.
   */
  protected ?AccountInterface $currentUser = NULL;

  /**
   * Array of all instantiated logger channels keyed by channel name.
   *
@@ -38,37 +28,21 @@ class LoggerChannelFactory implements LoggerChannelFactoryInterface {
  /**
   * Constructs a LoggerChannelFactory.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack|null $requestStack
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   (optional) The request stack.
   * @param \Drupal\Core\Session\AccountInterface|null $currentUser
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   (optional) The current user.
   */
  public function __construct(
    ?RequestStack $requestStack = NULL,
    ?AccountInterface $currentUser = NULL,
    protected RequestStack $requestStack,
    protected AccountInterface $currentUser,
  ) {
    $this->requestStack = $requestStack;
    $this->currentUser = $currentUser;
    if (!$requestStack) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $requestStack argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354', E_USER_DEPRECATED);
      $this->requestStack = \Drupal::service('request_stack');
    }
    if (!$currentUser) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $currentUser argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354', E_USER_DEPRECATED);
      $this->currentUser = \Drupal::service('current_user');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($channel) {
    if (!$this->requestStack || !$this->currentUser) {
      @trigger_error('Calling ' . __METHOD__ . ' without calling the constructor is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354', E_USER_DEPRECATED);
      $this->requestStack = \Drupal::service('request_stack');
      $this->currentUser = \Drupal::service('current_user');
    }

    if (!isset($this->channels[$channel])) {
      $instance = new LoggerChannel($channel);

@@ -97,26 +71,4 @@ public function addLogger(LoggerInterface $logger, $priority = 0) {
    }
  }

  /**
   * Sets the service container.
   *
   * @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use
   *    dependency injection instead.
   *
   * @see https://www.drupal.org/node/3416354
   */
  public function setContainer() {
    @trigger_error('Calling ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use dependency injection instead. See https://www.drupal.org/node/3416354', E_USER_DEPRECATED);
  }

  /**
   * {@inheritdoc}
   */
  public function __get(string $name) {
    if ($name === 'container') {
      @trigger_error('Accessing the container property in ' . __CLASS__ . ' is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use dependency injection instead. See https://www.drupal.org/node/3416354', E_USER_DEPRECATED);
      return \Drupal::getContainer();
    }
  }

}
+9 −40
Original line number Diff line number Diff line
@@ -20,55 +20,24 @@
 */
class DefaultMenuLinkTreeManipulators {

  /**
   * The access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators object.
   *
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   * @param \Drupal\Core\Access\AccessManagerInterface $accessManager
   *   The access manager.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface|null $module_handler
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   */
  public function __construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler = NULL) {
    $this->accessManager = $access_manager;
    $this->account = $account;
    $this->entityTypeManager = $entity_type_manager;
    if ($module_handler === NULL) {
      @trigger_error('Calling DefaultMenuLinkTreeManipulators::__construct() without the $module_handler argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3336973', E_USER_DEPRECATED);
      $module_handler = \Drupal::moduleHandler();
    }
    $this->moduleHandler = $module_handler;
  public function __construct(
    protected AccessManagerInterface $accessManager,
    protected AccountInterface $account,
    protected EntityTypeManagerInterface $entityTypeManager,
    protected ModuleHandlerInterface $moduleHandler,
  ) {
  }

  /**
+12 −51
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerResolverInterface;
use Drupal\Core\Routing\PreloadableRouteProviderInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Template\Attribute;
@@ -16,65 +15,27 @@
 */
class MenuLinkTree implements MenuLinkTreeInterface {

  /**
   * The menu link tree storage.
   *
   * @var \Drupal\Core\Menu\MenuTreeStorageInterface
   */
  protected $treeStorage;

  /**
   * The menu link plugin manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * The route provider to load routes by name.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The active menu trail service.
   *
   * @var \Drupal\Core\Menu\MenuActiveTrailInterface
   */
  protected $menuActiveTrail;

  /**
   * The callable resolver.
   *
   * @var \Drupal\Core\Utility\CallableResolver
   */
  protected CallableResolver $callableResolver;

  /**
   * Constructs a \Drupal\Core\Menu\MenuLinkTree object.
   *
   * @param \Drupal\Core\Menu\MenuTreeStorageInterface $tree_storage
   * @param \Drupal\Core\Menu\MenuTreeStorageInterface $treeStorage
   *   The menu link tree storage.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   *   The menu link plugin manager.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   * @param \Drupal\Core\Routing\RouteProviderInterface $routeProvider
   *   The route provider to load routes by name.
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menuActiveTrail
   *   The active menu trail service.
   * @param \Drupal\Core\Utility\CallableResolver|\Drupal\Core\Controller\ControllerResolverInterface $callable_resolver
   * @param \Drupal\Core\Utility\CallableResolver $callableResolver
   *   The callable resolver.
   */
  public function __construct(MenuTreeStorageInterface $tree_storage, MenuLinkManagerInterface $menu_link_manager, RouteProviderInterface $route_provider, MenuActiveTrailInterface $menu_active_trail, ControllerResolverInterface|CallableResolver $callable_resolver) {
    $this->treeStorage = $tree_storage;
    $this->menuLinkManager = $menu_link_manager;
    $this->routeProvider = $route_provider;
    $this->menuActiveTrail = $menu_active_trail;
    if ($callable_resolver instanceof ControllerResolverInterface) {
      @trigger_error('Calling ' . __METHOD__ . '() with an argument of ControllerResolverInterface is deprecated in drupal:10.2.0 and is removed in drupal:11.0.0. Use \Drupal\Core\Utility\CallableResolver instead. See https://www.drupal.org/node/3395294', E_USER_DEPRECATED);
      $callable_resolver = \Drupal::service('callable_resolver');
    }
    $this->callableResolver = $callable_resolver;
  public function __construct(
    protected MenuTreeStorageInterface $treeStorage,
    protected MenuLinkManagerInterface $menuLinkManager,
    protected RouteProviderInterface $routeProvider,
    protected MenuActiveTrailInterface $menuActiveTrail,
    protected CallableResolver $callableResolver,
  ) {
  }

  /**
+0 −70
Original line number Diff line number Diff line
@@ -6,9 +6,7 @@

use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
@@ -33,74 +31,6 @@ public function testGet() {
    $this->assertSame($factory->get('test'), $factory->get('test'));
  }

  /**
   * @covers ::__construct
   * @group legacy
   */
  public function testConstructorDeprecation() {
    $container = $this->prophesize(ContainerInterface::class);
    $container->get('request_stack')
      ->willReturn($this->prophesize(RequestStack::class)->reveal());
    $container->get('current_user')
      ->willReturn($this->prophesize(AccountProxy::class)->reveal());
    \Drupal::setContainer($container->reveal());

    $this->expectDeprecation('Calling Drupal\Core\Logger\LoggerChannelFactory::__construct without the $requestStack argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354');
    $this->expectDeprecation('Calling Drupal\Core\Logger\LoggerChannelFactory::__construct without the $currentUser argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354');
    new LoggerChannelFactory();
  }

  /**
   * @covers ::get
   * @group legacy
   */
  public function testWithoutConstructor() {
    $container = $this->prophesize(ContainerInterface::class);
    $container->get('request_stack')
      ->willReturn($this->prophesize(RequestStack::class)->reveal());
    $container->get('current_user')
      ->willReturn($this->prophesize(AccountProxy::class)->reveal());
    \Drupal::setContainer($container->reveal());

    $factory = new LoggerChannelWithoutConstructor();

    $this->expectDeprecation('Calling Drupal\Core\Logger\LoggerChannelFactory::get without calling the constructor is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3416354');
    $this->assertSame($factory->get('test'), $factory->get('test'));
  }

  /**
   * @covers ::setContainer
   * @group legacy
   */
  public function testDeprecatedSetContainer() {
    $factory = new LoggerChannelFactory(
      $this->createMock(RequestStack::class),
      $this->createMock(AccountInterface::class),
    );

    $this->expectDeprecation('Calling Drupal\Core\Logger\LoggerChannelFactory::setContainer() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use dependency injection instead. See https://www.drupal.org/node/3416354');
    $factory->setContainer();
  }

  /**
   * @covers ::__get
   * @group legacy
   */
  public function testDeprecatedGetContainer() {
    $factory = new LoggerChannelFactory(
      $this->createMock(RequestStack::class),
      $this->createMock(AccountInterface::class),
    );

    $container = $this->prophesize(ContainerInterface::class);
    $request_stack = $this->prophesize(RequestStack::class)->reveal();
    $container->get('request_stack')->willReturn($request_stack);
    \Drupal::setContainer($container->reveal());

    $this->expectDeprecation('Accessing the container property in Drupal\Core\Logger\LoggerChannelFactory is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use dependency injection instead. See https://www.drupal.org/node/3416354');
    $this->assertSame($request_stack, $factory->container->get('request_stack'));
  }

}

class LoggerChannelWithoutConstructor extends LoggerChannelFactory {
+0 −8
Original line number Diff line number Diff line
@@ -365,12 +365,4 @@ public function testCheckNodeAccess() {
    $this->assertEquals(AccessResult::neutral(), $tree[5]->subtree[6]->access);
  }

  /**
   * @group legacy
   */
  public function testDeprecation(): void {
    $this->expectDeprecation('Calling DefaultMenuLinkTreeManipulators::__construct() without the $module_handler argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3336973');
    new DefaultMenuLinkTreeManipulators($this->accessManager, $this->currentUser, $this->entityTypeManager);
  }

}