Skip to content

Commit 825e2d2

Browse files
committed
[Routing] Add locale requirement for localized routes
1 parent ac1095c commit 825e2d2

File tree

8 files changed

+22
-5
lines changed

8 files changed

+22
-5
lines changed

Loader/AnnotationClassLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
1919
use Symfony\Component\Routing\Route;
2020
use Symfony\Component\Routing\RouteCollection;
21+
use Symfony\Component\Routing\RouteCompiler;
2122

2223
/**
2324
* AnnotationClassLoader loads routing information from a PHP class and its methods.
@@ -211,6 +212,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
211212
$this->configureRoute($route, $class, $method, $annot);
212213
if (0 !== $locale) {
213214
$route->setDefault('_locale', $locale);
215+
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
214216
$route->setDefault('_canonical_route', $name);
215217
$collection->add($name.'.'.$locale, $route);
216218
} else {

Loader/Configurator/Traits/AddTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
1616
use Symfony\Component\Routing\Route;
1717
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Component\Routing\RouteCompiler;
1819

1920
trait AddTrait
2021
{
@@ -67,6 +68,7 @@ final public function add(string $name, $path): RouteConfigurator
6768
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
6869
$this->collection->add($this->name.$name.'.'.$locale, $route);
6970
$route->setDefault('_locale', $locale);
71+
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
7072
$route->setDefault('_canonical_route', $this->name.$name);
7173
}
7274

Loader/XmlFileLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Util\XmlUtils;
1717
use Symfony\Component\Routing\Route;
1818
use Symfony\Component\Routing\RouteCollection;
19+
use Symfony\Component\Routing\RouteCompiler;
1920

2021
/**
2122
* XmlFileLoader loads XML routing files.
@@ -129,6 +130,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
129130
foreach ($paths as $locale => $p) {
130131
$defaults['_locale'] = $locale;
131132
$defaults['_canonical_route'] = $id;
133+
$requirements['_locale'] = preg_quote($locale, RouteCompiler::REGEX_DELIMITER);
132134
$route = new Route($p, $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
133135
$collection->add($id.'.'.$locale, $route);
134136
}

Loader/YamlFileLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Resource\FileResource;
1616
use Symfony\Component\Routing\Route;
1717
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Component\Routing\RouteCompiler;
1819
use Symfony\Component\Yaml\Exception\ParseException;
1920
use Symfony\Component\Yaml\Parser as YamlParser;
2021
use Symfony\Component\Yaml\Yaml;
@@ -140,6 +141,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
140141
foreach ($config['path'] as $locale => $path) {
141142
$localizedRoute = clone $route;
142143
$localizedRoute->setDefault('_locale', $locale);
144+
$localizedRoute->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
143145
$localizedRoute->setDefault('_canonical_route', $name);
144146
$localizedRoute->setPath($path);
145147
$collection->add($name.'.'.$locale, $localizedRoute);

Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public function testLocalizedPathRoutes()
125125
$this->assertCount(2, $routes);
126126
$this->assertEquals('/path', $routes->get('action.en')->getPath());
127127
$this->assertEquals('/pad', $routes->get('action.nl')->getPath());
128+
129+
$this->assertEquals('nl', $routes->get('action.nl')->getRequirement('_locale'));
130+
$this->assertEquals('en', $routes->get('action.en')->getRequirement('_locale'));
128131
}
129132

130133
public function testLocalizedPathRoutesWithExplicitPathPropety()

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ public function testRoutingI18nConfigurator()
229229

230230
$expectedCollection = new RouteCollection();
231231

232-
$expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo']));
233-
$expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar']));
234-
$expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz']));
235-
$expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo']));
236-
$expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar']));
232+
$expectedCollection->add('foo.en', (new Route('/glish/foo'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'foo'])->setRequirement('_locale', 'en'));
233+
$expectedCollection->add('bar.en', (new Route('/glish/bar'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'bar'])->setRequirement('_locale', 'en'));
234+
$expectedCollection->add('baz.en', (new Route('/baz'))->setDefaults(['_locale' => 'en', '_canonical_route' => 'baz'])->setRequirement('_locale', 'en'));
235+
$expectedCollection->add('c_foo.fr', (new Route('/ench/pub/foo'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_foo'])->setRequirement('_locale', 'fr'));
236+
$expectedCollection->add('c_bar.fr', (new Route('/ench/pub/bar'))->setDefaults(['_locale' => 'fr', '_canonical_route' => 'c_bar'])->setRequirement('_locale', 'fr'));
237237

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

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public function testLocalizedImports()
185185

186186
$this->assertEquals('/le-prefix/le-suffix', $routeCollection->get('imported.fr')->getPath());
187187
$this->assertEquals('/the-prefix/suffix', $routeCollection->get('imported.en')->getPath());
188+
189+
$this->assertEquals('fr', $routeCollection->get('imported.fr')->getRequirement('_locale'));
190+
$this->assertEquals('en', $routeCollection->get('imported.en')->getRequirement('_locale'));
188191
}
189192

190193
public function testLocalizedImportsOfNotLocalizedRoutes()

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ public function testImportingRoutesWithLocales()
321321
$this->assertCount(2, $routes);
322322
$this->assertEquals('/nl/voorbeeld', $routes->get('imported.nl')->getPath());
323323
$this->assertEquals('/en/example', $routes->get('imported.en')->getPath());
324+
325+
$this->assertEquals('nl', $routes->get('imported.nl')->getRequirement('_locale'));
326+
$this->assertEquals('en', $routes->get('imported.en')->getRequirement('_locale'));
324327
}
325328

326329
public function testImportingNonLocalizedRoutesWithLocales()

0 commit comments

Comments
 (0)