[TASK] Make "$hookObject must implement interface FooInterface" more verbose
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / ClearCacheToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
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 use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
18 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Page\PageRenderer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Render cache clearing toolbar item
27 */
28 class ClearCacheToolbarItem implements ToolbarItemInterface
29 {
30 /**
31 * @var array
32 */
33 protected $cacheActions = array();
34
35 /**
36 * @var array
37 */
38 protected $optionValues = array();
39
40 /**
41 * @var IconFactory
42 */
43 protected $iconFactory;
44
45 /**
46 * Constructor
47 *
48 * @throws \UnexpectedValueException
49 */
50 public function __construct()
51 {
52 $backendUser = $this->getBackendUser();
53 $languageService = $this->getLanguageService();
54 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
55
56 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
57
58 // Clear all page-related caches
59 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.pages')) {
60 $this->cacheActions[] = array(
61 'id' => 'pages',
62 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesTitle', true),
63 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushPageCachesDescription', true),
64 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'pages', 'ajaxCall' => 1]),
65 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-low', Icon::SIZE_SMALL)->render()
66 );
67 $this->optionValues[] = 'pages';
68 }
69
70 // Clear cache for ALL tables!
71 if ($backendUser->isAdmin() || $backendUser->getTSConfigVal('options.clearCache.all')) {
72 $this->cacheActions[] = array(
73 'id' => 'all',
74 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesTitle', true),
75 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushGeneralCachesDescription', true),
76 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'all', 'ajaxCall' => 1]),
77 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-medium', Icon::SIZE_SMALL)->render()
78 );
79 $this->optionValues[] = 'all';
80 }
81
82 // Clearing of system cache (core cache, class cache etc)
83 // is only shown explicitly if activated for a BE-user (not activated for admins by default)
84 // or if the system runs in development mode (only for admins)
85 // or if $GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] is set (only for admins)
86 if (
87 $backendUser->getTSConfigVal('options.clearCache.system')
88 || (GeneralUtility::getApplicationContext()->isDevelopment() && $backendUser->isAdmin())
89 || ((bool)$GLOBALS['TYPO3_CONF_VARS']['SYS']['clearCacheSystem'] === true && $backendUser->isAdmin())
90 ) {
91 $this->cacheActions[] = array(
92 'id' => 'system',
93 'title' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesTitle', true),
94 'description' => $languageService->sL('LLL:EXT:lang/locallang_core.xlf:flushSystemCachesDescription', true),
95 'href' => BackendUtility::getModuleUrl('tce_db', ['vC' => $backendUser->veriCode(), 'cacheCmd' => 'system', 'ajaxCall' => 1]),
96 'icon' => $this->iconFactory->getIcon('actions-system-cache-clear-impact-high', Icon::SIZE_SMALL)->render()
97 );
98 $this->optionValues[] = 'system';
99 }
100
101 // Hook for manipulating cacheActions
102 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'])) {
103 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] as $cacheAction) {
104 $hookObject = GeneralUtility::getUserObj($cacheAction);
105 if (!$hookObject instanceof ClearCacheActionsHookInterface) {
106 throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
107 }
108 $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
109 }
110 }
111 }
112
113 /**
114 * Checks whether the user has access to this toolbar item
115 *
116 * @return bool TRUE if user has access, FALSE if not
117 */
118 public function checkAccess()
119 {
120 $backendUser = $this->getBackendUser();
121 if ($backendUser->isAdmin()) {
122 return true;
123 }
124 if (is_array($this->optionValues)) {
125 foreach ($this->optionValues as $value) {
126 if ($backendUser->getTSConfigVal('options.clearCache.' . $value)) {
127 return true;
128 }
129 }
130 }
131 return false;
132 }
133
134 /**
135 * Render clear cache icon
136 *
137 * @return string Icon HTML
138 */
139 public function getItem()
140 {
141 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.clearCache_clearCache', true);
142 return '<span title="' . $title . '">'
143 . $this->iconFactory->getIcon('apps-toolbar-menu-cache', Icon::SIZE_SMALL)->render('inline')
144 . '</span>';
145 }
146
147 /**
148 * Render drop down
149 *
150 * @return string Drop down HTML
151 */
152 public function getDropDown()
153 {
154 $result = array();
155 $result[] = '<ul class="dropdown-list">';
156 foreach ($this->cacheActions as $cacheAction) {
157 $title = $cacheAction['description'] ?: $cacheAction['title'];
158 $result[] = '<li>';
159 $result[] = '<a class="dropdown-list-link" href="' . htmlspecialchars($cacheAction['href']) . '" title="' . htmlspecialchars($title) . '">';
160 $result[] = $cacheAction['icon'] . ' ' . htmlspecialchars($cacheAction['title']);
161 $result[] = '</a>';
162 $result[] = '</li>';
163 }
164 $result[] = '</ul>';
165 return implode(LF, $result);
166 }
167
168 /**
169 * No additional attributes needed.
170 *
171 * @return array
172 */
173 public function getAdditionalAttributes()
174 {
175 return array();
176 }
177
178 /**
179 * This item has a drop down
180 *
181 * @return bool
182 */
183 public function hasDropDown()
184 {
185 return true;
186 }
187
188 /**
189 * Position relative to others
190 *
191 * @return int
192 */
193 public function getIndex()
194 {
195 return 25;
196 }
197
198 /**
199 * Returns the current BE user.
200 *
201 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
202 */
203 protected function getBackendUser()
204 {
205 return $GLOBALS['BE_USER'];
206 }
207
208 /**
209 * Returns current PageRenderer
210 *
211 * @return PageRenderer
212 */
213 protected function getPageRenderer()
214 {
215 return GeneralUtility::makeInstance(PageRenderer::class);
216 }
217
218 /**
219 * Returns LanguageService
220 *
221 * @return \TYPO3\CMS\Lang\LanguageService
222 */
223 protected function getLanguageService()
224 {
225 return $GLOBALS['LANG'];
226 }
227 }