diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php index 6f1073de4ea75..fe43efec18864 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php @@ -11,15 +11,20 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Psr\Container\ContainerInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; use Symfony\Component\DependencyInjection\Dumper\Preloader; +use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate; use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; +use Symfony\Contracts\Service\ServiceProviderInterface; /** * Warmup the cache. @@ -31,13 +36,11 @@ #[AsCommand(name: 'cache:warmup', description: 'Warm up an empty cache')] class CacheWarmupCommand extends Command { - private CacheWarmerAggregate $cacheWarmer; - - public function __construct(CacheWarmerAggregate $cacheWarmer) - { + public function __construct( + private readonly CacheWarmerAggregate $cacheWarmer, + private readonly ServiceProviderInterface $cacheWarmers, + ) { parent::__construct(); - - $this->cacheWarmer = $cacheWarmer; } protected function configure(): void @@ -45,6 +48,7 @@ protected function configure(): void $this ->setDefinition([ new InputOption('no-optional-warmers', '', InputOption::VALUE_NONE, 'Skip optional cache warmers (faster)'), + new InputArgument('warmer-name', InputArgument::OPTIONAL, 'Specific warmer name to warmup') ]) ->setHelp(<<<'EOF' The %command.name% command warms up the cache. @@ -62,11 +66,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int $kernel = $this->getApplication()->getKernel(); $io->comment(sprintf('Warming up the cache for the %s environment with debug %s', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); + $cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'); + $buildDir = $kernel->getContainer()->getParameter('kernel.build_dir'); + + if ($name = $input->getArgument('warmer-name')) { + if (!$this->cacheWarmers->has($name)) { + $io->error(sprintf('Cache warmer "%s" does not exist.', $name)); + + return 1; + } + + $this->cacheWarmers->get($name)->warmUp($cacheDir, $buildDir); + + $io->success(sprintf('Cache warmer "%s" was successfully warmed.', $name)); + + return 0; + } if (!$input->getOption('no-optional-warmers')) { $this->cacheWarmer->enableOptionalWarmers(); } - $cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'); if ($kernel instanceof WarmableInterface) { $kernel->warmUp($cacheDir); @@ -74,7 +93,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $preload = $this->cacheWarmer->warmUp($cacheDir); - $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/Resources/config/console.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php index 334d20426c68c..53a345140cfa8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php @@ -120,6 +120,7 @@ ->set('console.command.cache_warmup', CacheWarmupCommand::class) ->args([ service('cache_warmer'), + tagged_locator('kernel.cache_warmer') ]) ->tag('console.command')