Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52a999c

Browse files
bug #40135 [FrameworkBundle] Fix freshness checks with boolean parameters on routes (HypeMC)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Fix freshness checks with boolean parameters on routes | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #40017 | License | MIT | Doc PR | - Commits ------- af6db0c [FrameworkBundle] Fix freshness checks with boolean parameters on routes
2 parents c75687f + af6db0c commit 52a999c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,16 @@ private function resolve($value)
178178

179179
$resolved = ($this->paramFetcher)($match[1]);
180180

181-
if (\is_bool($resolved)) {
182-
$resolved = (string) (int) $resolved;
183-
}
184-
185-
if (\is_string($resolved) || is_numeric($resolved)) {
181+
if (is_scalar($resolved)) {
186182
$this->collectedParameters[$match[1]] = $resolved;
187183

188-
return (string) $this->resolve($resolved);
184+
if (\is_string($resolved)) {
185+
$resolved = $this->resolve($resolved);
186+
}
187+
188+
if (is_scalar($resolved)) {
189+
return false === $resolved ? '0' : (string) $resolved;
190+
}
189191
}
190192

191193
throw new RuntimeException(sprintf('The container parameter "%s", used in the route configuration value "%s", must be a string or numeric, but it is of type "%s".', $match[1], $value, \gettype($resolved)));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo:
2+
path: /foo
3+
condition: '%parameter.condition%'

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Container\ContainerInterface;
1616
use Symfony\Bundle\FrameworkBundle\Routing\Router;
17+
use Symfony\Component\Config\FileLocator;
1718
use Symfony\Component\Config\Loader\LoaderInterface;
19+
use Symfony\Component\Config\ResourceCheckerConfigCache;
20+
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
1821
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
22+
use Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker;
1923
use Symfony\Component\DependencyInjection\Container;
2024
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2125
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
26+
use Symfony\Component\Routing\Loader\YamlFileLoader;
2227
use Symfony\Component\Routing\Route;
2328
use Symfony\Component\Routing\RouteCollection;
2429

@@ -503,6 +508,51 @@ public function getNonStringValues()
503508
return [[null], [false], [true], [new \stdClass()], [['foo', 'bar']], [[[]]]];
504509
}
505510

511+
/**
512+
* @dataProvider getContainerParameterForRoute
513+
*/
514+
public function testCacheValiditiyWithContainerParameters($parameter)
515+
{
516+
$cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true);
517+
518+
try {
519+
$container = new Container();
520+
$container->set('routing.loader', new YamlFileLoader(new FileLocator(__DIR__.'/Fixtures')));
521+
522+
$container->setParameter('parameter.condition', $parameter);
523+
524+
$router = new Router($container, 'with_condition.yaml', [
525+
'debug' => true,
526+
'cache_dir' => $cacheDir,
527+
]);
528+
529+
$resourceCheckers = [
530+
new ContainerParametersResourceChecker($container),
531+
];
532+
533+
$router->setConfigCacheFactory(new ResourceCheckerConfigCacheFactory($resourceCheckers));
534+
535+
$router->getMatcher(); // trigger cache build
536+
537+
$cache = new ResourceCheckerConfigCache($cacheDir.\DIRECTORY_SEPARATOR.'UrlMatcher.php', $resourceCheckers);
538+
539+
$this->assertTrue($cache->isFresh());
540+
} finally {
541+
if (is_dir($cacheDir)) {
542+
array_map('unlink', glob($cacheDir.\DIRECTORY_SEPARATOR.'*'));
543+
rmdir($cacheDir);
544+
}
545+
}
546+
}
547+
548+
public function getContainerParameterForRoute()
549+
{
550+
yield 'String' => ['"foo"'];
551+
yield 'Integer' => [0];
552+
yield 'Boolean true' => [true];
553+
yield 'Boolean false' => [false];
554+
}
555+
506556
private function getServiceContainer(RouteCollection $routes): Container
507557
{
508558
$loader = $this->createMock(LoaderInterface::class);

0 commit comments

Comments
 (0)