Skip to content

Commit 91f4097

Browse files
committed
[Routing] Better nesting for RouteCollections in dumped URL matcher classes
With this change, a route prefixed with '/blogger' will be nested inside '/blog' (for example)
1 parent f1e8c1b commit 91f4097

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,55 @@ public function match(\$pathinfo)
7272
private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
7373
{
7474
$code = array();
75-
foreach ($routes as $name => $route) {
75+
76+
$routeIterator = $routes->getIterator();
77+
$keys = array_keys($routeIterator->getArrayCopy());
78+
79+
$i = 0;
80+
81+
foreach ($routeIterator as $name => $route) {
82+
$i++;
83+
7684
if ($route instanceof RouteCollection) {
7785
$prefix = $route->getPrefix();
7886
$optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
7987
$indent = '';
8088
if ($optimizable) {
89+
for ($j = $i; $j < count($keys); $j++) {
90+
if ($keys[$j] === null) continue;
91+
92+
$testRoute = $routeIterator->offsetGet($keys[$j]);
93+
$isCollection = ($testRoute instanceof RouteCollection);
94+
95+
$testPrefix = $isCollection ? $testRoute->getPrefix() : $testRoute->getPattern();
96+
97+
if (0 === strpos($testPrefix, $prefix)) {
98+
$routeIterator->offsetUnset($keys[$j]);
99+
100+
if ($isCollection) {
101+
$route->addCollection($testRoute);
102+
}
103+
else {
104+
$route->add($keys[$j], $testRoute);
105+
}
106+
107+
$i++;
108+
$keys[$j] = null;
109+
}
110+
}
111+
81112
$code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
82113
$indent = ' ';
83114
}
84-
115+
85116
foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
86117
foreach (explode("\n", $line) as $l) {
87118
$code[] = $indent.$l;
88119
}
89120
}
90121

91122
if ($optimizable) {
123+
$code[] = " throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
92124
$code[] = " }\n";
93125
}
94126
} else {

0 commit comments

Comments
 (0)