[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'));
    }
}
This commit is contained in:
Lee McDermott 2011-06-11 01:20:22 +01:00
parent 7e89a6ad41
commit 10bb4ff25e

View File

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