diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index c8231ede08..c9aea0a367 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -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(<<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 20ff970649..52050287a1 100644 --- a/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php +++ b/tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php @@ -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'); + } } diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php index 5d3349d453..4e5f74c213 100644 --- a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php +++ b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php @@ -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'); + } }