Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf4cd61

Browse files
committed
[Routing] deprecate some router options
1 parent a617006 commit bf4cd61

File tree

5 files changed

+34
-44
lines changed

5 files changed

+34
-44
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Bridge\Twig\Extension\CsrfExtension;
2121
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2222
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
23+
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
2324
use Symfony\Bundle\FullStack;
2425
use Symfony\Component\BrowserKit\Client;
2526
use Symfony\Component\Cache\Adapter\AbstractAdapter;
@@ -83,6 +84,7 @@
8384
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
8485
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
8586
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
87+
use Symfony\Component\Routing\Generator\UrlGenerator;
8688
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
8789
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
8890
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
@@ -750,17 +752,17 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
750752
}
751753

752754
$container->setParameter('router.resource', $config['resource']);
753-
$container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class'));
755+
$container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class')); // deprecated
754756
$router = $container->findDefinition('router.default');
755757
$argument = $router->getArgument(2);
756758
$argument['strict_requirements'] = $config['strict_requirements'];
757759
if (isset($config['type'])) {
758760
$argument['resource_type'] = $config['type'];
759761
}
760762
if (!class_exists(CompiledUrlMatcher::class)) {
761-
$argument['matcher_class'] = $argument['matcher_base_class'];
763+
$argument['matcher_class'] = $argument['matcher_base_class'] = $argument['matcher_base_class'] ?? RedirectableUrlMatcher::class;
762764
$argument['matcher_dumper_class'] = PhpMatcherDumper::class;
763-
$argument['generator_class'] = $argument['generator_base_class'];
765+
$argument['generator_class'] = $argument['generator_base_class'] = $argument['generator_base_class'] ?? UrlGenerator::class;
764766
$argument['generator_dumper_class'] = PhpGeneratorDumper::class;
765767
}
766768
$router->replaceArgument(2, $argument);

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@
6060
<argument key="cache_dir">%kernel.cache_dir%</argument>
6161
<argument key="debug">%kernel.debug%</argument>
6262
<argument key="generator_class">Symfony\Component\Routing\Generator\CompiledUrlGenerator</argument>
63-
<argument key="generator_base_class">Symfony\Component\Routing\Generator\UrlGenerator</argument>
6463
<argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper</argument>
65-
<argument key="generator_cache_class">%router.cache_class_prefix%UrlGenerator</argument>
6664
<argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher</argument>
67-
<argument key="matcher_base_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher</argument>
6865
<argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper</argument>
69-
<argument key="matcher_cache_class">%router.cache_class_prefix%UrlMatcher</argument>
7066
</argument>
7167
<argument type="service" id="router.request_context" on-invalid="ignore" />
7268
<argument type="service" id="parameter_bag" on-invalid="ignore" />

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added `CompiledUrlMatcher` and `CompiledUrlMatcherDumper`
88
* added `CompiledUrlGenerator` and `CompiledUrlGeneratorDumper`
99
* deprecated `PhpGeneratorDumper` and `PhpMatcherDumper`
10+
* deprecated `generator_base_class`, `generator_cache_class`, `matcher_base_class` and `matcher_cache_class` router options
1011

1112
4.2.0
1213
-----

src/Symfony/Component/Routing/Router.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
1516
use Symfony\Component\Config\ConfigCacheFactory;
1617
use Symfony\Component\Config\ConfigCacheFactoryInterface;
1718
use Symfony\Component\Config\ConfigCacheInterface;
@@ -113,13 +114,9 @@ public function __construct(LoaderInterface $loader, $resource, array $options =
113114
* * cache_dir: The cache directory (or null to disable caching)
114115
* * debug: Whether to enable debugging or not (false by default)
115116
* * generator_class: The name of a UrlGeneratorInterface implementation
116-
* * generator_base_class: The base class for the dumped generator class
117-
* * generator_cache_class: The class name for the dumped generator class
118117
* * generator_dumper_class: The name of a GeneratorDumperInterface implementation
119118
* * matcher_class: The name of a UrlMatcherInterface implementation
120-
* * matcher_base_class: The base class for the dumped matcher class
121-
* * matcher_dumper_class: The class name for the dumped matcher class
122-
* * matcher_cache_class: The name of a MatcherDumperInterface implementation
119+
* * matcher_dumper_class: The name of a MatcherDumperInterface implementation
123120
* * resource_type: Type hint for the main resource (optional)
124121
* * strict_requirements: Configure strict requirement checking for generators
125122
* implementing ConfigurableRequirementsInterface (default is true)
@@ -134,20 +131,21 @@ public function setOptions(array $options)
134131
'cache_dir' => null,
135132
'debug' => false,
136133
'generator_class' => CompiledUrlGenerator::class,
137-
'generator_base_class' => UrlGenerator::class,
134+
'generator_base_class' => UrlGenerator::class, // deprecated
138135
'generator_dumper_class' => CompiledUrlGeneratorDumper::class,
139-
'generator_cache_class' => 'UrlGenerator',
136+
'generator_cache_class' => 'UrlGenerator', // deprecated
140137
'matcher_class' => CompiledUrlMatcher::class,
141-
'matcher_base_class' => UrlMatcher::class,
138+
'matcher_base_class' => UrlMatcher::class, // deprecated
142139
'matcher_dumper_class' => CompiledUrlMatcherDumper::class,
143-
'matcher_cache_class' => 'UrlMatcher',
140+
'matcher_cache_class' => 'UrlMatcher', // deprecated
144141
'resource_type' => null,
145142
'strict_requirements' => true,
146143
];
147144

