8000 [Security] changed the HttpUtils constructor to tak both a UrlGenerat… · Rodsevich/symfony@16a0af1 · GitHub < 8000 /head>
[go: up one dir, main page]

Skip to content

Commit 16a0af1

Browse files
committed
[Security] changed the HttpUtils constructor to tak both a UrlGenerator and a UrlMatcher instead of a Router (to make it useable by Silex)
1 parent d131f9d commit 16a0af1

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128

129129
<service id="security.http_utils" class="%security.http_utils.class%" public="false">
130130
<argument type="service" id="router" on-invalid="null" />
131+
<argument type="service" id="router" on-invalid="null" />
131132
</service>
132133

133134
<!-- Validator -->

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.1.0
55
-----
66

7+
* changed the HttpUtils constructor signature to take a UrlGenerator and a UrlMatcher instead of a Router
78
* EncoderFactoryInterface::getEncoder() can now also take a class name as an argument
89
* allow switching to the user that is already impersonated
910
* added support for the remember_me parameter in the query

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\RedirectResponse;
18-
use Symfony\Component\Routing\RouterInterface;
18+
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
19+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1920
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
2021
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2122

@@ -26,16 +27,19 @@
2627
*/
2728
class HttpUtils
2829
{
29-
private $router;
30+
private $urlGenerator;
31+
private $urlMatcher;
3032

3133
/**
3234
* Constructor.
3335
*
34-
* @param RouterInterface $router An RouterInterface instance
36+
* @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
37+
* @param UrlMatcherInterface $urlMatcher A UrlMatcherInterface instance
3538
*/
36-
public function __construct(RouterInterface $router = null)
39+
public function __construct(UrlGeneratorInterface $urlGenerator = null, UrlMatcherInterface $urlMatcher = null)
3740
{
38-
$this->router = $router;
41+
$this->urlGenerator = $urlGenerator;
42+
$this->urlMatcher = $urlMatcher;
3943
}
4044

4145
/**
@@ -105,7 +109,7 @@ public function checkRequestPath(Request $request, $path)
105109
{
106110
if ('/' !== $path[0]) {
107111
try {
108-
$parameters = $this->router->match($request->getPathInfo());
112+
$parameters = $this->urlMatcher->match($request->getPathInfo());
109113

110114
return $path === $parameters['_route'];
111115
} catch (MethodNotAllowedException $e) {
@@ -120,10 +124,10 @@ public function checkRequestPath(Request $request, $path)
120124

121125
private function generateUrl($route, $absolute = false)
122126
{
123-
if (null === $this->router) {
124-
throw new \LogicException('You must provide a RouterInterface instance to be able to use routes.');
127+
if (null === $this->urlGenerator) {
128+
throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
125129
}
126130

127-
return $this->router->generate($route, array(), $absolute);
131+
return $this->urlGenerator->generate($route, array(), $absolute);
128132
}
129133
}

src/Symfony/Component/Security/Tests/Http/HttpUtilsTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp()
3030

3131
public function testCreateRedirectResponse()
3232
{
33-
$utils = new HttpUtils($this->getRouter());
33+
$utils = new HttpUtils($this->getUrlGenerator());
3434

3535
// absolute path
3636
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
@@ -42,14 +42,14 @@ public function testCreateRedirectResponse()
4242
$this->assertTrue($response->isRedirect('http://symfony.com/'));
4343

4444
// route name
45-
$utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
46-
$router
45+
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
46+
$urlGenerator
4747
->expects($this->any())
4848
->method('generate')
4949
->with('foobar', array(), true)
5050
->will($this->returnValue('http://localhost/foo/bar'))
5151
;
52-
$router
52+
$urlGenerator
5353
->expects($this->any())
5454
->method('getContext')
5555
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
@@ -60,7 +60,7 @@ public function testCreateRedirectResponse()
6060

6161
public function testCreateRequest()
6262
{
63-
$utils = new HttpUtils($this->getRouter());
63+
$utils = new HttpUtils($this->getUrlGenerator());
6464

6565
// absolute path
6666
$request = $this->getRequest();
@@ -72,13 +72,13 @@ public function testCreateRequest()
7272
$this->assertEquals('bar', $subRequest->server->get('Foo'));
7373

7474
// route name
75-
$utils = new HttpUtils($router = $this->getMockBuilder('Symfony\Component\Routing\Router')->disableOriginalConstructor()->getMock());
76-
$router
75+
$utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
76+
$urlGenerator
7777
->expects($this->once())
7878
->method('generate')
7979
->will($this->returnValue('/foo/bar'))
8080
;
81-
$router
81+
$urlGenerator
8282
->expects($this->any())
8383
->method('getContext')
8484
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
@@ -93,55 +93,55 @@ public function testCreateRequest()
9393

9494
public function testCheckRequestPath()
9595
{
96-
$utils = new HttpUtils($this->getRouter());
96+
$utils = new HttpUtils($this->getUrlGenerator());
9797

9898
$this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
9999
$this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
100100

101-
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
102-
$router
101+
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
102+
$urlMatcher
103103
->expects($this->any())
104104
->method('match')
105105
->will($this->throwException(new ResourceNotFoundException()))
106106
;
107-
$utils = new HttpUtils($router);
107+
$utils = new HttpUtils(null, $urlMatcher);
108108
$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
109109

110-
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
111-
$router
110+
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
111+
$urlMatcher
112112
->expects($this->any())
113113
->method('match')
114114
->will($this->returnValue(array('_route' => 'foobar')))
115115
;
116-
$utils = new HttpUtils($router);
116+
$utils = new HttpUtils(null, $urlMatcher);
117117
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
118118
}
119119

120120
/**
121121
* @expectedException \RuntimeException
122122
*/
123-
public function testCheckRequestPathWithRouterLoadingException()
123+
public function testCheckRequestPathWithUrlMatcherLoadingException()
124124
{
125-
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
126-
$router
125+
$urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
126+
$urlMatcher
127127
->expects($this->any())
128128
->method('match')
129129
->will($this->throwException(new \RuntimeException()))
130130
;
131-
$utils = new HttpUtils($router);
131+
$utils = new HttpUtils(null, $urlMatcher);
132132
$utils->checkRequestPath($this->getRequest(), 'foobar');
133133
}
134134

135-
private function getRouter()
135+
private function getUrlGenerator()
136136
{
137-
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
138-
$router
137+
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
138+
$urlGenerator
139139
->expects($this->any())
140140
->method('generate')
141141
->will($this->returnValue('/foo/bar'))
142142
;
143143

144-
return $router;
144+
return $urlGenerator;
145145
}
146146

147147
private function getRequest($path = '/')

0 commit comments

Comments
 (0)
0