From ca60e253d78dcf2bcab767826fba1f0137af135f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 7 Jan 2024 17:43:02 +0100 Subject: [PATCH] deprecate not passing a build dir when warming up the router cache --- UPGRADE-7.1.md | 1 + .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../CacheWarmer/RouterCacheWarmer.php | 7 +-- .../Bundle/FrameworkBundle/Routing/Router.php | 11 +++-- .../CacheWarmer/RouterCacheWarmerTest.php | 46 ++++++++++++------- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/UPGRADE-7.1.md b/UPGRADE-7.1.md index ce3a26a180664..71db7f924023a 100644 --- a/UPGRADE-7.1.md +++ b/UPGRADE-7.1.md @@ -9,6 +9,7 @@ Cache FrameworkBundle --------------- + * Deprecate not passing a build dir to `RouterCacheWarmer::warmUp()` and `Router::warmUp()` * Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final` Messenger diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 43d7f62950e83..c035c30909c7f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 7.1 --- + * Deprecate not passing a build dir to `RouterCacheWarmer::warmUp()` and `Router::warmUp()` * Add `private_ranges` as a shortcut for private IP address ranges to the `trusted_proxies` option * Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final` * Move the Router `cache_dir` to `kernel.build_dir` diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php index 9dfa71c2c542f..93b2f29e13164 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php @@ -36,14 +36,15 @@ public function __construct(ContainerInterface $container) public function warmUp(string $cacheDir, string $buildDir = null): array { - if (!$buildDir) { - return []; + if (null === $buildDir) { + trigger_deprecation('symfony/framework-bundle', '7.1', sprintf('Not passing a build dir as the second argument to "%s()" is deprecated.', __METHOD__)); + // return []; } $router = $this->container->get('router'); if ($router instanceof WarmableInterface) { - return (array) $router->warmUp($cacheDir, $buildDir); + return (array) $router->warmUp($cacheDir, $buildDir, false); } throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index 6712817eba128..c8aea59f85132 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -84,14 +84,17 @@ public function getRouteCollection(): RouteCollection public function warmUp(string $cacheDir, string $buildDir = null): array { - if (!$buildDir) { - return []; + if (null === $buildDir) { + if (\func_num_args() < 3) { + trigger_deprecation('symfony/framework-bundle', '7.1', sprintf('Not passing a build dir as the second argument to "%s()" is deprecated.', __METHOD__)); + } + // return []; } $currentDir = $this->getOption('cache_dir'); - // force cache generation in build_dir - $this->setOption('cache_dir', $buildDir); + // force cache generation (in build_dir if present) + $this->setOption('cache_dir', $buildDir ?? $cacheDir); $this->getMatcher(); $this->getGenerator(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php index 7686b139f28f9..f25dad6487f79 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php @@ -12,20 +12,23 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer; use PHPUnit\Framework\TestCase; -use Psr\Container\ContainerInterface; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; use Symfony\Component\Routing\RouterInterface; class RouterCacheWarmerTest extends TestCase { + use ExpectDeprecationTrait; + public function testWarmUpWithWarmableInterfaceWithBuildDir() { - $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock(); + $container = new ContainerBuilder(); $routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock(); - $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock); - $routerCacheWarmer = new RouterCacheWarmer($containerMock); + $container->set('router', $routerMock); + $routerCacheWarmer = new RouterCacheWarmer($container); $routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build'); $routerMock->expects($this->any())->method('warmUp')->with('/tmp/cache', '/tmp/build')->willReturn([]); @@ -34,39 +37,50 @@ public function testWarmUpWithWarmableInterfaceWithBuildDir() public function testWarmUpWithoutWarmableInterfaceWithBuildDir() { - $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock(); + $container = new ContainerBuilder(); $routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock(); - $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock); - $routerCacheWarmer = new RouterCacheWarmer($containerMock); + $container->set('router', $routerMock); + $routerCacheWarmer = new RouterCacheWarmer($container); $this->expectException(\LogicException::class); $this->expectExceptionMessage('cannot be warmed up because it does not implement "Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface"'); $routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build'); } + /** + * @group legacy + */ public function testWarmUpWithWarmableInterfaceWithoutBuildDir() { - $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock(); + $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.'); + + $container = new ContainerBuilder(); $routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock(); - $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock); - $routerCacheWarmer = new RouterCacheWarmer($containerMock); + $container->set('router', $routerMock); + $routerCacheWarmer = new RouterCacheWarmer($container); $preload = $routerCacheWarmer->warmUp('/tmp'); $routerMock->expects($this->never())->method('warmUp'); self::assertSame([], $preload); - $this->addToAssertionCount(1); } + /** + * @group legacy + */ public function testWarmUpWithoutWarmableInterfaceWithoutBuildDir() { - $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock(); + $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.'); + + $container = new ContainerBuilder(); $routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock(); - $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock); - $routerCacheWarmer = new RouterCacheWarmer($containerMock); - $preload = $routerCacheWarmer->warmUp('/tmp'); - self::assertSame([], $preload); + $container->set('router', $routerMock); + $routerCacheWarmer = new RouterCacheWarmer($container); + + $this->expectException(\LogicException::class); + + $routerCacheWarmer->warmUp('/tmp'); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy