[Routing] added support for _scheme requirement in UrlMatcher (see 07aae98495)

This commit is contained in:
Fabien Potencier 2011-10-22 08:04:10 +02:00
parent b95fe53bf4
commit c5ca40c711
4 changed files with 41 additions and 2 deletions

View File

@ -215,7 +215,7 @@ EOF
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

View File

@ -16,6 +16,7 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException;
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.
@ -133,6 +134,17 @@ class UrlMatcher implements UrlMatcherInterface
}
}
// 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));
}
}

View File

@ -17,7 +17,7 @@ use Symfony\Component\Routing\RequestContext;
class RedirectableUrlMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testNoMethodSoAllowed()
public function testRedirectWhenNoSlash()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/'));
@ -26,4 +26,19 @@ class RedirectableUrlMatcherTest extends \PHPUnit_Framework_TestCase
$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');
}
}

View File

@ -205,4 +205,16 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
} 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');
}
}