fcbf1de56fda9b8b91f0caec08ff38193c31f78c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / CacheService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Cache clearing helper functions
19 * @internal only to be used within Extbase, not part of TYPO3 Core API.
20 */
21 class CacheService implements \TYPO3\CMS\Core\SingletonInterface
22 {
23 /**
24 * @var \SplStack
25 */
26 protected $pageIdStack;
27
28 /**
29 * @var \TYPO3\CMS\Core\Cache\CacheManager
30 */
31 protected $cacheManager;
32
33 /**
34 * @param \TYPO3\CMS\Core\Cache\CacheManager $cacheManager
35 */
36 public function injectCacheManager(\TYPO3\CMS\Core\Cache\CacheManager $cacheManager)
37 {
38 $this->cacheManager = $cacheManager;
39 }
40
41 /**
42 * Initializes the pageIdStack
43 */
44 public function __construct()
45 {
46 $this->pageIdStack = new \SplStack;
47 }
48
49 /**
50 * @return \SplStack
51 */
52 public function getPageIdStack()
53 {
54 return $this->pageIdStack;
55 }
56
57 /**
58 * Clears the page cache
59 *
60 * @param mixed $pageIdsToClear (int) single or (array) multiple pageIds to clear the cache for
61 */
62 public function clearPageCache($pageIdsToClear = null)
63 {
64 if ($pageIdsToClear === null) {
65 $this->cacheManager->flushCachesInGroup('pages');
66 } else {
67 if (!is_array($pageIdsToClear)) {
68 $pageIdsToClear = [(int)$pageIdsToClear];
69 }
70 $tags = array_map(function ($item) {
71 return 'pageId_' . $item;
72 }, $pageIdsToClear);
73 $this->cacheManager->flushCachesInGroupByTags('pages', $tags);
74 }
75 }
76
77 /**
78 * Walks through the pageIdStack, collects all pageIds
79 * as array and passes them on to clearPageCache.
80 */
81 public function clearCachesOfRegisteredPageIds()
82 {
83 if (!$this->pageIdStack->isEmpty()) {
84 $pageIds = [];
85 while (!$this->pageIdStack->isEmpty()) {
86 $pageIds[] = (int)$this->pageIdStack->pop();
87 }
88 $pageIds = array_values(array_unique($pageIds));
89 $this->clearPageCache($pageIds);
90 }
91 }
92 }