Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Routing] optimized PHP dumper when the parent prefix is the same for…
… several adjacent collections (avoids the same test to be made)
  • Loading branch information
fabpot committed Jun 15, 2011
1 parent 1438f6b commit 46a93c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Expand Up @@ -79,7 +79,6 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
$keysCount = count($keys);

$i = 0;

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

Expand Down Expand Up @@ -113,8 +112,10 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
}
}

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

foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
Expand All @@ -123,7 +124,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections, $
}
}

if ($optimizable) {
if ($optimizable && $prefix !== $parentPrefix) {
$code[] = " throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
$code[] = " }\n";
}
Expand Down
Expand Up @@ -113,6 +113,18 @@ public function match($pathinfo)
return $matches;
}

// foo1
if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo1';
return $matches;
}

// bar1
if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar1';
return $matches;
}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}

Expand Down
Expand Up @@ -125,6 +125,18 @@ public function match($pathinfo)
return $matches;
}

// foo1
if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'foo1';
return $matches;
}

// bar1
if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
$matches['_route'] = 'bar1';
return $matches;
}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}

Expand Down
Expand Up @@ -80,6 +80,10 @@ public function testDump()
$collection1->add('bar', new Route('/{bar}'));
$collection2 = new RouteCollection();
$collection2->addCollection($collection1, '/b');
$collection1 = new RouteCollection();
$collection1->add('foo1', new Route('/{foo1}'));
$collection1->add('bar1', new Route('/{bar1}'));
$collection2->addCollection($collection1, '/b');
$collection->addCollection($collection2, '/a');

// "dynamic" prefix
Expand All @@ -98,6 +102,7 @@ public function testDump()
$collection->addCollection($collection1, '/aba');

$dumper = new PhpMatcherDumper($collection, new RequestContext());

$this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');

// force HTTPS redirection
Expand Down

0 comments on commit 46a93c3

Please sign in to comment.