Skip to content

Commit

Permalink
[Routing] Better nesting for RouteCollections in dumped URL matcher c…
Browse files Browse the repository at this point in the history
…lasses

With this change, a route prefixed with '/blogger' will be nested inside '/blog' (for example)
  • Loading branch information
lmcd committed Jun 11, 2011
1 parent f1e8c1b commit 91f4097
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Expand Up @@ -72,23 +72,55 @@ public function match(\$pathinfo)
private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
{
$code = array();
foreach ($routes as $name => $route) {

$routeIterator = $routes->getIterator();
$keys = array_keys($routeIterator->getArrayCopy());

$i = 0;

foreach ($routeIterator as $name => $route) {
$i++;

if ($route instanceof RouteCollection) {
$prefix = $route->getPrefix();
$optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
$indent = '';
if ($optimizable) {
for ($j = $i; $j < count($keys); $j++) {
if ($keys[$j] === null) continue;

$testRoute = $routeIterator->offsetGet($keys[$j]);
$isCollection = ($testRoute instanceof RouteCollection);

$testPrefix = $isCollection ? $testRoute->getPrefix() : $testRoute->getPattern();

if (0 === strpos($testPrefix, $prefix)) {
$routeIterator->offsetUnset($keys[$j]);

if ($isCollection) {
$route->addCollection($testRoute);
}
else {
$route->add($keys[$j], $testRoute);
}

$i++;
$keys[$j] = null;
}
}

$code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
$indent = ' ';
}

foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
foreach (explode("\n", $line) as $l) {
$code[] = $indent.$l;
}
}

if ($optimizable) {
$code[] = " throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
$code[] = " }\n";
}
} else {
Expand Down

0 comments on commit 91f4097

Please sign in to comment.