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

Skip to content

Commit 9ad7aaf

Browse files
committed
[Routing] deprecate some router options
1 parent 6a80e7d commit 9ad7aaf

File tree

5 files changed

+35
-44
lines changed

5 files changed

+35
-44
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@
8383
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
8484
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
8585
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
86+
use Symfony\Component\Routing\Generator\UrlGenerator;
8687
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
8788
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
8889
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
8990
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
91+
use Symfony\Component\Routing\Matcher\UrlMatcher;
9092
use Symfony\Component\Security\Core\Security;
9193
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
9294
use Symfony\Component\Serializer\Encoder\DecoderInterface;
@@ -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'] ?? UrlMatcher::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: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,9 @@ public function __construct(LoaderInterface $loader, $resource, array $options =
113113
* * cache_dir: The cache directory (or null to disable caching)
114114
* * debug: Whether to enable debugging or not (false by default)
115115
* * 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
118116
* * generator_dumper_class: The name of a GeneratorDumperInterface implementation
119117
* * 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
118+
* * matcher_dumper_class: The name of a MatcherDumperInterface implementation
123119
* * resource_type: Type hint for the main resource (optional)
124120
* * strict_requirements: Configure strict requirement checking for generators
125121
* implementing ConfigurableRequirementsInterface (default is true)
@@ -134,20 +130,21 @@ public function setOptions(array $options)
134130
'cache_dir' => null,
135131
'debug' => false,
136132
'generator_class' => CompiledUrlGenerator::class,
137-
'generator_base_class' => UrlGenerator::class,
133+
'generator_base_class' => UrlGenerator::class, // deprecated
138134
'generator_dumper_class' => CompiledUrlGeneratorDumper::class,
139-
'generator_cache_class' => 'UrlGenerator',
135+
'generator_cache_class' => 'UrlGenerator', // deprecated
140136
'matcher_class' => CompiledUrlMatcher::class,
141-
'matcher_base_class' => UrlMatcher::class,
137+
'matcher_base_class' => UrlMatcher::class, // deprecated
142138
'matcher_dumper_class' => CompiledUrlMatcherDumper::class,
143-
'matcher_cache_class' => 'UrlMatcher',
139+
'matcher_cache_class' => 'UrlMatcher', // deprecated
144140
'resource_type' => null,
145141
'strict_requirements' => true,
146142
];
147143

148144
// check option names and live merge, if errors are encountered Exception will be thrown
149145
$invalid = [];
150146
foreach ($options as $key => $value) {
147+
$this->checkDeprecatedOption($key);
151148
if (array_key_exists($key, $this->options)) {
152149
$this->options[$key] = $value;
153150
} else {
@@ -160,6 +157,8 @@ public function setOptions(array $options)
160157
}
161158
}
162159

160+
161+
163162
/**
164163
* Sets an option.
165164
*
@@ -174,6 +173,8 @@ public function setOption($key, $value)
174173
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
175174
}
176175

176+
$this->checkDeprecatedOption($key);
177+
177178
$this->options[$key] = $value;
178179
}
179180

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

196+
$this->checkDeprecatedOption($key);
197+
195198
return $this->options[$key];
196199
}
197200

@@ -279,7 +282,7 @@ public function getMatcher()
279282
return $this->matcher;
280283
}
281284

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

284287
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
285288
$routes = $this->getRouteCollection();
@@ -336,7 +339,7 @@ public function getGenerator()
336339
return $this->generator;
337340
}
338341

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

341344
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
342345
$routes = $this->getRouteCollection();
@@ -411,4 +414,15 @@ private function getConfigCacheFactory()
411414

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

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)