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