From bf4cd6164dbd31861ca4e1c61a052d878ce6eae7 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 14 Feb 2019 20:11:38 +0100 Subject: [PATCH] [Routing] deprecate some router options --- .../FrameworkExtension.php | 8 +++-- .../Resources/config/routing.xml | 4 --- src/Symfony/Component/Routing/CHANGELOG.md | 1 + src/Symfony/Component/Routing/Router.php | 35 +++++++++++++------ .../Component/Routing/Tests/RouterTest.php | 30 +++------------- 5 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index cd513ec90a9f6..ee23ac58aac10 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -20,6 +20,7 @@ use Symfony\Bridge\Twig\Extension\CsrfExtension; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader; +use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher; use Symfony\Bundle\FullStack; use Symfony\Component\BrowserKit\Client; use Symfony\Component\Cache\Adapter\AbstractAdapter; @@ -83,6 +84,7 @@ use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; +use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader; use Symfony\Component\Routing\Loader\AnnotationFileLoader; use Symfony\Component\Routing\Matcher\CompiledUrlMatcher; @@ -750,7 +752,7 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co } $container->setParameter('router.resource', $config['resource']); - $container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class')); + $container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class')); // deprecated $router = $container->findDefinition('router.default'); $argument = $router->getArgument(2); $argument['strict_requirements'] = $config['strict_requirements']; @@ -758,9 +760,9 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co $argument['resource_type'] = $config['type']; } if (!class_exists(CompiledUrlMatcher::class)) { - $argument['matcher_class'] = $argument['matcher_base_class']; + $argument['matcher_class'] = $argument['matcher_base_class'] = $argument['matcher_base_class'] ?? RedirectableUrlMatcher::class; $argument['matcher_dumper_class'] = PhpMatcherDumper::class; - $argument['generator_class'] = $argument['generator_base_class']; + $argument['generator_class'] = $argument['generator_base_class'] = $argument['generator_base_class'] ?? UrlGenerator::class; $argument['generator_dumper_class'] = PhpGeneratorDumper::class; } $router->replaceArgument(2, $argument); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index bdce1956a3015..a240dd85eb62c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -60,13 +60,9 @@ %kernel.cache_dir% %kernel.debug% Symfony\Component\Routing\Generator\CompiledUrlGenerator - Symfony\Component\Routing\Generator\UrlGenerator Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper - %router.cache_class_prefix%UrlGenerator Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher - Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper - %router.cache_class_prefix%UrlMatcher diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index f9755971044db..d695cd6bd1e91 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * added `CompiledUrlMatcher` and `CompiledUrlMatcherDumper` * added `CompiledUrlGenerator` and `CompiledUrlGeneratorDumper` * deprecated `PhpGeneratorDumper` and `PhpMatcherDumper` + * deprecated `generator_base_class`, `generator_cache_class`, `matcher_base_class` and `matcher_cache_class` router options 4.2.0 ----- diff --git a/src/Symfony/Component/Routing/Router.php b/src/Symfony/Component/Routing/Router.php index 35c2cf679d21d..34c4d9ce23ba1 100644 --- a/src/Symfony/Component/Routing/Router.php +++ b/src/Symfony/Component/Routing/Router.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Routing; use Psr\Log\LoggerInterface; +use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher; use Symfony\Component\Config\ConfigCacheFactory; use Symfony\Component\Config\ConfigCacheFactoryInterface; use Symfony\Component\Config\ConfigCacheInterface; @@ -113,13 +114,9 @@ public function __construct(LoaderInterface $loader, $resource, array $options = * * cache_dir: The cache directory (or null to disable caching) * * debug: Whether to enable debugging or not (false by default) * * generator_class: The name of a UrlGeneratorInterface implementation - * * generator_base_class: The base class for the dumped generator class - * * generator_cache_class: The class name for the dumped generator class * * generator_dumper_class: The name of a GeneratorDumperInterface implementation * * matcher_class: The name of a UrlMatcherInterface implementation - * * matcher_base_class: The base class for the dumped matcher class - * * matcher_dumper_class: The class name for the dumped matcher class - * * matcher_cache_class: The name of a MatcherDumperInterface implementation + * * matcher_dumper_class: The name of a MatcherDumperInterface implementation * * resource_type: Type hint for the main resource (optional) * * strict_requirements: Configure strict requirement checking for generators * implementing ConfigurableRequirementsInterface (default is true) @@ -134,13 +131,13 @@ public function setOptions(array $options) 'cache_dir' => null, 'debug' => false, 'generator_class' => CompiledUrlGenerator::class, - 'generator_base_class' => UrlGenerator::class, + 'generator_base_class' => UrlGenerator::class, // deprecated 'generator_dumper_class' => CompiledUrlGeneratorDumper::class, - 'generator_cache_class' => 'UrlGenerator', + 'generator_cache_class' => 'UrlGenerator', // deprecated 'matcher_class' => CompiledUrlMatcher::class, - 'matcher_base_class' => UrlMatcher::class, + 'matcher_base_class' => UrlMatcher::class, // deprecated 'matcher_dumper_class' => CompiledUrlMatcherDumper::class, - 'matcher_cache_class' => 'UrlMatcher', + 'matcher_cache_class' => 'UrlMatcher', // deprecated 'resource_type' => null, 'strict_requirements' => true, ]; @@ -148,6 +145,7 @@ public function setOptions(array $options) // check option names and live merge, if errors are encountered Exception will be thrown $invalid = []; foreach ($options as $key => $value) { + $this->checkDeprecatedOption($key); if (array_key_exists($key, $this->options)) { $this->options[$key] = $value; } else { @@ -174,6 +172,8 @@ public function setOption($key, $value) throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key)); } + $this->checkDeprecatedOption($key); + $this->options[$key] = $value; } @@ -192,6 +192,8 @@ public function getOption($key) throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key)); } + $this->checkDeprecatedOption($key); + return $this->options[$key]; } @@ -279,7 +281,7 @@ public function getMatcher() return $this->matcher; } - $compiled = is_a($this->options['matcher_class'], CompiledUrlMatcher::class, true); + $compiled = is_a($this->options['matcher_class'], CompiledUrlMatcher::class, true) && (UrlMatcher::class === $this->options['matcher_base_class'] || RedirectableUrlMatcher::class === $this->options['matcher_base_class']); if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) { $routes = $this->getRouteCollection(); @@ -336,7 +338,7 @@ public function getGenerator() return $this->generator; } - $compiled = is_a($this->options['generator_class'], CompiledUrlGenerator::class, true); + $compiled = is_a($this->options['generator_class'], CompiledUrlGenerator::class, true) && UrlGenerator::class === $this->options['generator_base_class']; if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) { $routes = $this->getRouteCollection(); @@ -411,4 +413,15 @@ private function getConfigCacheFactory() return $this->configCacheFactory; } + + private function checkDeprecatedOption($key) + { + switch ($key) { + case 'generator_base_class': + case 'generator_cache_class': + case 'matcher_base_class': + case 'matcher_cache_class': + @trigger_error(sprintf('Option "%s" given to router %s is deprecated since Symfony 4.3.', $key, static::class), E_USER_DEPRECATED); + } + } } diff --git a/src/Symfony/Component/Routing/Tests/RouterTest.php b/src/Symfony/Component/Routing/Tests/RouterTest.php index 589ce5403ac2b..46a45fef082e1 100644 --- a/src/Symfony/Component/Routing/Tests/RouterTest.php +++ b/src/Symfony/Component/Routing/Tests/RouterTest.php @@ -93,12 +93,9 @@ public function testThatRouteCollectionIsLoaded() $this->assertSame($routeCollection, $this->router->getRouteCollection()); } - /** - * @dataProvider provideMatcherOptionsPreventingCaching - */ - public function testMatcherIsCreatedIfCacheIsNotConfigured($option) + public function testMatcherIsCreatedIfCacheIsNotConfigured() { - $this->router->setOption($option, null); + $this->router->setOption('cache_dir', null); $this->loader->expects($this->once()) ->method('load')->with('routing.yml', null) @@ -107,20 +104,9 @@ public function testMatcherIsCreatedIfCacheIsNotConfigured($option) $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher()); } - public function provideMatcherOptionsPreventingCaching() - { - return [ - ['cache_dir'], - ['matcher_cache_class'], - ]; - } - - /** - * @dataProvider provideGeneratorOptionsPreventingCaching - */ - public function testGeneratorIsCreatedIfCacheIsNotConfigured($option) + public function testGeneratorIsCreatedIfCacheIsNotConfigured() { - $this->router->setOption($option, null); + $this->router->setOption('cache_dir', null); $this->loader->expects($this->once()) ->method('load')->with('routing.yml', null) @@ -129,14 +115,6 @@ public function testGeneratorIsCreatedIfCacheIsNotConfigured($option) $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator()); } - public function provideGeneratorOptionsPreventingCaching() - { - return [ - ['cache_dir'], - ['generator_cache_class'], - ]; - } - public function testMatchRequestWithUrlMatcherInterface() { $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy