Skip to content

Commit f863bdf

Browse files
Merge branch '4.4' into 5.0
* 4.4: (25 commits) [DoctrineBridge] Use new Types::* constants and support new json type [Debug][ErrorHandler] improved deprecation notices for methods new args and return type [BrowserKit] Nested file array prevents uploading file [ExpressionLanguage] Fixed collisions of character operators with object properties [Validator] Remove specific check for Valid targets [PhpUnitBridge] Use trait instead of extending deprecated class Fix versioned namespace clears fix remember me Use strict assertion in asset tests [DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types Do not rely on the current locale when dumping a Graphviz object fix typo [Ldap] force default network timeout [Config] don't throw on missing excluded paths Docs: Typo, grammar [Validator] Add the missing translations for the Polish ("pl") locale [PhpUnitBridge] Add compatibility to PHPUnit 9 #35662 [Routing] Add locale requirement for localized routes [Console] Inline exact-match handling with 4.4 Set previous exception when rethrown from controller resolver ...
2 parents 9d187e7 + 825e2d2 commit f863bdf

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.
@@ -207,6 +208,7 @@ protected function addRoute(RouteCollection $collection, $annot, array $globals,
207208
$this->configureRoute($route, $class, $method, $annot);
208209
if (0 !== $locale) {
209210
$route->setDefault('_locale', $locale);
211+
$route->setRequirement('_locale', preg_quote($locale, RouteCompiler::REGEX_DELIMITER));
210212
$route->setDefault('_canonical_route', $name);
211213
$collection->add($name.'.'.$locale, $route);
212214
} 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, st
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, string $name, array $
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
@@ -121,6 +121,9 @@ public function testLocalizedPathRoutes()
121121
$this->assertCount(2, $routes);
122122
$this->assertEquals('/path', $routes->get('action.en')->getPath());
123123
$this->assertEquals('/pad', $routes->get('action.nl')->getPath());
124+
125+
$this->assertEquals('nl', $routes->get('action.nl')->getRequirement('_locale'));
126+
$this->assertEquals('en', $routes->get('action.en')->getRequirement('_locale'));
124127
}
125128

126129
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)