Skip to content

Commit 10bb4ff

Browse files
committed
[Routing] Optimised the PHP URL matcher dumper
The cached URL matcher classes contain some unneeded logic. Consider the following example: if (0 === strpos($pathinfo, '/Blog')) { // blog_index if (0 === strpos($pathinfo, '/Blog') && preg_match('#^/Blog/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) { return array_merge($this->mergeDefaults($matches, array ( '_action' => 'index',)), array('_route' => 'blog_index')); } } The 2nd strpos is not required, as we have already satisfied this condition in the parent if statement. My change will produce the following code for the same routing setup:: if (0 === strpos($pathinfo, '/Blog')) { // blog_index if (preg_match('#^/Blog/(?P<slug>[^/]+?)$#x', $pathinfo, $matches)) { return array_merge($this->mergeDefaults($matches, array ( '_action' => 'index',)), array('_route' => 'blog_index')); } }
1 parent 7e89a6a commit 10bb4ff

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function match(\$pathinfo)
6969
EOF;
7070
}
7171

72-
private function compileRoutes(RouteCollection $routes, $supportsRedirections)
72+
private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
7373
{
7474
$code = array();
7575
foreach ($routes as $name => $route) {
@@ -80,7 +80,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
8080
$indent = ' ';
8181
}
8282

83-
foreach ($this->compileRoutes($route, $supportsRedirections) as $line) {
83+
foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
8484
foreach (explode("\n", $line) as $l) {
8585
$code[] = $indent.$l;
8686
}
@@ -90,7 +90,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
9090
$code[] = " }\n";
9191
}
9292
} else {
93-
foreach ($this->compileRoute($route, $name, $supportsRedirections) as $line) {
93+
foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
9494
$code[] = $line;
9595
}
9696
}
@@ -99,7 +99,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
9999
return $code;
100100
}
101101

102-
private function compileRoute(Route $route, $name, $supportsRedirections)
102+
private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
103103
{
104104
$compiledRoute = $route->compile();
105105
$conditions = array();
@@ -113,7 +113,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections)
113113
$conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
114114
}
115115
} else {
116-
if ($compiledRoute->getStaticPrefix()) {
116+
if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
117117
$conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
118118
}
119119

0 commit comments

Comments
 (0)