setDescription('Flush TYPO3 caches.'); $this->setHelp( 'Clears TYPO3 caches. ' . 'Useful after code changes during development or after deployments. ' . 'You can flush a specific cache group (system, pages, di) or all caches.' ); $this->setDefinition([ new InputOption('group', 'g', InputOption::VALUE_OPTIONAL, 'Cache group to flush (system, pages, di, or all).', 'all'), ]); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $group = $input->getOption('group') ?? 'all'; $this->flushDependencyInjectionCaches($group); if ($group === 'di') { if ($output->isVerbose()) { $io->success('Dependency Injection caches flushed.'); } return Command::SUCCESS; } $container = $this->bootService->getContainer(true); $this->flushCoreCaches($group, $container); $this->bootService->loadExtLocalconfDatabase(false, true); $eventDispatcher = $container->get(EventDispatcherInterface::class); $groups = $group === 'all' ? $container->get(CacheManager::class)->getCacheGroups() : [$group]; $event = new CacheFlushEvent($groups); $eventDispatcher->dispatch($event); if (count($event->getErrors()) > 0) { $io->error('Errors occurred while flushing caches.'); foreach ($event->getErrors() as $error) { $io->error($error); } return Command::FAILURE; } if ($output->isVerbose()) { if ($group === 'all') { $io->success('All caches flushed.'); } else { $io->success(sprintf('Caches for group "%s" flushed.', $group)); } } return Command::SUCCESS; } protected function flushDependencyInjectionCaches(string $group): void { if ($group !== 'di' && $group !== 'system' && $group !== 'all') { return; } if ($this->dependencyInjectionCache->getBackend() instanceof ContainerBackend) { $diCacheBackend = $this->dependencyInjectionCache->getBackend(); // We need to remove using the forceFlush method because the DI cache backend disables the flush method $diCacheBackend->forceFlush(); } } protected function flushCoreCaches(string $group, ContainerInterface $container): void { if ($group !== 'system' && $group !== 'all') { return; } $container->get('cache.core')->flush(); } }