5ba4f992f12fd2ef5149c34d107fe6af95900fe8
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Hooks / SiteDataHandlerCacheHook.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Hooks;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Cache\CacheManager;
20 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21 use TYPO3\CMS\Core\DataHandling\DataHandler;
22 use TYPO3\CMS\Core\Routing\SiteMatcher;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * When a sys_domain or sys_language record is modified, the Site Handling caches should be flushed.
27 * Also, if pages on root level are changed, site handling caches need flush.
28 *
29 * @internal This class is a hook implementation and is not part of the TYPO3 Core API.
30 */
31 class SiteDataHandlerCacheHook
32 {
33 /**
34 * Called after a record was edited or added.
35 *
36 * @param string $status DataHandler operation status, either 'new' or 'update'
37 * @param string $table The DB table the operation was carried out on
38 * @param mixed $recordId The record's uid for update records, a string to look the record's uid up after it has been created
39 * @param array $updatedFields Array of changed fields and their new values
40 * @param DataHandler $dataHandler DataHandler parent object
41 */
42 public function processDatamap_afterDatabaseOperations(string $status, string $table, $recordId, array $updatedFields, DataHandler $dataHandler)
43 {
44 if ($table === 'sys_domain'
45 || $table === 'sys_language'
46 || ($status === 'new' && $table === 'pages' && (int)$updatedFields['pid'] === 0)
47 ) {
48 $this->getCache()->remove('pseudo-sites');
49 $this->getCache()->remove('legacy-domains');
50 // After evicting caches, we need to make sure these are re-initialized within the
51 // current request if needed. Easiest solution is to purge the SiteMatcher singleton.
52 GeneralUtility::removeSingletonInstance(SiteMatcher::class, GeneralUtility::makeInstance(SiteMatcher::class));
53 }
54 }
55
56 /**
57 * Called after a record was deleted, moved or restored.
58 *
59 * @param string $command the cmd which was executed
60 * @param string $table The DB table the operation was carried out on
61 * @param mixed $id the ID which was operated on
62 * @param mixed $value
63 * @param DataHandler $dataHandler
64 * @param mixed $pasteUpdate
65 * @param array $pasteDatamap
66 */
67 public function processCmdmap_postProcess(string $command, string $table, $id, $value, DataHandler $dataHandler, $pasteUpdate, array $pasteDatamap)
68 {
69 if ($table === 'sys_domain' || $table === 'sys_language') {
70 $this->getCache()->remove('pseudo-sites');
71 $this->getCache()->remove('legacy-domains');
72 }
73 }
74
75 /**
76 * Shorthand method to flush the related caches
77 * @return FrontendInterface
78 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
79 */
80 protected function getCache(): FrontendInterface
81 {
82 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_core');
83 }
84 }