8000 deprecate not passing a build dir when warming up the router cache · symfony/symfony@ca60e25 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ca60e25

Browse files
committed
deprecate not passing a build dir when warming up the router cache
1 parent 37138e6 commit ca60e25

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

UPGRADE-7.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Cache
99
FrameworkBundle
1010
---------------
1111

12+
* Deprecate not passing a build dir to `RouterCacheWarmer::warmUp()` and `Router::warmUp()`
1213
* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`
1314

1415
Messenger

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.1
55
---
66

7+
* Deprecate not passing a build dir to `RouterCacheWarmer::warmUp()` and `Router::warmUp()`
78
* Add `private_ranges` as a shortcut for private IP address ranges to the `trusted_proxies` option
89
* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`
910
* Move the Router `cache_dir` to `kernel.build_dir`

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public function __construct(ContainerInterface $container)
3636

3737
public function warmUp(string $cacheDir, string $buildDir = null): array
3838
{
39-
if (!$buildDir) {
40-
return [];
39+
if (null === $buildDir) {
40+
trigger_deprecation('symfony/framework-bundle', '7.1', sprintf('Not passing a build dir as the second argument to "%s()" is deprecated.', __METHOD__));
41+
// return [];
4142
}
4243

4344
$router = $this->container->get('router');
4445

4546
if ($router instanceof WarmableInterface) {
46-
return (array) $router->warmUp($cacheDir, $buildDir);
47+
return (array) $router->warmUp($cacheDir, $buildDir, false);
4748
}
4849

4950
throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,17 @@ public function getRouteCollection(): RouteCollection
8484

8585
public function warmUp(string $cacheDir, string $buildDir = null): array
8686
{
87-
if (!$buildDir) {
88-
return [];
87+
if (null === $buildDir) {
88+
if (\func_num_args() < 3) {
89+
trigger_deprecation('symfony/framework-bundle', '7.1', sprintf('Not passing a build dir as the second argument to "%s()" is deprecated.', __METHOD__));
90+
}
91+
// return [];
8992
}
9093

9194
$currentDir = $this->getOption('cache_dir');
9295

93-
// force cache generation in build_dir
94-
$this->setOption('cache_dir', $buildDir);
96+
// force cache generation (in build_dir if present)
97+
$this->setOption('cache_dir', $buildDir ?? $cacheDir);
9598
$this->getMatcher();
9699
$this->getGenerator();
97100

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Psr\Container\ContainerInterface;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1819
use Symfony\Component\Routing\RouterInterface;
1920

2021
class RouterCacheWarmerTest extends TestCase
2122
{
23+
use ExpectDeprecationTrait;
24+
2225
public function testWarmUpWithWarmableInterfaceWithBuildDir()
2326
{
24-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
27+
$container = new ContainerBuilder();
2528

2629
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
27-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
28-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
30+
$container->set('router', $routerMock);
31+
$routerCacheWarmer = new RouterCacheWarmer($container);
2932

3033
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
3134
$routerMock->expects($this->any())->method('warmUp')->with('/tmp/cache', '/tmp/build')->willReturn([]);
@@ -34,39 +37,50 @@ public function testWarmUpWithWarmableInterfaceWithBuildDir()
3437

3538
public function testWarmUpWithoutWarmableInterfaceWithBuildDir()
3639
{
37-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
40+
$container = new ContainerBuilder();
3841

3942
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
40-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
41-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
43+
$container->set('router', $routerMock);
44+
$routerCacheWarmer = new RouterCacheWarmer($container);
4245
$this->expectException(\LogicException::class);
4346
$this->expectExceptionMessage('cannot be warmed up because it does not implement "Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface"');
4447
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
4548
}
4649

50+
/**
51+
* @group legacy
52+
*/
4753
public function testWarmUpWithWarmableInterfaceWithoutBuildDir()
4854
{
49-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
55+
$this->expectDeprecation('Since symfony/framework-bundle 7.1: Not passing a build dir as the second argument to "Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer::warmUp()" is deprecated.');
56+
57+
$container = new ContainerBuilder();
5058

5159
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
52-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
53-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
60+
$container->set('router', $routerMock);
61+
$routerCacheWarmer = new RouterCacheWarmer($container);
5462

5563
$preload = $routerCacheWarmer->warmUp('/tmp');
5664
$routerMock->expects($this->never())->method('warmUp');
5765
self::assertSame([], $preload);
58-
$this->addToAssertionCount(1);
5966
}
6067

68+
/**
69+
* @group legacy
70+
*/
6171
public function testWarmUpWithoutWarmableInterfaceWithoutBuildDir()
6272
{
63-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
73+
$this->expectDeprecation('Since symfony/framework-bundle 7.1: Not passing a build dir as the second argument to "Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer::warmUp()" is deprecated.');
74+
75+
$container = new ContainerBuilder();
6476

6577
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
66-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
67-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
68-
$preload = $routerCacheWarmer->warmUp('/tmp');
69-
self::assertSame([], $preload);
78+
$container->set('router', $routerMock);
79+
$routerCacheWarmer = new RouterCacheWarmer($container);
80+
81+
$this->expectException(\LogicException::class);
82+
83+
$routerCacheWarmer->warmUp('/tmp');
7084
}
7185
}
7286

0 commit comments

Comments
 (0)
0