‪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;
22 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
29 
35 #[Autoconfigure(public: true)]
37 {
38  protected array ‪$cacheActions = [];
39  protected array ‪$optionValues = [];
40  private ServerRequestInterface ‪$request;
41 
42  public function ‪__construct(
43  ‪UriBuilder $uriBuilder,
44  EventDispatcherInterface $eventDispatcher,
45  private readonly ‪BackendViewFactory $backendViewFactory,
46  ) {
47  $isAdmin = $this->‪getBackendUser()->isAdmin();
48  $userTsConfig = $this->‪getBackendUser()->getTSConfig();
49 
50  // Clear all page-related caches
51  if ($isAdmin || ($userTsConfig['options.']['clearCache.']['pages'] ?? false)) {
52  $this->cacheActions[] = [
53  'id' => 'pages',
54  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesTitle',
55  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushPageCachesDescription',
56  'href' => (string)$uriBuilder->‪buildUriFromRoute('tce_db', ['cacheCmd' => 'pages']),
57  'severity' => 'success',
58  'iconIdentifier' => 'actions-system-cache-clear-impact-low',
59  ];
60  $this->optionValues[] = 'pages';
61  }
62 
63  // Clearing of all caches is only shown if explicitly enabled via TSConfig
64  // or if BE-User is admin and the TSconfig explicitly disables the possibility for admins.
65  // This is useful for big production systems where admins accidentally could slow down the system.
66  if (($userTsConfig['options.']['clearCache.']['all'] ?? false)
67  || ($isAdmin && (bool)($userTsConfig['options.']['clearCache.']['all'] ?? true))
68  ) {
69  $this->cacheActions[] = [
70  'id' => 'all',
71  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesTitle2',
72  'description' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:flushAllCachesDescription2',
73  'href' => (string)$uriBuilder->‪buildUriFromRoute('tce_db', ['cacheCmd' => 'all']),
74  'severity' => 'danger',
75  'iconIdentifier' => 'actions-system-cache-clear-impact-high',
76  ];
77  $this->optionValues[] = 'all';
78  }
79 
80  $event = new ‪ModifyClearCacheActionsEvent($this->cacheActions, $this->optionValues);
81  $event = $eventDispatcher->dispatch($event);
82  $this->cacheActions = $event->getCacheActions();
83  $this->optionValues = $event->getCacheActionIdentifiers();
84  }
85 
86  public function ‪setRequest(ServerRequestInterface ‪$request): void
87  {
88  $this->request = ‪$request;
89  }
90 
94  public function ‪checkAccess(): bool
95  {
96  $backendUser = $this->‪getBackendUser();
97  if ($backendUser->isAdmin()) {
98  return true;
99  }
100  foreach ($this->optionValues as $value) {
101  if ($backendUser->getTSConfig()['options.']['clearCache.'][$value] ?? false) {
102  return true;
103  }
104  }
105  return false;
106  }
107 
111  public function ‪getItem(): string
112  {
113  $view = $this->backendViewFactory->create($this->request);
114  if ($this->‪hasDropDown()) {
115  return $view->render('ToolbarItems/ClearCacheToolbarItem');
116  }
117  $cacheAction = end($this->cacheActions);
118  $view->assignMultiple([
119  'link' => $cacheAction['href'],
120  'title' => $cacheAction['title'],
121  'iconIdentifier' => $cacheAction['iconIdentifier'],
122  ]);
123  return $view->render('ToolbarItems/ClearCacheToolbarItemSingle');
124  }
125 
129  public function ‪getDropDown(): string
130  {
131  $view = $this->backendViewFactory->create($this->request);
132  $view->assign('cacheActions', $this->cacheActions);
133  return $view->render('ToolbarItems/ClearCacheToolbarItemDropDown');
134  }
135 
139  public function ‪getAdditionalAttributes(): array
140  {
141  return [];
142  }
143 
147  public function ‪hasDropDown(): bool
148  {
149  return count($this->cacheActions) > 1;
150  }
151 
155  public function ‪getIndex(): int
156  {
157  return 25;
158  }
159 
161  {
162  return ‪$GLOBALS['BE_USER'];
163  }
164 }
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getAdditionalAttributes
‪getAdditionalAttributes()
Definition: ClearCacheToolbarItem.php:139
‪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:39
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getDropDown
‪getDropDown()
Definition: ClearCacheToolbarItem.php:129
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\hasDropDown
‪hasDropDown()
Definition: ClearCacheToolbarItem.php:147
‪TYPO3\CMS\Backend\Backend\Event\ModifyClearCacheActionsEvent
Definition: ModifyClearCacheActionsEvent.php:24
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getItem
‪getItem()
Definition: ClearCacheToolbarItem.php:111
‪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:40
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\__construct
‪__construct(UriBuilder $uriBuilder, EventDispatcherInterface $eventDispatcher, private readonly BackendViewFactory $backendViewFactory,)
Definition: ClearCacheToolbarItem.php:42
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\checkAccess
‪checkAccess()
Definition: ClearCacheToolbarItem.php:94
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\getIndex
‪getIndex()
Definition: ClearCacheToolbarItem.php:155
‪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:160
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\$cacheActions
‪array $cacheActions
Definition: ClearCacheToolbarItem.php:38
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: ClearCacheToolbarItem.php:86
‪TYPO3\CMS\Backend\Backend\ToolbarItems\ClearCacheToolbarItem
Definition: ClearCacheToolbarItem.php:37