Skip to content

Commit

Permalink
[Routing] added support for _scheme requirement in UrlMatcher (see 07…
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 22, 2011
1 parent b95fe53 commit c5ca40c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Expand Up @@ -215,7 +215,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren

if ($scheme = $route->getRequirement('_scheme')) {
if (!$supportsRedirections) {
throw new \LogicException('The "_scheme" requirement is only supported for route dumper that implements RedirectableUrlMatcherInterface.');
throw new \LogicException('The "_scheme" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}

$code[] = sprintf(<<<EOF
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;

/**
* UrlMatcher matches URL based on a set of routes.
Expand Down Expand Up @@ -133,6 +134,17 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
}
}

// check HTTP scheme requirement
if ($scheme = $route->getRequirement('_scheme')) {
if (!$this instanceof RedirectableUrlMatcherInterface) {
throw new \LogicException('The "_scheme" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}

if ($this->context->getScheme() !== $scheme) {
return $this->redirect($pathinfo, $name, $scheme);
}
}

return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
}
}
Expand Down
Expand Up @@ -17,7 +17,7 @@

class RedirectableUrlMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testNoMethodSoAllowed()
public function testRedirectWhenNoSlash()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/'));
Expand All @@ -26,4 +26,19 @@ public function testNoMethodSoAllowed()
$matcher->expects($this->once())->method('redirect');
$matcher->match('/foo');
}

public function testSchemeRedirect()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));

$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
$matcher
->expects($this->once())
->method('redirect')
->with('/foo', 'foo', 'https')
->will($this->returnValue(array('_route' => 'foo')))
;
$matcher->match('/foo');
}
}
12 changes: 12 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Expand Up @@ -205,4 +205,16 @@ public function testMatchRegression()
} catch (ResourceNotFoundException $e) {
}
}

/**
* @expectedException \LogicException
*/
public function testSchemeRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));

$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/foo');
}
}

0 comments on commit c5ca40c

Please sign in to comment.