CacheWarmupCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15. /**
  16. * Warmup the cache.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class CacheWarmupCommand extends ContainerAwareCommand
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('cache:warmup')
  29. ->setDefinition(array(
  30. new InputOption('no-optional-warmers', '', InputOption::VALUE_NONE, 'Skip optional cache warmers (faster)'),
  31. ))
  32. ->setDescription('Warms up an empty cache')
  33. ->setHelp(<<<'EOF'
  34. The <info>%command.name%</info> command warms up the cache.
  35. Before running this command, the cache must be empty.
  36. This command does not generate the classes cache (as when executing this
  37. command, too many classes that should be part of the cache are already loaded
  38. in memory). Use <comment>curl</comment> or any other similar tool to warm up
  39. the classes cache if you want.
  40. EOF
  41. )
  42. ;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function execute(InputInterface $input, OutputInterface $output)
  48. {
  49. $io = new SymfonyStyle($input, $output);
  50. $kernel = $this->getContainer()->get('kernel');
  51. $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)));
  52. $warmer = $this->getContainer()->get('cache_warmer');
  53. if (!$input->getOption('no-optional-warmers')) {
  54. $warmer->enableOptionalWarmers();
  55. }
  56. $warmer->warmUp($this->getContainer()->getParameter('kernel.cache_dir'));
  57. $io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully warmed.', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
  58. }
  59. }