CacheWarmupCommand.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\DependencyInjection\Dumper\Preloader;
  17. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
  18. /**
  19. * Warmup the cache.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. *
  23. * @final
  24. */
  25. class CacheWarmupCommand extends Command
  26. {
  27. protected static $defaultName = 'cache:warmup';
  28. private $cacheWarmer;
  29. public function __construct(CacheWarmerAggregate $cacheWarmer)
  30. {
  31. parent::__construct();
  32. $this->cacheWarmer = $cacheWarmer;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function configure()
  38. {
  39. $this
  40. ->setDefinition([
  41. new InputOption('no-optional-warmers', '', InputOption::VALUE_NONE, 'Skip optional cache warmers (faster)'),
  42. ])
  43. ->setDescription('Warm up an empty cache')
  44. ->setHelp(<<<'EOF'
  45. The <info>%command.name%</info> command warms up the cache.
  46. Before running this command, the cache must be empty.
  47. This command does not generate the classes cache (as when executing this
  48. command, too many classes that should be part of the cache are already loaded
  49. in memory). Use <comment>curl</comment> or any other similar tool to warm up
  50. the classes cache if you want.
  51. EOF
  52. )
  53. ;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function execute(InputInterface $input, OutputInterface $output): int
  59. {
  60. $io = new SymfonyStyle($input, $output);
  61. $kernel = $this->getApplication()->getKernel();
  62. $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)));
  63. if (!$input->getOption('no-optional-warmers')) {
  64. $this->cacheWarmer->enableOptionalWarmers();
  65. }
  66. $preload = $this->cacheWarmer->warmUp($cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'));
  67. if ($preload && file_exists($preloadFile = $cacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
  68. Preloader::append($preloadFile, $preload);
  69. }
  70. $io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully warmed.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
  71. return 0;
  72. }
  73. }