Skip to content

Commit

Permalink
Add warmer-name argument to cache:warmup command
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Dec 9, 2023
1 parent c4e97eb commit 26654e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,20 +36,19 @@
#[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
{
$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 <info>%command.name%</info> command warms up the cache.
Expand All @@ -62,19 +66,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$kernel = $this->getApplication()->getKernel();
$io->comment(sprintf('Warming up the cache for the <info>%s</info> environment with debug <info>%s</info>', $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);
}

$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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
->set('console.command.cache_warmup', CacheWarmupCommand::class)
->args([
service('cache_warmer'),
tagged_locator('kernel.cache_warmer')
])
->tag('console.command')

Expand Down

0 comments on commit 26654e7

Please sign in to comment.