diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 4c91ff11be..4fc04f0671 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index 6bea82e1ef..2f252846b7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -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; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php index 329f1e7cba..b2b848a7e2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php @@ -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']], [[[]]]];