1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Configuration Object project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\ConfigurationObject\Core\Service; |
15
|
|
|
|
16
|
|
|
use TYPO3\CMS\Core\Cache\Backend\FileBackend; |
17
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
18
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
19
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; |
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
21
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
22
|
|
|
|
23
|
|
|
class CacheService implements SingletonInterface |
24
|
|
|
{ |
25
|
|
|
const CACHE_IDENTIFIER = 'cache_configuration_object'; |
26
|
|
|
const CACHE_TAG_DYNAMIC_CACHE = 'dynamic-cache'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Options for the internal cache. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $cacheOptions = [ |
34
|
|
|
'backend' => FileBackend::class, |
35
|
|
|
'frontend' => VariableFrontend::class, |
36
|
|
|
'groups' => ['all', 'system'] |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var CacheManager |
41
|
|
|
*/ |
42
|
|
|
protected $cacheManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Function called from `ext_localconf` file which will register the |
46
|
|
|
* internal cache earlier. |
47
|
|
|
* |
48
|
|
|
* @internal |
49
|
|
|
*/ |
50
|
|
|
public function registerInternalCache() |
51
|
|
|
{ |
52
|
|
|
$this->getCacheManager() |
53
|
|
|
->setCacheConfigurations([self::CACHE_IDENTIFIER => $this->cacheOptions]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This function will take care of initializing all caches that were defined |
58
|
|
|
* previously by the `CacheService` which allows dynamic caches to be used |
59
|
|
|
* for every configuration object type. |
60
|
|
|
* |
61
|
|
|
* @see \Romm\ConfigurationObject\Service\Items\Cache\CacheService::initialize() |
62
|
|
|
* @internal |
63
|
|
|
*/ |
64
|
|
|
public function registerDynamicCaches() |
65
|
|
|
{ |
66
|
|
|
$dynamicCaches = $this->getCache()->getByTag(self::CACHE_TAG_DYNAMIC_CACHE); |
67
|
|
|
|
68
|
|
|
foreach ($dynamicCaches as $cacheData) { |
69
|
|
|
$identifier = $cacheData['identifier']; |
70
|
|
|
$options = $cacheData['options']; |
71
|
|
|
|
72
|
|
|
$this->registerCacheInternal($identifier, $options); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Registers a new dynamic cache: the cache will be added to the cache |
78
|
|
|
* manager after this function is executed. Its configuration will also be |
79
|
|
|
* remembered so the cache will be registered properly during the cache |
80
|
|
|
* framework initialization in further requests. |
81
|
|
|
* |
82
|
|
|
* @param string $identifier |
83
|
|
|
* @param array $options |
84
|
|
|
*/ |
85
|
|
|
public function registerDynamicCache($identifier, array $options) |
86
|
|
|
{ |
87
|
|
|
if (false === $this->getCache()->has($identifier)) { |
88
|
|
|
$this->getCache()->set( |
89
|
|
|
$identifier, |
90
|
|
|
[ |
91
|
|
|
'identifier' => $identifier, |
92
|
|
|
'options' => $options |
93
|
|
|
], |
94
|
|
|
[self::CACHE_TAG_DYNAMIC_CACHE] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->registerCacheInternal($identifier, $options); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $identifier |
103
|
|
|
* @param array $options |
104
|
|
|
*/ |
105
|
|
|
protected function registerCacheInternal($identifier, array $options) |
106
|
|
|
{ |
107
|
|
|
$cacheManager = $this->getCacheManager(); |
108
|
|
|
|
109
|
|
|
if (false === $cacheManager->hasCache($identifier)) { |
110
|
|
|
$cacheManager->setCacheConfigurations([$identifier => $options]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return FrontendInterface |
116
|
|
|
*/ |
117
|
|
|
protected function getCache() |
118
|
|
|
{ |
119
|
|
|
return $this->getCacheManager()->getCache(self::CACHE_IDENTIFIER); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return CacheManager |
124
|
|
|
*/ |
125
|
|
|
protected function getCacheManager() |
126
|
|
|
{ |
127
|
|
|
if (null === $this->cacheManager) { |
128
|
|
|
/** @var CacheManager $cacheManager */ |
129
|
|
|
$this->cacheManager = GeneralUtility::makeInstance(CacheManager::class); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->cacheManager; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|