‪TYPO3CMS  10.4
ClearCacheToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
33 {
37  protected ‪$cacheActions = [];
38 
42  protected ‪$optionValues = [];
43 
47  public function ‪__construct()
48  {
49  $this->‪getPageRenderer()->‪loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
50  $isAdmin = $this->‪getBackendUser()->‪isAdmin();
51  $userTsConfig = $this->‪getBackendUser()->‪getTSConfig();
52  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
53 
54  // Clear all page-related caches
55  if ($isAdmin || $userTsConfig['options.']['clearCache.']['pages'] ?? false) {
56  $this->cacheActions[] = [
57  'id' => 'pages',
58  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle',
59  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription',
60  'href' => (string)$uriBuilder->buildUriFromRoute('tce_db', ['cacheCmd' => 'pages']),
61  'iconIdentifier' => 'actions-system-cache-clear-impact-low'
62  ];
63  $this->optionValues[] = 'pages';
64  }
65 
66  // Clearing of all caches is only shown if explicitly enabled via TSConfig
67  // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
68  // This is useful for big production systems where admins accidentally could slow down the system.
69  if (($userTsConfig['options.']['clearCache.']['all'] ?? false)
70  || ($isAdmin && (bool)($userTsConfig['options.']['clearCache.']['all'] ?? true))
71  ) {
72  $this->cacheActions[] = [
73  'id' => 'all',
74  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2',
75  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2',
76  'href' => (string)$uriBuilder->buildUriFromRoute('tce_db', ['cacheCmd' => 'all']),
77  'iconIdentifier' => 'actions-system-cache-clear-impact-high'
78  ];
79  $this->optionValues[] = 'all';
80  }
81 
82  // Hook for manipulating cacheActions
83  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] ?? [] as $cacheAction) {
84  $hookObject = GeneralUtility::makeInstance($cacheAction);
85  if (!$hookObject instanceof ‪ClearCacheActionsHookInterface) {
86  throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
87  }
88  $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
89  }
90  }
91 
97  public function ‪checkAccess()
98  {
99  $backendUser = $this->‪getBackendUser();
100  if ($backendUser->isAdmin()) {
101  return true;
102  }
103  foreach ($this->optionValues as $value) {
104  if ($backendUser->getTSConfig()['options.']['clearCache.'][$value] ?? false) {
105  return true;
106  }
107  }
108  return false;
109  }
110 
116  public function ‪getItem()
117  {
118  if ($this->‪hasDropDown()) {
119  return $this->‪getFluidTemplateObject('ClearCacheToolbarItem.html')->‪render();
120  }
121  $view = $this->‪getFluidTemplateObject('ClearCacheToolbarItemSingle.html');
122  $cacheAction = end($this->cacheActions);
123  $view->assignMultiple([
124  'link' => $cacheAction['href'],
125  'title' => $cacheAction['title'],
126  'iconIdentifier' => $cacheAction['iconIdentifier'],
127  ]);
128  return $view->render();
129  }
130 
136  public function ‪getDropDown()
137  {
138  $view = $this->‪getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
139  $view->assign('cacheActions', $this->cacheActions);
140  return $view->render();
141  }
142 
148  public function ‪getAdditionalAttributes()
149  {
150  return [];
151  }
152 
158  public function ‪hasDropDown()
159  {
160  return count($this->cacheActions) > 1;
161  }
162 
168  public function ‪getIndex()
169  {
170  return 25;
171  }
172 
178  protected function ‪getBackendUser()
179  {
180  return ‪$GLOBALS['BE_USER'];
181  }
182 
186  protected function ‪getPageRenderer()
187  {
188  return GeneralUtility::makeInstance(PageRenderer::class);
189  }
190 
197  protected function ‪getFluidTemplateObject(string $filename): ‪StandaloneView
198  {
199  $view = GeneralUtility::makeInstance(StandaloneView::class);
200  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
201  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
202  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
203 
204  $view->setTemplate($filename);
205 
206  $view->getRequest()->setControllerExtensionName('Backend');
207  return $view;
208  }
209 }
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:25
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$optionValues
‪array $optionValues
Definition: ClearCacheToolbarItem.php:40
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\hasDropDown
‪bool hasDropDown()
Definition: ClearCacheToolbarItem.php:156
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\checkAccess
‪bool checkAccess()
Definition: ClearCacheToolbarItem.php:95
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig()
Definition: BackendUserAuthentication.php:1217
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: ClearCacheToolbarItem.php:184
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:292
‪TYPO3\CMS\Core\Page\PageRenderer\loadRequireJsModule
‪loadRequireJsModule($mainModuleName, $callBackFunction=null)
Definition: PageRenderer.php:1493
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface
Definition: ClearCacheActionsHookInterface.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:16
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getIndex
‪int getIndex()
Definition: ClearCacheToolbarItem.php:166
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\__construct
‪__construct()
Definition: ClearCacheToolbarItem.php:45
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getDropDown
‪string getDropDown()
Definition: ClearCacheToolbarItem.php:134
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface\render
‪string render()
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$cacheActions
‪array $cacheActions
Definition: ClearCacheToolbarItem.php:36
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getItem
‪string getItem()
Definition: ClearCacheToolbarItem.php:114
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ClearCacheToolbarItem.php:176
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getAdditionalAttributes
‪array getAdditionalAttributes()
Definition: ClearCacheToolbarItem.php:146
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem
Definition: ClearCacheToolbarItem.php:33
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename)
Definition: ClearCacheToolbarItem.php:195