[FrameworkBundle] added sc parameters replacement in route requirements
This commit is contained in:
parent
b5783dffe1
commit
f9ecdfeb05
@ -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
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user