diff --git a/core/core.services.yml b/core/core.services.yml index 6655001af19..552d541f4b8 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1019,9 +1019,9 @@ services: arguments: ['@router.route_provider', '@path.current', '@url_generator'] tags: # @todo Try to combine those tags together, see https://www.drupal.org/node/2915772. - - { name: service_collector, tag: non_lazy_route_enhancer, call: addRouteEnhancer } + - { name: service_collector, tag: non_lazy_route_enhancer, call: addDeprecatedRouteEnhancer } - { name: service_collector, tag: route_enhancer, call: addRouteEnhancer } - - { name: service_collector, tag: non_lazy_route_filter, call: addRouteFilter } + - { name: service_collector, tag: non_lazy_route_filter, call: addDeprecatedRouteFilter } - { name: service_collector, tag: route_filter, call: addRouteFilter } calls: - [setContext, ['@router.request_context']] diff --git a/core/lib/Drupal/Core/Routing/Router.php b/core/lib/Drupal/Core/Routing/Router.php index 94eaf5bf7bb..2480ac9ea7b 100644 --- a/core/lib/Drupal/Core/Routing/Router.php +++ b/core/lib/Drupal/Core/Routing/Router.php @@ -84,6 +84,22 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf $this->filters[] = $route_filter; } + /** + * Adds a deprecated route filter. + * + * @param \Drupal\Core\Routing\FilterInterface $route_filter + * The route filter. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use + * route_filter instead. + * + * @see https://www.drupal.org/node/2894934 + */ + public function addDeprecatedRouteFilter(FilterInterface $route_filter) { + @trigger_error('non_lazy_route_filter is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use route_filter instead. See https://www.drupal.org/node/2894934', E_USER_DEPRECATED); + $this->filters[] = $route_filter; + } + /** * Adds a route enhancer. * @@ -94,6 +110,22 @@ class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterf $this->enhancers[] = $route_enhancer; } + /** + * Adds a deprecated route enhancer. + * + * @param \Drupal\Core\Routing\EnhancerInterface $route_enhancer + * The route enhancer. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use + * route_enhancer instead. + * + * @see https://www.drupal.org/node/2894934 + */ + public function addDeprecatedRouteEnhancer(EnhancerInterface $route_enhancer) { + @trigger_error('non_lazy_route_enhancer is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use route_enhancer instead. See https://www.drupal.org/node/2894934', E_USER_DEPRECATED); + $this->enhancers[] = $route_enhancer; + } + /** * {@inheritdoc} */ diff --git a/core/tests/Drupal/Tests/Core/Routing/RouterUnsupportedTest.php b/core/tests/Drupal/Tests/Core/Routing/RouterUnsupportedTest.php index c28ee30aa14..458654e0e4f 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RouterUnsupportedTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RouterUnsupportedTest.php @@ -3,6 +3,8 @@ namespace Drupal\Tests\Core\Routing; use Drupal\Core\Path\CurrentPathStack; +use Drupal\Core\Routing\EnhancerInterface; +use Drupal\Core\Routing\FilterInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Routing\Router; use Drupal\Core\Routing\UrlGeneratorInterface; @@ -33,4 +35,31 @@ class RouterUnsupportedTest extends UnitTestCase { $router->generate($route_name); } + /** + * @covers ::addDeprecatedRouteFilter + * @covers ::addDeprecatedRouteEnhancer + */ + public function testDeprecatedAdd() { + // Test needs access to router's protected properties. + $filters = new \ReflectionProperty(Router::class, 'filters'); + $filters->setAccessible(TRUE); + $enhancers = new \ReflectionProperty(Router::class, 'enhancers'); + $enhancers->setAccessible(TRUE); + + $route_provider = $this->prophesize(RouteProviderInterface::class); + $current_path_stack = $this->prophesize(CurrentPathStack::class); + $url_generator = $this->prophesize(UrlGeneratorInterface::class); + $router = new Router($route_provider->reveal(), $current_path_stack->reveal(), $url_generator->reveal()); + + $this->expectDeprecation('non_lazy_route_filter is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use route_filter instead. See https://www.drupal.org/node/2894934'); + $filter = $this->prophesize(FilterInterface::class)->reveal(); + $router->addDeprecatedRouteFilter($filter); + $this->assertSame($filter, $filters->getValue($router)[0]); + + $this->expectDeprecation('non_lazy_route_enhancer is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use route_enhancer instead. See https://www.drupal.org/node/2894934'); + $enhancer = $this->prophesize(EnhancerInterface::class)->reveal(); + $router->addDeprecatedRouteEnhancer($enhancer); + $this->assertSame($enhancer, $enhancers->getValue($router)[0]); + } + }