diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 799b536899..11af5c97a2 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -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 diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index aa9877ae6e..e3fb8f4e8d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -51,24 +51,24 @@ class Router extends BaseRouter { 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)) { @@ -80,8 +80,18 @@ class Router extends BaseRouter $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)); + } + } } } - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php index 5d1558272e..1c264beea3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php @@ -27,24 +27,22 @@ class RoutingTest extends \PHPUnit_Framework_TestCase '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'); @@ -55,6 +53,13 @@ class RoutingTest extends \PHPUnit_Framework_TestCase $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)