Skip to content

Commit

Permalink
bug #36500 [Routing][PrefixTrait] Add the _locale requirement (fancyweb)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Routing][PrefixTrait] Add the _locale requirement

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Commits
-------

9fd62f7 [Routing] Add missing _locale requirements
  • Loading branch information
nicolas-grekas committed Apr 21, 2020
2 parents 2a92dd3 + 9fd62f7 commit 60245d9
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 0 deletions.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouteCompiler;

/**
* @author Nicolas Grekas <p@tchwork.com>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -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);
Expand Down
Expand Up @@ -8,4 +8,6 @@

$add('foo', ['fr' => '/foo']);
$add('bar', ['fr' => '/bar']);

$routes->add('non_localized', '/non-localized');
};
Expand Up @@ -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')));
Expand Down
Expand Up @@ -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'));
}

/**
Expand Down
Expand Up @@ -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()
Expand Down

0 comments on commit 60245d9

Please sign in to comment.