148145
// check option names and live merge, if errors are encountered Exception will be thrown
149146
$invalid = [];
150147
foreach ($options as $key => $value) {
148+
$this->checkDeprecatedOption($key);
151149
if (array_key_exists($key, $this->options)) {
152150
$this->options[$key] = $value;
153151
} else {
@@ -174,6 +172,8 @@ public function setOption($key, $value)
174172
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
175173
}
176174

175+
$this->checkDeprecatedOption($key);
176+
177177
$this->options[$key] = $value;
178178
}
179179

@@ -192,6 +192,8 @@ public function getOption($key)
192192
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
193193
}
194194

195+
$this->checkDeprecatedOption($key);
196+
195197
return $this->options[$key];
196198
}
197199

@@ -279,7 +281,7 @@ public function getMatcher()
279281
return $this->matcher;
280282
}
281283

282-
$compiled = is_a($this->options['matcher_class'], CompiledUrlMatcher::class, true);
284+
$compiled = is_a($this->options['matcher_class'], CompiledUrlMatcher::class, true) && (UrlMatcher::class === $this->options['matcher_base_class'] || RedirectableUrlMatcher::class === $this->options['matcher_base_class']);
283285

284286
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
285287
$routes = $this->getRouteCollection();
@@ -336,7 +338,7 @@ public function getGenerator()
336338
return $this->generator;
337339
}
338340

339-
$compiled = is_a($this->options['generator_class'], CompiledUrlGenerator::class, true);
341+
$compiled = is_a($this->options['generator_class'], CompiledUrlGenerator::class, true) && UrlGenerator::class === $this->options['generator_base_class'];
340342

341343
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
342344
$routes = $this->getRouteCollection();
@@ -411,4 +413,15 @@ private function getConfigCacheFactory()
411413

412414
return $this->configCacheFactory;
413415
}
416+
417+
private function checkDeprecatedOption($key)
418+
{
419+
switch ($key) {
420+
case 'generator_base_class':
421+
case 'generator_cache_class':
422+
case 'matcher_base_class':
423+
case 'matcher_cache_class':
424+
@trigger_error(sprintf('Option "%s" given to router %s is deprecated since Symfony 4.3.', $key, static::class), E_USER_DEPRECATED);
425+
}
426+
}
414427
}

src/Symfony/Component/Routing/Tests/RouterTest.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ public function testThatRouteCollectionIsLoaded()
9393
$this->assertSame($routeCollection, $this->router->getRouteCollection());
9494
}
9595

96-
/**
97-
* @dataProvider provideMatcherOptionsPreventingCaching
98-
*/
99-
public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
96+
public function testMatcherIsCreatedIfCacheIsNotConfigured()
10097
{
101-
$this->router->setOption($option, null);
98+
$this->router->setOption('cache_dir', null);
10299

103100
$this->loader->expects($this->once())
104101
->method('load')->with('routing.yml', null)
@@ -107,20 +104,9 @@ public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
107104
$this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
108105
}
109106

110-
public function provideMatcherOptionsPreventingCaching()
111-
{
112-
return [
113-
['cache_dir'],
114-
['matcher_cache_class'],
115-
];
116-
}
117-
118-
/**
119-
* @dataProvider provideGeneratorOptionsPreventingCaching
120-
*/
121-
public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
107+
public function testGeneratorIsCreatedIfCacheIsNotConfigured()
122108
{
123-
$this->router->setOption($option, null);
109+
$this->router->setOption('cache_dir', null);
124110

125111
$this->loader->expects($this->once())
126112
->method('load')->with('routing.yml', null)
@@ -129,14 +115,6 @@ public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
129115
$this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
130116
}
131117

132-
public function provideGeneratorOptionsPreventingCaching()
133-
{
134-
return [
135-
['cache_dir'],
136-
['generator_cache_class'],
137-
];
138-
}
139-
140118
public function testMatchRequestWithUrlMatcherInterface()
141119
{
142120
$matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();

0 commit comments

Comments
 (0)