Skip to content

Commit 66b2bf2

Browse files
committed
[FrameworkBundle] added sc parameters replacement in route requirements
1 parent ac69816 commit 66b2bf2

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

Routing/Router.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ public function getRouteCollection()
5151
{
5252
if (null === $this->collection) {
5353
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
54-
$this->applyParameters($this->collection);
54+
$this->resolveParameters($this->collection);
5555
}
5656

5757
return $this->collection;
5858
}
5959

6060
/**
61-
* Replaces placeholders with service container parameter values in route defaults.
61+
* Replaces placeholders with service container parameter values in route defaults and requirements.
6262
*
6363
* @param $collection
6464
*
6565
* @return void
6666
*/
67-
private function applyParameters(RouteCollection $collection)
67+
private function resolveParameters(RouteCollection $collection)
6868
{
6969
foreach ($collection as $route) {
7070
if ($route instanceof RouteCollection) {
71-
$this->applyParameters($route);
71+
$this->resolveParameters($route);
7272
} else {
7373
foreach ($route->getDefaults() as $name => $value) {
7474
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
@@ -80,8 +80,18 @@ private function applyParameters(RouteCollection $collection)
8080
$route->setDefault($name, $this->container->getParameter($key));
8181
}
8282
}
83+
84+
foreach ($route->getRequirements() as $name => $value) {
85+
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
86+
continue;
87+
}
88+
89+
$key = substr($value, 1, -1);
90+
if ($this->container->hasParameter($key)) {
91+
$route->setRequirement($name, $this->container->getParameter($key));
92+
}
93+
}
8394
}
8495
}
85-
8696
}
8797
}

Tests/Routing/RouterTest.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,22 @@ public function testPlaceholders()
2727
'foo1' => '%foo',
2828
'foo2' => 'foo%',
2929
'foo3' => 'f%o%o',
30+
), array(
31+
'foo' => '%foo%',
32+
'bar' => '%bar%',
33+
'foobar' => 'foobar',
34+
'foo1' => '%foo',
35+
'foo2' => 'foo%',
36+
'foo3' => 'f%o%o',
3037
)));
3138

3239
$sc = $this->getServiceContainer($routes);
33-
$sc
34-
->expects($this->at(1))
35-
->method('hasParameter')
36-
->will($this->returnValue(false))
37-
;
38-
$sc
39-
->expects($this->at(2))
40-
->method('hasParameter')
41-
->will($this->returnValue(true))
42-
;
43-
$sc
44-
->expects($this->at(3))
45-
->method('getParameter')
46-
->will($this->returnValue('bar'))
47-
;
40+
$sc->expects($this->at(1))->method('hasParameter')->will($this->returnValue(false));
41+
$sc->expects($this->at(2))->method('hasParameter')->will($this->returnValue(true));
42+
$sc->expects($this->at(3))->method('getParameter')->will($this->returnValue('bar'));
43+
$sc->expects($this->at(4))->method('hasParameter')->will($this->returnValue(false));
44+
$sc->expects($this->at(5))->method('hasParameter')->will($this->returnValue(true));
45+
$sc->expects($this->at(6))->method('getParameter')->will($this->returnValue('bar'));
4846

4947
$router = new Router($sc, 'foo');
5048
$route = $router->getRouteCollection()->get('foo');
@@ -55,6 +53,13 @@ public function testPlaceholders()
5553
$this->assertEquals('%foo', $route->getDefault('foo1'));
5654
$this->assertEquals('foo%', $route->getDefault('foo2'));
5755
$this->assertEquals('f%o%o', $route->getDefault('foo3'));
56+
57+
$this->assertEquals('%foo%', $route->getRequirement('foo'));
58+
$this->assertEquals('bar', $route->getRequirement('bar'));
59+
$this->assertEquals('foobar', $route->getRequirement('foobar'));
60+
$this->assertEquals('%foo', $route->getRequirement('foo1'));
61+
$this->assertEquals('foo%', $route->getRequirement('foo2'));
62+
$this->assertEquals('f%o%o', $route->getRequirement('foo3'));
5863
}
5964

6065
private function getServiceContainer(RouteCollection $routes)

0 commit comments

Comments
 (0)