Skip to content

Commit

Permalink
[Routing] deprecate some router options
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Feb 15, 2019
1 parent a617006 commit a4e592f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -750,17 +752,17 @@ 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'];
if (isset($config['type'])) {
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@
<argument key="cache_dir">%kernel.cache_dir%</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="generator_class">Symfony\Component\Routing\Generator\CompiledUrlGenerator</argument>
<argument key="generator_base_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper</argument>
<argument key="generator_cache_class">%router.cache_class_prefix%UrlGenerator</argument>
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher</argument>
<argument key="matcher_base_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper</argument>
<argument key="matcher_cache_class">%router.cache_class_prefix%UrlMatcher</argument>
</argument>
<argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="parameter_bag" on-invalid="ignore" />
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
34 changes: 23 additions & 11 deletions src/Symfony/Component/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,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)
Expand All @@ -134,20 +130,21 @@ 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,
];

// 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 {
Expand All @@ -174,6 +171,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;
}

Expand All @@ -192,6 +191,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];
}

Expand Down Expand Up @@ -279,7 +280,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'];

if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
$routes = $this->getRouteCollection();
Expand Down Expand Up @@ -336,7 +337,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();
Expand Down Expand Up @@ -411,4 +412,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);
}
}
}
30 changes: 4 additions & 26 deletions src/Symfony/Component/Routing/Tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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();
Expand Down

0 comments on commit a4e592f

Please sign in to comment.