8000 [Routing] Add missing _locale requirements · symfony/symfony@9fd62f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fd62f7

Browse files
[Routing] Add missing _locale requirements
Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
1 parent 95becc4 commit 9fd62f7

File tree

7 files changed

+13
-0
lines changed

7 files changed

+13
-0
lines changed

src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Routing\Route;
1515
use Symfony\Component\Routing\RouteCollection;
16+
use Symfony\Component\Routing\RouteCompiler;
1617

1718
/**
1819
* @author Nicolas Grekas <p@tchwork.com>
@@ -63,6 +64,7 @@ final public function prefix($prefix, bool $trailingSlashOnRoot = true): self
6364
foreach ($prefix as $locale => $localePrefix) {
6465
$localizedRoute = clone $route;
6566
$localizedRoute->setDefault('_locale', $locale);
67+
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
6668
$localizedRoute->setDefault('_canonical_route', $name);
6769
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
6870
$this->route->add($name.'.'.$locale, $localizedRoute);

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
211211
$localizedRoute = clone $route;
212212
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
213213
$localizedRoute->setDefault('_locale', $locale);
214+
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
214215
$localizedRoute->setDefault('_canonical_route', $name);
215216
$subCollection->add($name.'.'.$locale, $localizedRoute);
216217
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
216216
foreach ($prefix as $locale => $localePrefix) {
217217
$localizedRoute = clone $route;
218218
$localizedRoute->setDefault('_locale', $locale);
219+
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
219220
$localizedRoute->setDefault('_canonical_route', $name);
220221
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
221222
$subCollection->add($name.'.'.$locale, $localizedRoute);

src/Symfony/Component/Routing/Tests/Fixtures/php_dsl_sub_i18n.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88

99
$add('foo', ['fr' => '/foo']);
1010
$add('bar', ['fr' => '/bar']);
11+
12+
$routes->add('non_localized', '/non-localized');
1113
};

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public function testRoutingI18nConfigurator()
234234
$expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en'));
235235
$expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr'));
236236
$expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr'));
237+
$expectedCollection->add('non_localized.fr', (new Route('/ench/non-localized'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'non_localized'])->setRequirement('_locale', 'fr'));
237238

238239
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub_i18n.php')));
239240
$expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_i18n.php')));

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public function testLocalizedImportsOfNotLocalizedRoutes()
201201

202202
$this->assertEquals('/le-prefix/suffix', $routeCollection->get('imported.fr')->getPath());
203203
$this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath());
204+
205+
$this->assertSame('fr', $routeCollection->get('imported.fr')->getRequirement('_locale'));
206+
$this->assertSame('en', $routeCollection->get('imported.en')->getRequirement('_locale'));
204207
}
205208

206209
/**

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ public function testImportingNonLocalizedRoutesWithLocales()
334334
$this->assertCount(2, $routes);
335335
$this->assertEquals('/nl/imported', $routes->get('imported.nl')->getPath());
336336
$this->assertEquals('/en/imported', $routes->get('imported.en')->getPath());
337+
338+
$this->assertSame('nl', $routes->get('imported.nl')->getRequirement('_locale'));
339+
$this->assertSame('en', $routes->get('imported.en')->getRequirement('_locale'));
337340
}
338341

339342
public function testImportingRoutesWithOfficialLocales()

0 commit comments

Comments
 (0)
0