‪TYPO3CMS  9.5
ClearCacheToolbarItem.php
Go to the documentation of this file.
1 <?php
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 
24 
32 {
36  protected ‪$cacheActions = [];
37 
41  protected ‪$optionValues = [];
42 
46  public function ‪__construct()
47  {
48  $this->‪getPageRenderer()->‪loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/ClearCacheMenu');
49  $isAdmin = $this->‪getBackendUser()->‪isAdmin();
50  $userTsConfig = $this->‪getBackendUser()->‪getTSConfig();
51  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
52 
53  // Clear all page-related caches
54  if ($isAdmin || $userTsConfig['options.']['clearCache.']['pages'] ?? false) {
55  $this->cacheActions[] = [
56  'id' => 'pages',
57  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle',
58  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription',
59  'href' => (string)$uriBuilder->buildUriFromRoute('tce_db', ['cacheCmd' => 'pages']),
60  'iconIdentifier' => 'actions-system-cache-clear-impact-low'
61  ];
62  $this->optionValues[] = 'pages';
63  }
64 
65  // Clearing of all caches is only shown if explicitly enabled via TSConfig
66  // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
67  // This is useful for big production systems where admins accidentally could slow down the system.
68  if (($userTsConfig['options.']['clearCache.']['all'] ?? false)
69  || ($isAdmin && (bool)($userTsConfig['options.']['clearCache.']['all'] ?? true))
70  ) {
71  $this->cacheActions[] = [
72  'id' => 'all',
73  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2',
74  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2',
75  'href' => (string)$uriBuilder->buildUriFromRoute('tce_db', ['cacheCmd' => 'all']),
76  'iconIdentifier' => 'actions-system-cache-clear-impact-high'
77  ];
78  $this->optionValues[] = 'all';
79  }
80 
81  // Hook for manipulating cacheActions
82  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'] ?? [] as $cacheAction) {
83  $hookObject = GeneralUtility::makeInstance($cacheAction);
84  if (!$hookObject instanceof ‪ClearCacheActionsHookInterface) {
85  throw new \UnexpectedValueException($cacheAction . ' must implement interface ' . ClearCacheActionsHookInterface::class, 1228262000);
86  }
87  $hookObject->manipulateCacheActions($this->cacheActions, $this->optionValues);
88  }
89  }
90 
96  public function ‪checkAccess()
97  {
98  $backendUser = $this->‪getBackendUser();
99  if ($backendUser->isAdmin()) {
100  return true;
101  }
102  foreach ($this->optionValues as $value) {
103  if ($backendUser->getTSConfig()['options.']['clearCache.'][$value] ?? false) {
104  return true;
105  }
106  }
107  return false;
108  }
109 
115  public function ‪getItem()
116  {
117  if ($this->‪hasDropDown()) {
118  return $this->‪getFluidTemplateObject('ClearCacheToolbarItem.html')->‪render();
119  }
120  $view = $this->‪getFluidTemplateObject('ClearCacheToolbarItemSingle.html');
121  $cacheAction = end($this->cacheActions);
122  $view->assignMultiple([
123  'link' => $cacheAction['href'],
124  'title' => $cacheAction['title'],
125  'iconIdentifier' => $cacheAction['iconIdentifier'],
126  ]);
127  return $view->render();
128  }
129 
135  public function ‪getDropDown()
136  {
137  $view = $this->‪getFluidTemplateObject('ClearCacheToolbarItemDropDown.html');
138  $view->assign('cacheActions', $this->cacheActions);
139  return $view->render();
140  }
141 
147  public function ‪getAdditionalAttributes()
148  {
149  return [];
150  }
151 
157  public function ‪hasDropDown()
158  {
159  return count($this->cacheActions) > 1;
160  }
161 
167  public function ‪getIndex()
168  {
169  return 25;
170  }
171 
177  protected function ‪getBackendUser()
178  {
179  return ‪$GLOBALS['BE_USER'];
180  }
181 
185  protected function ‪getPageRenderer()
186  {
187  return GeneralUtility::makeInstance(PageRenderer::class);
188  }
189 
196  protected function ‪getFluidTemplateObject(string $filename): ‪StandaloneView
197  {
198  $view = GeneralUtility::makeInstance(StandaloneView::class);
199  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
200  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
201  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
202 
203  $view->setTemplate($filename);
204 
205  $view->getRequest()->setControllerExtensionName('Backend');
206  return $view;
207  }
208 }
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:24
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$optionValues
‪array $optionValues
Definition: ClearCacheToolbarItem.php:39
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\hasDropDown
‪bool hasDropDown()
Definition: ClearCacheToolbarItem.php:155
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\checkAccess
‪bool checkAccess()
Definition: ClearCacheToolbarItem.php:94
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: ClearCacheToolbarItem.php:183
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:294
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig($objectString=null, $config=null)
Definition: BackendUserAuthentication.php:1232
‪TYPO3\CMS\Core\Page\PageRenderer\loadRequireJsModule
‪loadRequireJsModule($mainModuleName, $callBackFunction=null)
Definition: PageRenderer.php:1593
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface
Definition: ClearCacheActionsHookInterface.php:21
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:2
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getIndex
‪int getIndex()
Definition: ClearCacheToolbarItem.php:165
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\__construct
‪__construct()
Definition: ClearCacheToolbarItem.php:44
‪$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:133
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface\render
‪string render()
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$cacheActions
‪array $cacheActions
Definition: ClearCacheToolbarItem.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getItem
‪string getItem()
Definition: ClearCacheToolbarItem.php:113
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ClearCacheToolbarItem.php:175
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getAdditionalAttributes
‪array getAdditionalAttributes()
Definition: ClearCacheToolbarItem.php:145
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem
Definition: ClearCacheToolbarItem.php:32
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getFluidTemplateObject
‪StandaloneView getFluidTemplateObject(string $filename)
Definition: ClearCacheToolbarItem.php:194