Browse Source

[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'));
}
}

Lee McDermott 14 years ago
parent
commit
10bb4ff25e
1 changed files with 5 additions and 5 deletions
  1. 5 5
      src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

+ 5 - 5
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -69,7 +69,7 @@ $code
 EOF;
     }
 
-    private function compileRoutes(RouteCollection $routes, $supportsRedirections)
+    private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
     {
         $code = array();
         foreach ($routes as $name => $route) {
@@ -80,7 +80,7 @@ EOF;
                     $indent = '    ';
                 }
 
-                foreach ($this->compileRoutes($route, $supportsRedirections) as $line) {
+                foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
                     foreach (explode("\n", $line) as $l) {
                         $code[] = $indent.$l;
                     }
@@ -90,7 +90,7 @@ EOF;
                     $code[] = "        }\n";
                 }
             } else {
-                foreach ($this->compileRoute($route, $name, $supportsRedirections) as $line) {
+                foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
                     $code[] = $line;
                 }
             }
@@ -99,7 +99,7 @@ EOF;
         return $code;
     }
 
-    private function compileRoute(Route $route, $name, $supportsRedirections)
+    private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
     {
         $compiledRoute = $route->compile();
         $conditions = array();
@@ -113,7 +113,7 @@ EOF;
                 $conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
             }
         } else {
-            if ($compiledRoute->getStaticPrefix()) {
+            if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
                 $conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
             }