Skip to content

Commit

Permalink
[FrameworkBundle] added sc parameters replacement in route requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 14, 2011
1 parent b5783df commit f9ecdfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-2.1.md
Expand Up @@ -12,7 +12,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c

### FrameworkBundle

* added support for placeholders in route default values (replaced by the value set in the service container)
* added support for placeholders in route defaults and requirements (replaced by the value set in the service container)

### ClassLoader

Expand Down
20 changes: 15 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Expand Up @@ -51,24 +51,24 @@ public function getRouteCollection()
{
if (null === $this->collection) {
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
$this->applyParameters($this->collection);
$this->resolveParameters($this->collection);
}

return $this->collection;
}

/**
* Replaces placeholders with service container parameter values in route defaults.
* Replaces placeholders with service container parameter values in route defaults and requirements.
*
* @param $collection
*
* @return void
*/
private function applyParameters(RouteCollection $collection)
private function resolveParameters(RouteCollection $collection)
{
foreach ($collection as $route) {
if ($route instanceof RouteCollection) {
$this->applyParameters($route);
$this->resolveParameters($route);
} else {
foreach ($route->getDefaults() as $name => $value) {
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
Expand All @@ -80,8 +80,18 @@ private function applyParameters(RouteCollection $collection)
$route->setDefault($name, $this->container->getParameter($key));
}
}

foreach ($route->getRequirements() as $name => $value) {
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
continue;
}

$key = substr($value, 1, -1);
if ($this->container->hasParameter($key)) {
$route->setRequirement($name, $this->container->getParameter($key));
}
}
}
}

}
}
35 changes: 20 additions & 15 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php
Expand Up @@ -27,24 +27,22 @@ public function testPlaceholders()
'foo1' => '%foo',
'foo2' => 'foo%',
'foo3' => 'f%o%o',
), array(
'foo' => '%foo%',
'bar' => '%bar%',
'foobar' => 'foobar',
'foo1' => '%foo',
'foo2' => 'foo%',
'foo3' => 'f%o%o',
)));

$sc = $this->getServiceContainer($routes);
$sc
->expects($this->at(1))
->method('hasParameter')
->will($this->returnValue(false))
;
$sc
->expects($this->at(2))
->method('hasParameter')
->will($this->returnValue(true))
;
$sc
->expects($this->at(3))
->method('getParameter')
->will($this->returnValue('bar'))
;
$sc->expects($this->at(1))->method('hasParameter')->will($this->returnValue(false));
$sc->expects($this->at(2))->method('hasParameter')->will($this->returnValue(true));
$sc->expects($this->at(3))->method('getParameter')->will($this->returnValue('bar'));
$sc->expects($this->at(4))->method('hasParameter')->will($this->returnValue(false));
$sc->expects($this->at(5))->method('hasParameter')->will($this->returnValue(true));
$sc->expects($this->at(6))->method('getParameter')->will($this->returnValue('bar'));

$router = new Router($sc, 'foo');
$route = $router->getRouteCollection()->get('foo');
Expand All @@ -55,6 +53,13 @@ public function testPlaceholders()
$this->assertEquals('%foo', $route->getDefault('foo1'));
$this->assertEquals('foo%', $route->getDefault('foo2'));
$this->assertEquals('f%o%o', $route->getDefault('foo3'));

$this->assertEquals('%foo%', $route->getRequirement('foo'));
$this->assertEquals('bar', $route->getRequirement('bar'));
$this->assertEquals('foobar', $route->getRequirement('foobar'));
$this->assertEquals('%foo', $route->getRequirement('foo1'));
$this->assertEquals('foo%', $route->getRequirement('foo2'));
$this->assertEquals('f%o%o', $route->getRequirement('foo3'));
}

private function getServiceContainer(RouteCollection $routes)
Expand Down

0 comments on commit f9ecdfe

Please sign in to comment.