‪TYPO3CMS  ‪main
ClearCacheToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
21 use Psr\Http\Message\ServerRequestInterface;
28 
35 {
36  protected array ‪$cacheActions = [];
37  protected array ‪$optionValues = [];
38  private ServerRequestInterface ‪$request;
39 
40  public function ‪__construct(
41  ‪UriBuilder $uriBuilder,
42  EventDispatcherInterface $eventDispatcher,
43  private readonly ‪BackendViewFactory $backendViewFactory,
44  ) {
45  $isAdmin = $this->‪getBackendUser()->isAdmin();
46  $userTsConfig = $this->‪getBackendUser()->getTSConfig();
47 
48  // Clear all page-related caches
49  if ($isAdmin || ($userTsConfig['options.']['clearCache.']['pages'] ?? false)) {
50  $this->cacheActions[] = [
51  'id' => 'pages',
52  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle',
53  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription',
54  'href' => (string)$uriBuilder->‪buildUriFromRoute('tce_db', ['cacheCmd' => 'pages']),
55  'severity' => 'success',
56  'iconIdentifier' => 'actions-system-cache-clear-impact-low',
57  ];
58  $this->optionValues[] = 'pages';
59  }
60 
61  // Clearing of all caches is only shown if explicitly enabled via TSConfig
62  // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
63  // This is useful for big production systems where admins accidentally could slow down the system.
64  if (($userTsConfig['options.']['clearCache.']['all'] ?? false)
65  || ($isAdmin && (bool)($userTsConfig['options.']['clearCache.']['all'] ?? true))
66  ) {
67  $this->cacheActions[] = [
68  'id' => 'all',
69  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2',
70  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2',
71  'href' => (string)$uriBuilder->‪buildUriFromRoute('tce_db', ['cacheCmd' => 'all']),
72  'severity' => 'danger',
73  'iconIdentifier' => 'actions-system-cache-clear-impact-high',
74  ];
75  $this->optionValues[] = 'all';
76  }
77 
78  $event = new ‪ModifyClearCacheActionsEvent($this->cacheActions, $this->optionValues);
79  $event = $eventDispatcher->dispatch($event);
80  $this->cacheActions = $event->getCacheActions();
81  $this->optionValues = $event->getCacheActionIdentifiers();
82  }
83 
84  public function ‪setRequest(ServerRequestInterface ‪$request): void
85  {
86  $this->request = ‪$request;
87  }
88 
92  public function ‪checkAccess(): bool
93  {
94  $backendUser = $this->‪getBackendUser();
95  if ($backendUser->isAdmin()) {
96  return true;
97  }
98  foreach ($this->optionValues as $value) {
99  if ($backendUser->getTSConfig()['options.']['clearCache.'][$value] ?? false) {
100  return true;
101  }
102  }
103  return false;
104  }
105 
109  public function ‪getItem(): string
110  {
111  $view = $this->backendViewFactory->create($this->request);
112  if ($this->‪hasDropDown()) {
113  return $view->render('ToolbarItems/ClearCacheToolbarItem');
114  }
115  $cacheAction = end($this->cacheActions);
116  $view->assignMultiple([
117  'link' => $cacheAction['href'],
118  'title' => $cacheAction['title'],
119  'iconIdentifier' => $cacheAction['iconIdentifier'],
120  ]);
121  return $view->render('ToolbarItems/ClearCacheToolbarItemSingle');
122  }
123 
127  public function ‪getDropDown(): string
128  {
129  $view = $this->backendViewFactory->create($this->request);
130  $view->assign('cacheActions', $this->cacheActions);
131  return $view->render('ToolbarItems/ClearCacheToolbarItemDropDown');
132  }
133 
137  public function ‪getAdditionalAttributes(): array
138  {
139  return [];
140  }
141 
145  public function ‪hasDropDown(): bool
146  {
147  return count($this->cacheActions) > 1;
148  }
149 
153  public function ‪getIndex(): int
154  {
155  return 25;
156  }
157 
159  {
160  return ‪$GLOBALS['BE_USER'];
161  }
162 }
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getAdditionalAttributes
‪getAdditionalAttributes()
Definition: ClearCacheToolbarItem.php:137
‪TYPO3\CMS\Backend\Routing\UriBuilder\buildUriFromRoute
‪UriInterface buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:152
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$optionValues
‪array $optionValues
Definition: ClearCacheToolbarItem.php:37
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getDropDown
‪getDropDown()
Definition: ClearCacheToolbarItem.php:127
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\hasDropDown
‪hasDropDown()
Definition: ClearCacheToolbarItem.php:145
‪TYPO3\CMS\Backend\Backend\Event\ModifyClearCacheActionsEvent
Definition: ModifyClearCacheActionsEvent.php:24
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getItem
‪getItem()
Definition: ClearCacheToolbarItem.php:109
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:18
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$request
‪ServerRequestInterface $request
Definition: ClearCacheToolbarItem.php:38
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\__construct
‪__construct(UriBuilder $uriBuilder, EventDispatcherInterface $eventDispatcher, private readonly BackendViewFactory $backendViewFactory,)
Definition: ClearCacheToolbarItem.php:40
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\checkAccess
‪checkAccess()
Definition: ClearCacheToolbarItem.php:92
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getIndex
‪getIndex()
Definition: ClearCacheToolbarItem.php:153
‪TYPO3\CMS\Backend\Toolbar\RequestAwareToolbarItemInterface
Definition: RequestAwareToolbarItemInterface.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getBackendUser
‪getBackendUser()
Definition: ClearCacheToolbarItem.php:158
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$cacheActions
‪array $cacheActions
Definition: ClearCacheToolbarItem.php:36
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: ClearCacheToolbarItem.php:84
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem
Definition: ClearCacheToolbarItem.php:35