8000 [Routing] added support for _scheme requirement in UrlMatcher (see 07… · vatson/symfony@c5ca40c · GitHub
[go: up one dir, main page]

Skip to content

Commit c5ca40c

Browse files
committed
[Routing] added support for _scheme requirement in UrlMatcher (see 07aae98)
1 parent b95fe53 commit c5ca40c

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
215215

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

221221
$code[] = sprintf(<<<EOF

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Routing\Route;
1717
use Symfony\Component\Routing\RouteCollection;
1818
use Symfony\Component\Routing\RequestContext;
19+
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
1920

2021
/**
2122
* UrlMatcher matches URL based on a set of routes.
@@ -133,6 +134,17 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
133134
}
134135
}
135136

137+
// check HTTP scheme requirement
138+
if ($scheme = $route->getRequirement('_scheme')) {
139+
if (!$this instanceof RedirectableUrlMatcherInterface) {
140+
throw new \LogicException('The "_scheme" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
141+
}
142+
143+
if ($this->context->getScheme() !== $scheme) {
144+
return $this->redirect($pathinfo, $name, $scheme);
145+
}
146+
}
147+
136148
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
137149
}
138150
}

tests/Symfony/Tests/Component/Routing/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class RedirectableUrlMatcherTest extends \PHPUnit_Framework_TestCase
1919
{
20-
public function testNoMethodSoAllowed()
20+
public function testRedirectWhenNoSlash()
2121
{
2222
$coll = new RouteCollection();
2323
$coll->add('foo', new Route('/foo/'));
@@ -26,4 +26,19 @@ public function testNoMethodSoAllowed()
2626
$matcher->expects($this->once())->method('redirect');
2727
$matcher->match('/foo');
2828
}
29+
30+
public function testSchemeRedirect()
31+
{
32+
$coll = new RouteCollection();
33+
$coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));
34+
35+
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
36+
$matcher
37+
->expects($this->once())
38+
->method('redirect')
39+
->with('/foo', 'foo', 'https')
40+
->will($this->returnValue(array('_route' => 'foo')))
41+
;
42+
$matcher->match('/foo');
43+
}
2944
}

tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,16 @@ public function testMatchRegression()
205205
} catch (ResourceNotFoundException $e) {
206206
}
207207
}
208+
209+
/**
210+
* @expectedException \LogicException
211+
*/
212+
public function testSchemeRequirement()
213+
{
214+
$coll = new RouteCollection();
215+
$coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));
216+
217+
$matcher = new UrlMatcher($coll, new RequestContext());
218+
$matcher->match('/foo');
219+
}
208220
}

0 commit comments

Comments
 (0)
0