feature #30379 [FrameworkBundle][Routing] allow boolean container parameters for routes (dmaicher)

This PR was merged into the 4.3-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30366
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/11059

This fixes https://github.com/symfony/symfony/issues/30366 and adds support for boolean container parameters in route conditions, defaults etc.

Commits
-------

21f4e38800 [FrameworkBundle][Routing] allow boolean container parameters for routes
This commit is contained in:
Fabien Potencier 2019-03-04 07:35:11 +01:00
commit ec8033f4a9
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']], [[[]]]];