From 9fd62f79fb20ffa660fe99096471437f032ba0e0 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Mon, 20 Apr 2020 14:15:01 +0200 Subject: [PATCH] [Routing] Add missing _locale requirements Co-authored-by: Nicolas Grekas --- .../Routing/Loader/Configurator/ImportConfigurator.php | 2 ++ src/Symfony/Component/Routing/Loader/XmlFileLoader.php | 1 + src/Symfony/Component/Routing/Loader/YamlFileLoader.php | 1 + .../Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php | 2 ++ .../Component/Routing/Tests/Loader/PhpFileLoaderTest.php | 1 + .../Component/Routing/Tests/Loader/XmlFileLoaderTest.php | 3 +++ .../Component/Routing/Tests/Loader/YamlFileLoaderTest.php | 3 +++ 7 files changed, 13 insertions(+) diff --git a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php index f11b7957525b..0059a632a197 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php @@ -13,6 +13,7 @@ use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouteCompiler; /** * @author Nicolas Grekas @@ -63,6 +64,7 @@ final public function prefix($prefix, bool $trailingSlashOnRoot = true): self foreach ($prefix as $locale => $localePrefix) { $localizedRoute = clone $route; $localizedRoute->setDefault('_locale', $locale); + $localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $localizedRoute->setDefault('_canonical_route', $name); $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath())); $this->route->add($name.'.'.$locale, $localizedRoute); diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 49b552d627c9..31c614950e41 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -211,6 +211,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $ $localizedRoute = clone $route; $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath())); $localizedRoute->setDefault('_locale', $locale); + $localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $localizedRoute->setDefault('_canonical_route', $name); $subCollection->add($name.'.'.$locale, $localizedRoute); } diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index 868da9bd30b4..8d4b9abdb10f 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -216,6 +216,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path foreach ($prefix as $locale => $localePrefix) { $localizedRoute = clone $route; $localizedRoute->setDefault('_locale', $locale); + $localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER)); $localizedRoute->setDefault('_canonical_route', $name); $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath())); $subCollection->add($name.'.'.$locale, $localizedRoute); diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php b/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php index e79edc869d2d..e6d846dd463c 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php @@ -8,4 +8,6 @@ $add('foo', ['fr' => '/foo']); $add('bar', ['fr' => '/bar']); + + $routes->add('non_localized', '/non-localized'); }; diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index 789848c66021..b84d5ff3543b 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -234,6 +234,7 @@ public function testRoutingI18nConfigurator() $expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en')); $expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr')); $expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr')); + $expectedCollection->add('non_localized.fr', (new Route('/ench/non-localized'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'non_localized'])->setRequirement('_locale', 'fr')); $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php'))); $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php'))); diff --git a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php index 02e1e14d4aa9..383cda017695 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -201,6 +201,9 @@ public function testLocalizedImportsOfNotLocalizedRoutes() $this->assertEquals('/le-prefix/suffix', $routeCollection->get('imported.fr')->getPath()); $this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath()); + + $this->assertSame('fr', $routeCollection->get('imported.fr')->getRequirement('_locale')); + $this->assertSame('en', $routeCollection->get('imported.en')->getRequirement('_locale')); } /** diff --git a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php index 3878663e6b01..e5571b0b7a6e 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -334,6 +334,9 @@ public function testImportingNonLocalizedRoutesWithLocales() $this->assertCount(2, $routes); $this->assertEquals('/nl/imported', $routes->get('imported.nl')->getPath()); $this->assertEquals('/en/imported', $routes->get('imported.en')->getPath()); + + $this->assertSame('nl', $routes->get('imported.nl')->getRequirement('_locale')); + $this->assertSame('en', $routes->get('imported.en')->getRequirement('_locale')); } public function testImportingRoutesWithOfficialLocales()