Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ef95f2e

Browse filesBrowse files
minor #35855 [Routing] Improve localized routes performances (mtarld)
This PR was merged into the 4.4 branch. Discussion ---------- [Routing] Improve localized routes performances | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | License | MIT Implementation of the following idea: #35735 (review) Improve route matching performances by turning dynamic routes with fixed `_locale` to actual static routes. Commits ------- 8e9eafe [Routing] Improve localized routes performances
2 parents 88b96ab + 8e9eafe commit ef95f2e
Copy full SHA for ef95f2e

File tree

3 files changed

+57
-14
lines changed
Filter options

3 files changed

+57
-14
lines changed

‎src/Symfony/Component/Routing/RouteCompiler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/RouteCompiler.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public static function compile(Route $route)
6161
$hostRegex = $result['regex'];
6262
}
6363

64+
$locale = $route->getDefault('_locale');
65+
if (null !== $locale && null !== $route->getDefault('_canonical_route') && preg_quote($locale, self::REGEX_DELIMITER) === $route->getRequirement('_locale')) {
66+
$requirements = $route->getRequirements();
67+
unset($requirements['_locale']);
68+
$route->setRequirements($requirements);
69+
$route->setPath(str_replace('{_locale}', $locale, $route->getPath()));
70+
}
71+
6472
$path = $route->getPath();
6573

6674
$result = self::compilePattern($route, $path, false);
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file has been auto-generated
5+
* by the Symfony Routing Component.
6+
*/
7+
8+
return [
9+
false, // $matchHost
10+
[ // $staticRoutes
11+
'/fr/accueil' => [[['_route' => 'home', '_locale' => 'fr'], null, null, null, false, false, null]],
12+
'/en/home' => [[['_route' => 'home', '_locale' => 'en'], null, null, null, false, false, null]],
13+
],
14+
[ // $regexpList
15+
],
16+
[ // $dynamicRoutes
17+
],
18+
null, // $checkCondition
19+
];

‎src/Symfony/Component/Routing/Tests/Matcher/Dumper/CompiledUrlMatcherDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Matcher/Dumper/CompiledUrlMatcherDumperTest.php
+30-14Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
17+
use Symfony\Component\Routing\Loader\PhpFileLoader;
1518
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
1619
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
1720
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
@@ -442,21 +445,34 @@ public function getRouteCollections()
442445
$hostCollection->add('r1', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
443446
$hostCollection->add('r2', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
444447

448+
/* test case 14 */
449+
$fixedLocaleCollection = new RouteCollection();
450+
$routes = new RoutingConfigurator($fixedLocaleCollection, new PhpFileLoader(new FileLocator()), __FILE__, __FILE__);
451+
$routes
452+
->collection()
453+
->prefix('/{_locale}')
454+
->add('home', [
455+
'fr' => 'accueil',
456+
'en' => 'home',
457+
])
458+
;
459+
445460
return [
446-
[new RouteCollection(), 'compiled_url_matcher0.php'],
447-
[$collection, 'compiled_url_matcher1.php'],
448-
[$redirectCollection, 'compiled_url_matcher2.php'],
449-
[$rootprefixCollection, 'compiled_url_matcher3.php'],
450-
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
451-
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
452-
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
453-
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
454-
[$unicodeCollection, 'compiled_url_matcher8.php'],
455-
[$hostTreeCollection, 'compiled_url_matcher9.php'],
456-
[$chunkedCollection, 'compiled_url_matcher10.php'],
457-
[$demoCollection, 'compiled_url_matcher11.php'],
458-
[$suffixCollection, 'compiled_url_matcher12.php'],
459-
[$hostCollection, 'compiled_url_matcher13.php'],
461+
[new RouteCollection(), 'compiled_url_matcher0.php'],
462+
[$collection, 'compiled_url_matcher1.php'],
463+
[$redirectCollection, 'compiled_url_matcher2.php'],
464+
[$rootprefixCollection, 'compiled_url_matcher3.php'],
465+
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
466+
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
467+
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
468+
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
469+
[$unicodeCollection, 'compiled_url_matcher8.php'],
470+
[$hostTreeCollection, 'compiled_url_matcher9.php'],
471+
[$chunkedCollection, 'compiled_url_matcher10.php'],
472+
[$demoCollection, 'compiled_url_matcher11.php'],
473+
[$suffixCollection, 'compiled_url_matcher12.php'],
474+
[$hostCollection, 'compiled_url_matcher13.php'],
475+
[$fixedLocaleCollection, 'compiled_url_matcher14.php'],
460476
];
461477
}
462478

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.