From 872efe572977b9565ad29e404150ea52223ab0fa Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 1 Feb 2019 19:00:30 +0100 Subject: [PATCH] [Routing] fix perf issue when dumping large number of routes --- .../Routing/Matcher/Dumper/PhpMatcherDumper.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 61b2ed320a223..754e903d89ed0 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -159,6 +159,7 @@ private function groupStaticRoutes(RouteCollection $collection): array foreach ($collection->all() as $name => $route) { $compiledRoute = $route->compile(); + $staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/'); $hostRegex = $compiledRoute->getHostRegex(); $regex = $compiledRoute->getRegex(); if ($hasTrailingSlash = '/' !== $route->getPath()) { @@ -173,9 +174,9 @@ private function groupStaticRoutes(RouteCollection $collection): array if ($hasTrailingSlash) { $url = substr($url, 0, -1); } - foreach ($dynamicRegex as list($hostRx, $rx)) { - if (preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) { - $dynamicRegex[] = [$hostRegex, $regex]; + foreach ($dynamicRegex as list($hostRx, $rx, $prefix)) { + if (('' === $prefix || 0 === strpos($url, $prefix)) && preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) { + $dynamicRegex[] = [$hostRegex, $regex, $staticPrefix]; $dynamicRoutes->add($name, $route); continue 2; } @@ -183,7 +184,7 @@ private function groupStaticRoutes(RouteCollection $collection): array $staticRoutes[$url][$name] = [$route, $hasTrailingSlash]; } else { - $dynamicRegex[] = [$hostRegex, $regex]; + $dynamicRegex[] = [$hostRegex, $regex, $staticPrefix]; $dynamicRoutes->add($name, $route); } }