Skip to content

Commit

Permalink
[Routing] Improve localized routes performances
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Feb 25, 2020
1 parent 1024f5f commit 7d6e446
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/Routing/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public static function compile(Route $route)
$hostRegex = $result['regex'];
}

$locale = $route->getDefault('_locale');
if (null !== $locale && null !== $route->getDefault('_canonical_route') && preg_quote($locale, self::REGEX_DELIMITER) === $route->getRequirement('_locale')) {
$requirements = $route->getRequirements();
unset($requirements['_locale']);
$route->setRequirements($requirements);
$route->setPath(str_replace('{_locale}', $locale, $route->getPath()));
}

$path = $route->getPath();

$result = self::compilePattern($route, $path, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* This file has been auto-generated
* by the Symfony Routing Component.
*/

return [
false, // $matchHost
[ // $staticRoutes
'/fr/accueil' => [[['_route' => 'home', '_locale' => 'fr'], null, null, null, false, false, null]],
'/en/home' => [[['_route' => 'home', '_locale' => 'en'], null, null, null, false, false, null]],
],
[ // $regexpList
],
[ // $dynamicRoutes
],
null, // $checkCondition
];
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,27 @@ public function getRouteCollections()
$hostCollection->add('r1', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
$hostCollection->add('r2', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));

/* test case 14 */
$fixedLocaleCollection = new RouteCollection();
$fixedLocaleCollection->add('home.fr', new Route('/{_locale}/accueil', ['_locale' => 'fr', '_canonical_route' => 'home'], ['_locale' => 'fr']));
$fixedLocaleCollection->add('home.en', new Route('/{_locale}/home', ['_locale' => 'en', '_canonical_route' => 'home'], ['_locale' => 'en']));

return [
[new RouteCollection(), 'compiled_url_matcher0.php'],
[$collection, 'compiled_url_matcher1.php'],
[$redirectCollection, 'compiled_url_matcher2.php'],
[$rootprefixCollection, 'compiled_url_matcher3.php'],
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
[$unicodeCollection, 'compiled_url_matcher8.php'],
[$hostTreeCollection, 'compiled_url_matcher9.php'],
[$chunkedCollection, 'compiled_url_matcher10.php'],
[$demoCollection, 'compiled_url_matcher11.php'],
[$suffixCollection, 'compiled_url_matcher12.php'],
[$hostCollection, 'compiled_url_matcher13.php'],
[new RouteCollection(), 'compiled_url_matcher0.php'],
[$collection, 'compiled_url_matcher1.php'],
[$redirectCollection, 'compiled_url_matcher2.php'],
[$rootprefixCollection, 'compiled_url_matcher3.php'],
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
[$unicodeCollection, 'compiled_url_matcher8.php'],
[$hostTreeCollection, 'compiled_url_matcher9.php'],
[$chunkedCollection, 'compiled_url_matcher10.php'],
[$demoCollection, 'compiled_url_matcher11.php'],
[$suffixCollection, 'compiled_url_matcher12.php'],
[$hostCollection, 'compiled_url_matcher13.php'],
[$fixedLocaleCollection, 'compiled_url_matcher14.php'],
];
}

Expand Down

0 comments on commit 7d6e446

Please sign in to comment.