[TASK] Allow multiple extractor services with the same priority
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / CacheService.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Extbase\Service;
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 /**
20 * Cache clearing helper functions
21 * @internal only to be used within Extbase, not part of TYPO3 Core API.
22 */
23 class CacheService implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 /**
26 * @var \SplStack
27 */
28 protected $pageIdStack;
29
30 /**
31 * @var \TYPO3\CMS\Core\Cache\CacheManager
32 */
33 protected $cacheManager;
34
35 /**
36 * @param \TYPO3\CMS\Core\Cache\CacheManager $cacheManager
37 */
38 public function injectCacheManager(\TYPO3\CMS\Core\Cache\CacheManager $cacheManager): void
39 {
40 $this->cacheManager = $cacheManager;
41 }
42
43 /**
44 * Initializes the pageIdStack
45 */
46 public function __construct()
47 {
48 $this->pageIdStack = new \SplStack;
49 }
50
51 /**
52 * @return \SplStack
53 */
54 public function getPageIdStack(): \SplStack
55 {
56 return $this->pageIdStack;
57 }
58
59 /**
60 * Clears the page cache
61 *
62 * @param int|int[] $pageIdsToClear single or multiple pageIds to clear the cache for
63 */
64 public function clearPageCache($pageIdsToClear = null): void
65 {
66 if ($pageIdsToClear === null) {
67 $this->cacheManager->flushCachesInGroup('pages');
68 } else {
69 if (!is_array($pageIdsToClear)) {
70 $pageIdsToClear = [(int)$pageIdsToClear];
71 }
72 $tags = array_map(function ($item) {
73 return 'pageId_' . $item;
74 }, $pageIdsToClear);
75 $this->cacheManager->flushCachesInGroupByTags('pages', $tags);
76 }
77 }
78
79 /**
80 * Walks through the pageIdStack, collects all pageIds
81 * as array and passes them on to clearPageCache.
82 */
83 public function clearCachesOfRegisteredPageIds(): void
84 {
85 if (!$this->pageIdStack->isEmpty()) {
86 $pageIds = [];
87 while (!$this->pageIdStack->isEmpty()) {
88 $pageIds[] = (int)$this->pageIdStack->pop();
89 }
90 $pageIds = array_values(array_unique($pageIds));
91 $this->clearPageCache($pageIds);
92 }
93 }
94 }