[FrameworkBundle][Routing] allow boolean container parameters for routes

This commit is contained in:
David Maicher 2019-02-25 19:34:05 +01:00
parent d2e9a7051f
commit 21f4e38800
3 changed files with 24 additions and 1 deletions

View File

@ -17,6 +17,7 @@ CHANGELOG
to the `session` section of the configuration
* Added support for Translator paths, Twig paths in translation commands.
* Added support for PHP files with translations in translation commands.
* Added support for boolean container parameters within routes.
4.2.0
-----

View File

@ -164,6 +164,10 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI
$resolved = ($this->paramFetcher)($match[1]);
if (\is_bool($resolved)) {
$resolved = (string) (int) $resolved;
}
if (\is_string($resolved) || is_numeric($resolved)) {
$this->collectedParameters[$match[1]] = $resolved;

View File

@ -27,7 +27,7 @@ class RouterTest extends TestCase
*/
public function testConstructThrowsOnNonSymfonyNorPsr11Container()
{
new Router($this->getMockBuilder(ContainerInterface::class)->getMock(), 'foo');
new Router($this->createMock(ContainerInterface::class), 'foo');
}
public function testGenerateWithServiceParam()
@ -447,6 +447,24 @@ class RouterTest extends TestCase
$this->assertEquals([new ContainerParametersResource(['locale' => 'en'])], $routeCollection->getResources());
}
public function testBooleanContainerParametersWithinRouteCondition()
{
$routes = new RouteCollection();
$route = new Route('foo');
$route->setCondition('%parameter.true% or %parameter.false%');
$routes->add('foo', $route);
$sc = $this->getPsr11ServiceContainer($routes);
$parameters = $this->getParameterBag(['parameter.true' => true, 'parameter.false' => false]);
$router = new Router($sc, 'foo', [], null, $parameters);
$route = $router->getRouteCollection()->get('foo');
$this->assertSame('1 or 0', $route->getCondition());
}
public function getNonStringValues()
{
return [[null], [false], [true], [new \stdClass()], [['foo', 'bar']], [[[]]]];