‪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;
33 
34 class ‪CacheFlushCommand extends Command
35 {
36  public function ‪__construct(
37  protected readonly ‪BootService $bootService,
38  protected readonly ‪FrontendInterface $dependencyInjectionCache
39  ) {
40  parent::__construct('cache:flush');
41  }
42 
46  protected function ‪configure(): void
47  {
48  $this->setDescription('Flush TYPO3 caches.');
49  $this->setHelp('This command is useful for deployments to clear caches during release postparation.');
50  $this->setDefinition([
51  new InputOption('group', 'g', InputOption::VALUE_OPTIONAL, 'The cache group to flush (system, pages, di or all)', 'all'),
52  ]);
53  }
54 
58  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
59  {
60  $group = $input->getOption('group') ?? 'all';
61 
62  $this->‪flushLegacyDatabaseCacheTables($group);
63  $this->‪flushDependencyInjectionCaches($group);
64  if ($group === 'di') {
65  return Command::SUCCESS;
66  }
67 
68  $container = $this->bootService->getContainer(true);
69 
70  $this->‪flushCoreCaches($group, $container);
71 
72  $this->bootService->loadExtLocalconfDatabaseAndExtTables(false, true);
73 
74  $eventDispatcher = $container->get(EventDispatcherInterface::class);
75 
76  $groups = $group === 'all' ? $container->get(CacheManager::class)->getCacheGroups() : [$group];
77  $event = new ‪CacheFlushEvent($groups);
78  $eventDispatcher->dispatch($event);
79 
80  if (count($event->getErrors()) > 0) {
81  return Command::FAILURE;
82  }
83 
84  return Command::SUCCESS;
85  }
86 
87  protected function ‪flushDependencyInjectionCaches(string $group): void
88  {
89  if ($group !== 'di' && $group !== 'system' && $group !== 'all') {
90  return;
91  }
92 
93  if ($this->dependencyInjectionCache->getBackend() instanceof ‪ContainerBackend) {
94  $diCacheBackend = $this->dependencyInjectionCache->getBackend();
95  // We need to remove using the forceFlush method because the DI cache backend disables the flush method
96  $diCacheBackend->forceFlush();
97  }
98  }
99 
100  protected function ‪flushCoreCaches(string $group, ContainerInterface $container): void
101  {
102  if ($group !== 'system' && $group !== 'all') {
103  return;
104  }
105 
106  $container->get('cache.core')->flush();
107  }
108 
109  protected function ‪flushLegacyDatabaseCacheTables($group): void
110  {
111  if ($group !== 'all') {
112  return;
113  }
114 
115  $legacyDatabaseCacheTables = [
116  'cache_treelist',
117  ];
118 
119  // Low level flush of legacy database cache tables
120  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
121  foreach ($legacyDatabaseCacheTables as $tableName) {
122  $connection = $connectionPool->getConnectionForTable($tableName);
123  $connection->truncate($tableName);
124  }
125  }
126 }
‪TYPO3\CMS\Core\DependencyInjection\Cache\ContainerBackend
Definition: ContainerBackend.php:26
‪TYPO3\CMS\Core\Command\CacheFlushCommand
Definition: CacheFlushCommand.php:35
‪TYPO3\CMS\Core\Command\CacheFlushCommand\flushDependencyInjectionCaches
‪flushDependencyInjectionCaches(string $group)
Definition: CacheFlushCommand.php:87
‪TYPO3\CMS\Core\Command\CacheFlushCommand\__construct
‪__construct(protected readonly BootService $bootService, protected readonly FrontendInterface $dependencyInjectionCache)
Definition: CacheFlushCommand.php:36
‪TYPO3\CMS\Core\Command\CacheFlushCommand\flushCoreCaches
‪flushCoreCaches(string $group, ContainerInterface $container)
Definition: CacheFlushCommand.php:100
‪TYPO3\CMS\Core\Command\CacheFlushCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: CacheFlushCommand.php:58
‪TYPO3\CMS\Core\Core\BootService
Definition: BootService.php:35
‪TYPO3\CMS\Core\Command\CacheFlushCommand\configure
‪configure()
Definition: CacheFlushCommand.php:46
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Command\CacheFlushCommand\flushLegacyDatabaseCacheTables
‪flushLegacyDatabaseCacheTables($group)
Definition: CacheFlushCommand.php:109
‪TYPO3\CMS\Core\Cache\Event\CacheFlushEvent
Definition: CacheFlushEvent.php:24
‪TYPO3\CMS\Core\Command
Definition: CacheFlushCommand.php:18
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52