From 8b604ff3ae9bb7d12c3135df5c2ed241673565ed Mon Sep 17 00:00:00 2001 From: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:25:41 +0200 Subject: [PATCH] [FrameworkBundle][HttpKernel] Introduce `$buildDir` argument to `WarmableInterface::warmup` to warm read-only artefacts in `build_dir` --- .github/expected-missing-return-types.diff | 6 +-- .../Doctrine/CacheWarmer/ProxyCacheWarmer.php | 4 +- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../AbstractPhpFileCacheWarmer.php | 11 ++++-- .../CacheWarmer/AnnotationsCacheWarmer.php | 5 ++- .../CachePoolClearerCacheWarmer.php | 5 +-- .../CacheWarmer/ConfigBuilderCacheWarmer.php | 12 ++++-- .../CacheWarmer/RouterCacheWarmer.php | 4 +- .../CacheWarmer/SerializerCacheWarmer.php | 5 ++- .../CacheWarmer/TranslationsCacheWarmer.php | 8 ++-- .../CacheWarmer/ValidatorCacheWarmer.php | 5 ++- .../Command/CacheClearCommand.php | 2 +- .../Command/CacheWarmupCommand.php | 3 +- .../Bundle/FrameworkBundle/Routing/Router.php | 4 +- .../ConfigBuilderCacheWarmerTest.php | 16 +++++++- .../Fixture/TestAppKernel.php | 2 +- .../Translation/Translator.php | 4 +- .../CacheWarmer/ExpressionCacheWarmer.php | 4 +- .../CacheWarmer/TemplateCacheWarmer.php | 4 +- src/Symfony/Component/HttpKernel/CHANGELOG.md | 1 + .../CacheWarmer/CacheWarmerAggregate.php | 15 ++++++-- .../CacheWarmer/WarmableInterface.php | 5 ++- src/Symfony/Component/HttpKernel/Kernel.php | 4 +- .../CacheWarmer/CacheWarmerAggregateTest.php | 38 ++++++++++++++++++- .../Tests/CacheWarmer/CacheWarmerTest.php | 2 +- .../Component/HttpKernel/Tests/KernelTest.php | 6 ++- .../CompiledClassMetadataCacheWarmer.php | 2 +- .../Translation/DataCollectorTranslator.php | 8 ++-- 28 files changed, 136 insertions(+), 50 deletions(-) diff --git a/.github/expected-missing-return-types.diff b/.github/expected-missing-return-types.diff index 1f28f2ed6049f..6717323d5bb6f 100644 --- a/.github/expected-missing-return-types.diff +++ b/.github/expected-missing-return-types.diff @@ -8109,11 +8109,11 @@ diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerInterface.p diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php --- a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php +++ b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php -@@ -24,4 +24,4 @@ interface WarmableInterface +@@ -27,4 +27,4 @@ interface WarmableInterface * @return string[] A list of classes or files to preload on PHP 7.4+ */ -- public function warmUp(string $cacheDir); -+ public function warmUp(string $cacheDir): array; +- public function warmUp(string $cacheDir /* , string $buildDir = null */); ++ public function warmUp(string $cacheDir /* , string $buildDir = null */): array; } diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php --- a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php diff --git a/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php b/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php index 9f1f97e7f93db..f0731c26b7941 100644 --- a/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php +++ b/src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php @@ -38,9 +38,9 @@ public function isOptional(): bool } /** - * @return string[] A list of files to preload on PHP 7.4+ + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { $files = []; foreach ($this->registry->getManagers() as $em) { diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 2e22a3816414b..086447f717fe2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -32,6 +32,7 @@ CHANGELOG * Add `--exclude` option to the `cache:pool:clear` command * Add parameters deprecations to the output of `debug:container` command * Change `framework.asset_mapper.importmap_polyfill` from a URL to the name of an item in the importmap + * Provide `$buildDir` when running `CacheWarmer` to build read-only resources 6.3 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php index 9409bba2e04b4..98c281276be54 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php @@ -35,15 +35,16 @@ public function isOptional(): bool } /** - * @return string[] A list of classes to preload on PHP 7.4+ + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { + $buildDir = 1 < \func_num_args() ? func_get_arg(1) : null; $arrayAdapter = new ArrayAdapter(); spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']); try { - if (!$this->doWarmUp($cacheDir, $arrayAdapter)) { + if (!$this->doWarmUp($cacheDir, $arrayAdapter, $buildDir)) { return []; } } finally { @@ -78,7 +79,9 @@ final protected function ignoreAutoloadException(string $class, \Exception $exce } /** + * @param string|null $buildDir + * * @return bool false if there is nothing to warm-up */ - abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool; + abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool; } diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index 279dc4ec9e702..20533bb60e0ff 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -44,7 +44,10 @@ public function __construct( parent::__construct($phpArrayFile); } - protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool + /** + * @param string|null $buildDir + */ + protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool { $annotatedClassPatterns = $cacheDir.'/annotations.map'; diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php index cad71a409dbc0..7498a82d1f3a3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php @@ -37,10 +37,7 @@ public function __construct(Psr6CacheClearer $poolClearer, array $pools = []) $this->pools = $pools; } - /** - * @return string[] - */ - public function warmUp(string $cacheDirectory): array + public function warmUp(string $cacheDir, string $buildDir = null): array { foreach ($this->pools as $pool) { if ($this->poolClearer->hasPool($pool)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php index ccd3cc9d17df8..7e039ae2f45bb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php @@ -38,11 +38,17 @@ public function __construct(KernelInterface $kernel, LoggerInterface $logger = n } /** - * @return string[] + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { - $generator = new ConfigBuilderGenerator($this->kernel->getBuildDir()); + $buildDir = 1 < \func_num_args() ? func_get_arg(1) : null; + + if (!$buildDir) { + return []; + } + + $generator = new ConfigBuilderGenerator($buildDir); foreach ($this->kernel->getBundles() as $bundle) { $extension = $bundle->getContainerExtension(); diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php index 13daf436939e4..2af9a2fe80a3e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php @@ -34,12 +34,12 @@ public function __construct(ContainerInterface $container) $this->container = $container; } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, string $buildDir = null): array { $router = $this->container->get('router'); if ($router instanceof WarmableInterface) { - return (array) $router->warmUp($cacheDir); + return (array) $router->warmUp($cacheDir, $buildDir); } 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/CacheWarmer/SerializerCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php index 35595474ef01f..b47a48ce698d0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php @@ -39,7 +39,10 @@ public function __construct(array $loaders, string $phpArrayFile) $this->loaders = $loaders; } - protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool + /** + * @param string|null $buildDir + */ + protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool { if (!$this->loaders) { return true; diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php index 039658f4b721b..39b1444b0e113 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php @@ -34,14 +34,16 @@ public function __construct(ContainerInterface $container) } /** - * @return string[] + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { $this->translator ??= $this->container->get('translator'); if ($this->translator instanceof WarmableInterface) { - return (array) $this->translator->warmUp($cacheDir); + $buildDir = 1 < \func_num_args() ? func_get_arg(1) : null; + + return (array) $this->translator->warmUp($cacheDir, $buildDir); } return []; diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php index 3119c9942ad62..224e90985ebe9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php @@ -39,7 +39,10 @@ public function __construct(ValidatorBuilder $validatorBuilder, string $phpArray $this->validatorBuilder = $validatorBuilder; } - protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool + /** + * @param string|null $buildDir + */ + protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool { $loaders = $this->validatorBuilder->getLoaders(); $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter); diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 3a6d1b99d8c2a..878157e872ef7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -245,7 +245,7 @@ private function warmupOptionals(string $cacheDir, string $warmupDir, SymfonySty $warmer = $kernel->getContainer()->get('cache_warmer'); // non optional warmers already ran during container compilation $warmer->enableOnlyOptionalWarmers(); - $preload = (array) $warmer->warmUp($cacheDir, $io); + $preload = (array) $warmer->warmUp($cacheDir, $warmupDir, $io); if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) { Preloader::append($preloadFile, $preload); diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php index c40e3938608d8..6f1073de4ea75 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php @@ -74,7 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $preload = $this->cacheWarmer->warmUp($cacheDir); - if ($preload && file_exists($preloadFile = $cacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) { + $buildDir = $kernel->getContainer()->getParameter('kernel.build_dir'); + if ($preload && $cacheDir === $buildDir && file_exists($preloadFile = $buildDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) { Preloader::append($preloadFile, $preload); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index 3db6519d2db54..3367ecec2bd89 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -81,9 +81,9 @@ public function getRouteCollection(): RouteCollection } /** - * @return string[] A list of classes to preload on PHP 7.4+ + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { $currentDir = $this->getOption('cache_dir'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php index 5ce1f2724c315..a0e8fe834ef81 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php @@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Kernel; @@ -39,7 +40,7 @@ protected function tearDown(): void public function testBuildDirIsUsedAsConfigBuilderOutputDir() { - $kernel = new class($this->varDir) extends Kernel { + $kernel = new class($this->varDir) extends Kernel implements CompilerPassInterface { private $varDir; public function __construct(string $varDir) @@ -75,12 +76,25 @@ public function registerContainerConfiguration(LoaderInterface $loader): void ]); }); } + + public function process(ContainerBuilder $container): void + { + $container->removeDefinition('config_builder.warmer'); + } }; $kernel->boot(); + self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony'); + self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony'); + $warmer = new ConfigBuilderCacheWarmer($kernel); $warmer->warmUp($kernel->getCacheDir()); + self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony'); + self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony'); + + $warmer->warmUp($kernel->getCacheDir(), $kernel->getBuildDir()); + self::assertDirectoryExists($kernel->getBuildDir().'/Symfony'); self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php index 2e4f9f5a9a3f9..7992c4f9d599b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php @@ -52,7 +52,7 @@ public function isOptional(): bool return false; } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, string $buildDir = null): array { file_put_contents($cacheDir.'/dummy.txt', 'Hello'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index c4d414dfb191c..04b56308f3f03 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -95,9 +95,9 @@ public function __construct(ContainerInterface $container, MessageFormatterInter } /** - * @return string[] + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { // skip warmUp when translator doesn't use cache if (null === $this->options['cache_dir']) { diff --git a/src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php b/src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php index 41036f81d31bb..1cbb681c2d709 100644 --- a/src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php +++ b/src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php @@ -35,9 +35,9 @@ public function isOptional(): bool } /** - * @return string[] + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { foreach ($this->expressions as $expression) { $this->expressionLanguage->parse($expression, ['token', 'user', 'object', 'subject', 'role_names', 'request', 'trust_resolver']); diff --git a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php index 4e748ddc61228..2ab801130b6ce 100644 --- a/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php +++ b/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php @@ -36,9 +36,9 @@ public function __construct(ContainerInterface $container, iterable $iterator) } /** - * @return string[] A list of template files to preload on PHP 7.4+ + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { $this->twig ??= $this->container->get('twig'); diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index bf5291721dac7..23f8f0ca4d566 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -15,6 +15,7 @@ CHANGELOG * Support the `!` character at the beginning of a string as a negation operator in the url filter of the profiler * Deprecate `UriSigner`, use `UriSigner` from the HttpFoundation component instead * Deprecate `FileLinkFormatter`, use `FileLinkFormatter` from the ErrorHandler component instead + * Add argument `$buildDir` to `WarmableInterface` 6.3 --- diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php b/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php index 30132921672ca..a672956e0fdcf 100644 --- a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php +++ b/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php @@ -48,8 +48,17 @@ public function enableOnlyOptionalWarmers(): void $this->onlyOptionalsEnabled = $this->optionalsEnabled = true; } - public function warmUp(string $cacheDir, SymfonyStyle $io = null): array + /** + * @param string|null $buildDir + */ + public function warmUp(string $cacheDir, string|SymfonyStyle $buildDir = null, SymfonyStyle $io = null): array { + if ($buildDir instanceof SymfonyStyle) { + trigger_deprecation('symfony/http-kernel', '6.4', 'Passing a "%s" as second argument of "%s()" is deprecated, pass it as third argument instead, after the build directory.', SymfonyStyle::class, __METHOD__); + $io = $buildDir; + $buildDir = null; + } + if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) { $collectedLogs = []; $previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) { @@ -96,8 +105,8 @@ public function warmUp(string $cacheDir, SymfonyStyle $io = null): array } $start = microtime(true); - foreach ((array) $warmer->warmUp($cacheDir) as $item) { - if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) { + foreach ((array) $warmer->warmUp($cacheDir, $buildDir) as $item) { + if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item)) || ($buildDir && str_starts_with($item, \dirname($buildDir)) && !is_file($item))) { throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item)); } $preload[] = $item; diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php index 2f442cb5368b4..cd051b1add811 100644 --- a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php +++ b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php @@ -21,7 +21,10 @@ interface WarmableInterface /** * Warms up the cache. * + * @param string $cacheDir Where warm-up artifacts should be stored + * @param string|null $buildDir Where read-only artifacts should go; null when called after compile-time + * * @return string[] A list of classes or files to preload on PHP 7.4+ */ - public function warmUp(string $cacheDir); + public function warmUp(string $cacheDir /* , string $buildDir = null */); } diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 97d673bfa6c1b..e55cfdf3dca23 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -539,10 +539,10 @@ protected function initializeContainer() touch($oldContainerDir.'.legacy'); } - $preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : []; + $preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir'), $buildDir) : []; if ($this->container->has('cache_warmer')) { - $preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'))); + $preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'), $buildDir)); } if ($preload && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) { diff --git a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php index fd1e6105808c8..635f6dba98742 100644 --- a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerAggregateTest.php @@ -93,6 +93,40 @@ public function testWarmupChecksInvalidFiles() $aggregate->warmUp(__DIR__); } + public function testWarmupPassBuildDir() + { + $warmer = $this->createMock(CacheWarmerInterface::class); + $warmer + ->expects($this->once()) + ->method('isOptional') + ->willReturn(false); + $warmer + ->expects($this->once()) + ->method('warmUp') + ->with('cache_dir', 'build_dir'); + + $aggregate = new CacheWarmerAggregate([$warmer]); + $aggregate->enableOptionalWarmers(); + $aggregate->warmUp('cache_dir', 'build_dir'); + } + + public function testWarmupOnOptionalWarmerDoNotPassBuildDir() + { + $warmer = $this->createMock(CacheWarmerInterface::class); + $warmer + ->expects($this->once()) + ->method('isOptional') + ->willReturn(true); + $warmer + ->expects($this->once()) + ->method('warmUp') + ->with('cache_dir', null); + + $aggregate = new CacheWarmerAggregate([$warmer]); + $aggregate->enableOptionalWarmers(); + $aggregate->warmUp('cache_dir', 'build_dir'); + } + public function testWarmupWhenDebugDisplaysWarmupDuration() { $warmer = $this->createMock(CacheWarmerInterface::class); @@ -115,7 +149,7 @@ public function testWarmupWhenDebugDisplaysWarmupDuration() ->method('warmUp'); $aggregate = new CacheWarmerAggregate([$warmer]); - $aggregate->warmUp(__DIR__, $io); + $aggregate->warmUp(__DIR__, null, $io); } public function testWarmupWhenNotDebugDoesntDisplayWarmupDuration() @@ -140,6 +174,6 @@ public function testWarmupWhenNotDebugDoesntDisplayWarmupDuration() ->method('warmUp'); $aggregate = new CacheWarmerAggregate([$warmer]); - $aggregate->warmUp(__DIR__, $io); + $aggregate->warmUp(__DIR__, null, $io); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerTest.php b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerTest.php index c41bd1d1dc537..c1d6650fc3183 100644 --- a/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/CacheWarmer/CacheWarmerTest.php @@ -54,7 +54,7 @@ public function __construct(string $file) $this->file = $file; } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, string $buildDir = null): array { $this->writeCacheFile($this->file, 'content'); diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 942998eef4e9a..94d847fece27a 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -562,6 +562,7 @@ public function testWarmup() $kernel->boot(); $this->assertTrue($kernel->warmedUp); + $this->assertSame($kernel->getBuildDir(), $kernel->warmedUpBuildDir); } public function testServicesResetter() @@ -756,6 +757,8 @@ class CustomProjectDirKernel extends Kernel implements WarmableInterface { public bool $warmedUp = false; + public ?string $warmedUpBuildDir = null; + public function __construct( private readonly ?\Closure $buildContainer = null, private readonly ?HttpKernelInterface $httpKernel = null, @@ -778,9 +781,10 @@ public function getProjectDir(): string return __DIR__.'/Fixtures'; } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, string $buildDir = null): array { $this->warmedUp = true; + $this->warmedUpBuildDir = $buildDir; return []; } diff --git a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php b/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php index a43e9b4080cdb..64998e98fb295 100644 --- a/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php +++ b/src/Symfony/Component/Serializer/CacheWarmer/CompiledClassMetadataCacheWarmer.php @@ -29,7 +29,7 @@ public function __construct( ) { } - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir, string $buildDir = null): array { $metadatas = []; diff --git a/src/Symfony/Component/Translation/DataCollectorTranslator.php b/src/Symfony/Component/Translation/DataCollectorTranslator.php index c503c9675d411..785e5b89edaa8 100644 --- a/src/Symfony/Component/Translation/DataCollectorTranslator.php +++ b/src/Symfony/Component/Translation/DataCollectorTranslator.php @@ -72,12 +72,14 @@ public function getCatalogues(): array } /** - * @return string[] + * @param string|null $buildDir */ - public function warmUp(string $cacheDir): array + public function warmUp(string $cacheDir /* , string $buildDir = null */): array { + $buildDir = 1 < \func_num_args() ? func_get_arg(1) : null; + if ($this->translator instanceof WarmableInterface) { - return (array) $this->translator->warmUp($cacheDir); + return (array) $this->translator->warmUp($cacheDir, $buildDir); } return [];