Skip to content

Commit

Permalink
[Routing] Optimised the PHP URL matcher dumper
Browse files Browse the repository at this point in the history
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'));
    }
}
  • Loading branch information
lmcd committed Jun 11, 2011
1 parent 7e89a6a commit 10bb4ff
Showing 1 changed file with 5 additions and 5 deletions.
Expand Up @@ -69,7 +69,7 @@ public function match(\$pathinfo)
EOF;
}

private function compileRoutes(RouteCollection $routes, $supportsRedirections)
private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
{
$code = array();
foreach ($routes as $name => $route) {
Expand All @@ -80,7 +80,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
$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;
}
Expand All @@ -90,7 +90,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
$code[] = " }\n";
}
} else {
foreach ($this->compileRoute($route, $name, $supportsRedirections) as $line) {
foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
$code[] = $line;
}
}
Expand All @@ -99,7 +99,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
return $code;
}

private function compileRoute(Route $route, $name, $supportsRedirections)
private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
{
$compiledRoute = $route->compile();
$conditions = array();
Expand All @@ -113,7 +113,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections)
$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());
}

Expand Down

0 comments on commit 10bb4ff

Please sign in to comment.