From c5ca40c711511c245039c4a4cafb204c4ccd46bf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 22 Oct 2011 08:04:10 +0200 Subject: [PATCH] [Routing] added support for _scheme requirement in UrlMatcher (see 07aae9849536cb5fdd0d901aa7eb113b5a8c054e) --- .../Routing/Matcher/Dumper/PhpMatcherDumper.php | 2 +- .../Component/Routing/Matcher/UrlMatcher.php | 12 ++++++++++++ .../Matcher/RedirectableUrlMatcherTest.php | 17 ++++++++++++++++- .../Routing/Matcher/UrlMatcherTest.php | 12 ++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index c8231ede0853..c9aea0a36769 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -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(<<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)); } } diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php index 20ff970649f2..52050287a1b0 100644 --- a/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php +++ b/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php @@ -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/')); @@ -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'); + } } diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php index 5d3349d453ab..4e5f74c2130d 100644 --- a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php +++ b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php @@ -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'); + } }