vendor/symfony/framework-bundle/Command/CacheClearCommand.php line 43

  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\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Exception\RuntimeException;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\DependencyInjection\Dumper\Preloader;
  19. use Symfony\Component\EventDispatcher\EventDispatcher;
  20. use Symfony\Component\Filesystem\Exception\IOException;
  21. use Symfony\Component\Filesystem\Filesystem;
  22. use Symfony\Component\Finder\Finder;
  23. use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
  24. use Symfony\Component\HttpKernel\RebootableInterface;
  25. /**
  26.  * Clear and Warmup the cache.
  27.  *
  28.  * @author Francis Besset <francis.besset@gmail.com>
  29.  * @author Fabien Potencier <fabien@symfony.com>
  30.  *
  31.  * @final
  32.  */
  33. #[AsCommand(name'cache:clear'description'Clear the cache')]
  34. class CacheClearCommand extends Command
  35. {
  36.     private CacheClearerInterface $cacheClearer;
  37.     private Filesystem $filesystem;
  38.     public function __construct(CacheClearerInterface $cacheClearerFilesystem $filesystem null)
  39.     {
  40.         parent::__construct();
  41.         $this->cacheClearer $cacheClearer;
  42.         $this->filesystem $filesystem ?? new Filesystem();
  43.     }
  44.     protected function configure()
  45.     {
  46.         $this
  47.             ->setDefinition([
  48.                 new InputOption('no-warmup'''InputOption::VALUE_NONE'Do not warm up the cache'),
  49.                 new InputOption('no-optional-warmers'''InputOption::VALUE_NONE'Skip optional cache warmers (faster)'),
  50.             ])
  51.             ->setHelp(<<<'EOF'
  52. The <info>%command.name%</info> command clears and warms up the application cache for a given environment
  53. and debug mode:
  54.   <info>php %command.full_name% --env=dev</info>
  55.   <info>php %command.full_name% --env=prod --no-debug</info>
  56. EOF
  57.             )
  58.         ;
  59.     }
  60.     protected function execute(InputInterface $inputOutputInterface $output): int
  61.     {
  62.         $fs $this->filesystem;
  63.         $io = new SymfonyStyle($input$output);
  64.         $kernel $this->getApplication()->getKernel();
  65.         $realCacheDir $kernel->getContainer()->getParameter('kernel.cache_dir');
  66.         $realBuildDir $kernel->getContainer()->hasParameter('kernel.build_dir') ? $kernel->getContainer()->getParameter('kernel.build_dir') : $realCacheDir;
  67.         // the old cache dir name must not be longer than the real one to avoid exceeding
  68.         // the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
  69.         $oldCacheDir substr($realCacheDir0, -1).(str_ends_with($realCacheDir'~') ? '+' '~');
  70.         $fs->remove($oldCacheDir);
  71.         if (!is_writable($realCacheDir)) {
  72.             throw new RuntimeException(sprintf('Unable to write in the "%s" directory.'$realCacheDir));
  73.         }
  74.         $useBuildDir $realBuildDir !== $realCacheDir;
  75.         $oldBuildDir substr($realBuildDir0, -1).(str_ends_with($realBuildDir'~') ? '+' '~');
  76.         if ($useBuildDir) {
  77.             $fs->remove($oldBuildDir);
  78.             if (!is_writable($realBuildDir)) {
  79.                 throw new RuntimeException(sprintf('Unable to write in the "%s" directory.'$realBuildDir));
  80.             }
  81.             if ($this->isNfs($realCacheDir)) {
  82.                 $fs->remove($realCacheDir);
  83.             } else {
  84.                 $fs->rename($realCacheDir$oldCacheDir);
  85.             }
  86.             $fs->mkdir($realCacheDir);
  87.         }
  88.         $io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>'$kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
  89.         if ($useBuildDir) {
  90.             $this->cacheClearer->clear($realBuildDir);
  91.         }
  92.         $this->cacheClearer->clear($realCacheDir);
  93.         // The current event dispatcher is stale, let's not use it anymore
  94.         $this->getApplication()->setDispatcher(new EventDispatcher());
  95.         $containerFile = (new \ReflectionObject($kernel->getContainer()))->getFileName();
  96.         $containerDir basename(\dirname($containerFile));
  97.         // the warmup cache dir name must have the same length as the real one
  98.         // to avoid the many problems in serialized resources files
  99.         $warmupDir substr($realBuildDir0, -1).(str_ends_with($realBuildDir'_') ? '-' '_');
  100.         if ($output->isVerbose() && $fs->exists($warmupDir)) {
  101.             $io->comment('Clearing outdated warmup directory...');
  102.         }
  103.         $fs->remove($warmupDir);
  104.         if ($_SERVER['REQUEST_TIME'] <= filemtime($containerFile) && filemtime($containerFile) <= time()) {
  105.             if ($output->isVerbose()) {
  106.                 $io->comment('Cache is fresh.');
  107.             }
  108.             if (!$input->getOption('no-warmup') && !$input->getOption('no-optional-warmers')) {
  109.                 if ($output->isVerbose()) {
  110.                     $io->comment('Warming up optional cache...');
  111.                 }
  112.                 $this->warmupOptionals($realCacheDir);
  113.             }
  114.         } else {
  115.             $fs->mkdir($warmupDir);
  116.             if (!$input->getOption('no-warmup')) {
  117.                 if ($output->isVerbose()) {
  118.                     $io->comment('Warming up cache...');
  119.                 }
  120.                 $this->warmup($warmupDir$realBuildDir);
  121.                 if (!$input->getOption('no-optional-warmers')) {
  122.                     if ($output->isVerbose()) {
  123.                         $io->comment('Warming up optional cache...');
  124.                     }
  125.                     $this->warmupOptionals($realCacheDir);
  126.                 }
  127.             }
  128.             if (!$fs->exists($warmupDir.'/'.$containerDir)) {
  129.                 $fs->rename($realBuildDir.'/'.$containerDir$warmupDir.'/'.$containerDir);
  130.                 touch($warmupDir.'/'.$containerDir.'.legacy');
  131.             }
  132.             if ($this->isNfs($realBuildDir)) {
  133.                 $io->note('For better performances, you should move the cache and log directories to a non-shared folder of the VM.');
  134.                 $fs->remove($realBuildDir);
  135.             } else {
  136.                 $fs->rename($realBuildDir$oldBuildDir);
  137.             }
  138.             $fs->rename($warmupDir$realBuildDir);
  139.             if ($output->isVerbose()) {
  140.                 $io->comment('Removing old build and cache directory...');
  141.             }
  142.             if ($useBuildDir) {
  143.                 try {
  144.                     $fs->remove($oldBuildDir);
  145.                 } catch (IOException $e) {
  146.                     if ($output->isVerbose()) {
  147.                         $io->warning($e->getMessage());
  148.                     }
  149.                 }
  150.             }
  151.             try {
  152.                 $fs->remove($oldCacheDir);
  153.             } catch (IOException $e) {
  154.                 if ($output->isVerbose()) {
  155.                     $io->warning($e->getMessage());
  156.                 }
  157.             }
  158.         }
  159.         if ($output->isVerbose()) {
  160.             $io->comment('Finished');
  161.         }
  162.         $io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully cleared.'$kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
  163.         return 0;
  164.     }
  165.     private function isNfs(string $dir): bool
  166.     {
  167.         static $mounts null;
  168.         if (null === $mounts) {
  169.             $mounts = [];
  170.             if ('/' === \DIRECTORY_SEPARATOR && $files = @file('/proc/mounts')) {
  171.                 foreach ($files as $mount) {
  172.                     $mount \array_slice(explode(' '$mount), 1, -3);
  173.                     if (!\in_array(array_pop($mount), ['vboxsf''nfs'])) {
  174.                         continue;
  175.                     }
  176.                     $mounts[] = implode(' '$mount).'/';
  177.                 }
  178.             }
  179.         }
  180.         foreach ($mounts as $mount) {
  181.             if (str_starts_with($dir$mount)) {
  182.                 return true;
  183.             }
  184.         }
  185.         return false;
  186.     }
  187.     private function warmup(string $warmupDirstring $realBuildDir): void
  188.     {
  189.         // create a temporary kernel
  190.         $kernel $this->getApplication()->getKernel();
  191.         if (!$kernel instanceof RebootableInterface) {
  192.             throw new \LogicException('Calling "cache:clear" with a kernel that does not implement "Symfony\Component\HttpKernel\RebootableInterface" is not supported.');
  193.         }
  194.         $kernel->reboot($warmupDir);
  195.         // fix references to cached files with the real cache directory name
  196.         $search = [$warmupDirstr_replace('\\''\\\\'$warmupDir)];
  197.         $replace str_replace('\\''/'$realBuildDir);
  198.         foreach (Finder::create()->files()->in($warmupDir) as $file) {
  199.             $content str_replace($search$replacefile_get_contents($file), $count);
  200.             if ($count) {
  201.                 file_put_contents($file$content);
  202.             }
  203.         }
  204.     }
  205.     private function warmupOptionals(string $realCacheDir): void
  206.     {
  207.         $kernel $this->getApplication()->getKernel();
  208.         $warmer $kernel->getContainer()->get('cache_warmer');
  209.         // non optional warmers already ran during container compilation
  210.         $warmer->enableOnlyOptionalWarmers();
  211.         $preload = (array) $warmer->warmUp($realCacheDir);
  212.         if ($preload && file_exists($preloadFile $realCacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
  213.             Preloader::append($preloadFile$preload);
  214.         }
  215.     }
  216. }