‪TYPO3CMS  ‪main
CacheFlushCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\EventDispatcherInterface;
22 use Symfony\Component\Console\Command\Command;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Symfony\Component\Console\Input\InputOption;
25 use Symfony\Component\Console\Output\OutputInterface;
31 
32 class ‪CacheFlushCommand extends Command
33 {
34  public function ‪__construct(
35  protected readonly ‪BootService $bootService,
36  protected readonly ‪FrontendInterface $dependencyInjectionCache
37  ) {
38  parent::__construct('cache:flush');
39  }
40 
44  protected function ‪configure(): void
45  {
46  $this->setDescription('Flush TYPO3 caches.');
47  $this->setHelp('This command is useful for deployments to clear caches during release postparation.');
48  $this->setDefinition([
49  new InputOption('group', 'g', InputOption::VALUE_OPTIONAL, 'The cache group to flush (system, pages, di or all)', 'all'),
50  ]);
51  }
52 
56  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
57  {
58  $group = $input->getOption('group') ?? 'all';
59 
60  $this->‪flushDependencyInjectionCaches($group);
61  if ($group === 'di') {
62  return Command::SUCCESS;
63  }
64 
65  $container = $this->bootService->getContainer(true);
66 
67  $this->‪flushCoreCaches($group, $container);
68 
69  $this->bootService->loadExtLocalconfDatabaseAndExtTables(false, true);
70 
71  $eventDispatcher = $container->get(EventDispatcherInterface::class);
72 
73  $groups = $group === 'all' ? $container->get(CacheManager::class)->getCacheGroups() : [$group];
74  $event = new ‪CacheFlushEvent($groups);
75  $eventDispatcher->dispatch($event);
76 
77  if (count($event->getErrors()) > 0) {
78  return Command::FAILURE;
79  }
80 
81  return Command::SUCCESS;
82  }
83 
84  protected function ‪flushDependencyInjectionCaches(string $group): void
85  {
86  if ($group !== 'di' && $group !== 'system' && $group !== 'all') {
87  return;
88  }
89 
90  if ($this->dependencyInjectionCache->getBackend() instanceof ‪ContainerBackend) {
91  $diCacheBackend = $this->dependencyInjectionCache->getBackend();
92  // We need to remove using the forceFlush method because the DI cache backend disables the flush method
93  $diCacheBackend->forceFlush();
94  }
95  }
96 
97  protected function ‪flushCoreCaches(string $group, ContainerInterface $container): void
98  {
99  if ($group !== 'system' && $group !== 'all') {
100  return;
101  }
102 
103  $container->get('cache.core')->flush();
104  }
105 }
‪TYPO3\CMS\Core\DependencyInjection\Cache\ContainerBackend
Definition: ContainerBackend.php:26
‪TYPO3\CMS\Core\Command\CacheFlushCommand
Definition: CacheFlushCommand.php:33
‪TYPO3\CMS\Core\Command\CacheFlushCommand\flushDependencyInjectionCaches
‪flushDependencyInjectionCaches(string $group)
Definition: CacheFlushCommand.php:84
‪TYPO3\CMS\Core\Command\CacheFlushCommand\__construct
‪__construct(protected readonly BootService $bootService, protected readonly FrontendInterface $dependencyInjectionCache)
Definition: CacheFlushCommand.php:34
‪TYPO3\CMS\Core\Command\CacheFlushCommand\flushCoreCaches
‪flushCoreCaches(string $group, ContainerInterface $container)
Definition: CacheFlushCommand.php:97
‪TYPO3\CMS\Core\Command\CacheFlushCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: CacheFlushCommand.php:56
‪TYPO3\CMS\Core\Core\BootService
Definition: BootService.php:35
‪TYPO3\CMS\Core\Command\CacheFlushCommand\configure
‪configure()
Definition: CacheFlushCommand.php:44
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Cache\Event\CacheFlushEvent
Definition: CacheFlushEvent.php:24
‪TYPO3\CMS\Core\Command
Definition: CacheFlushCommand.php:18