1 | <?php |
||
24 | class CacheService implements SingletonInterface |
||
25 | { |
||
26 | use ExtendedFacadeInstanceTrait; |
||
27 | |||
28 | const CACHE_IDENTIFIER = 'cache_formz'; |
||
29 | const GENERATED_FILES_PATH = 'typo3temp/Formz/'; |
||
30 | |||
31 | /** |
||
32 | * @var TypoScriptService |
||
33 | */ |
||
34 | protected $typoScriptService; |
||
35 | |||
36 | /** |
||
37 | * @var FrontendInterface |
||
38 | */ |
||
39 | protected $cacheInstance; |
||
40 | |||
41 | /** |
||
42 | * Returns the type of backend cache defined in TypoScript at the path: |
||
43 | * `settings.defaultBackendCache`. |
||
44 | * |
||
45 | * @return string |
||
46 | * @throws \Exception |
||
47 | */ |
||
48 | public function getBackendCache() |
||
49 | { |
||
50 | $backendCache = $this->typoScriptService->getExtensionConfigurationFromPath('settings.defaultBackendCache'); |
||
51 | |||
52 | if (false === class_exists($backendCache) |
||
53 | && false === in_array(AbstractBackend::class, class_parents($backendCache)) |
||
54 | ) { |
||
55 | throw new \Exception( |
||
56 | 'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" must inherit "' . AbstractBackend::class . '" (current value: "' . (string)$backendCache . '")', |
||
57 | 1459251263 |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | return $backendCache; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns the cache instance for this extension. |
||
66 | * |
||
67 | * @return FrontendInterface |
||
68 | */ |
||
69 | public function getCacheInstance() |
||
70 | { |
||
71 | if (null === $this->cacheInstance) { |
||
72 | /** @var $cacheManager CacheManager */ |
||
73 | $cacheManager = Core::instantiate(CacheManager::class); |
||
74 | |||
75 | if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) { |
||
76 | $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return $this->cacheInstance; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param FrontendInterface $cacheInstance |
||
85 | */ |
||
86 | public function setCacheInstance(FrontendInterface $cacheInstance) |
||
87 | { |
||
88 | $this->cacheInstance = $cacheInstance; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Generic cache identifier creation for usages in the extension. |
||
93 | * |
||
94 | * @param string $string |
||
95 | * @param string $formClassName |
||
96 | * @param int $maxLength |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getCacheIdentifier($string, $formClassName, $maxLength = 55) |
||
100 | { |
||
101 | $explodedClassName = explode('\\', $formClassName); |
||
102 | |||
103 | $identifier = strtolower( |
||
104 | $string . |
||
105 | end($explodedClassName) . |
||
106 | '-' . |
||
107 | sha1($formClassName) |
||
108 | ); |
||
109 | |||
110 | return substr($identifier, 0, $maxLength); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param TypoScriptService $typoScriptService |
||
115 | */ |
||
116 | public function injectTypoScriptService(TypoScriptService $typoScriptService) |
||
117 | { |
||
118 | $this->typoScriptService = $typoScriptService; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Function called when clearing TYPO3 caches. It will remove the temporary |
||
123 | * asset files created by Formz. |
||
124 | * |
||
125 | * @param array $parameters |
||
126 | */ |
||
127 | public function clearCacheCommand($parameters) |
||
143 | } |
||
144 |