‪TYPO3CMS  ‪main
MainController.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\Http\Message\ServerRequestInterface;
30 use TYPO3\CMS\Adminpanel\Utility\ResourceUtility;
38 
49 {
51  protected array ‪$modules = [];
53 
54  public function ‪__construct(
55  private readonly ‪ModuleLoader $moduleLoader,
56  private readonly ‪UriBuilder $uriBuilder,
57  private readonly ‪RequestId $requestId,
58  ) {
59  $this->adminPanelModuleConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules'] ?? [];
60  }
61 
65  public function ‪initialize(ServerRequestInterface $request): ServerRequestInterface
66  {
67  $this->modules = $this->moduleLoader->validateSortAndInitializeModules(
68  $this->adminPanelModuleConfiguration
69  );
71  $request = $this->‪initializeModules($request, $this->modules);
72  }
73  return $request;
74  }
75 
81  public function ‪render(ServerRequestInterface $request): string
82  {
83  $resources = ResourceUtility::getResources(['nonce' => $this->requestId->nonce]);
84 
85  $backupRequest = null;
86  $frontendTypoScript = $request->getAttribute('frontend.typoscript');
87  if (!$frontendTypoScript->hasSetup()) {
88  // @todo: This is a hack: The admin panel is the only extension that starts
89  // a Fluid view in 'fully cached' scenarios. f:translate() now triggers
90  // the extbase configuration manager in FE, which fetches TS setup from
91  // the Request attribute, which is *usally* always available, *except*
92  // in fully cached scenarios.
93  // See https://review.typo3.org/c/Packages/TYPO3.CMS/+/80732
94  // We still want extbase to crash if it tries to fetch TS setup when it
95  // is not set, so we work around this in the admin panel here.
96  // This should later be resolved by avoiding the dependency to extbase
97  // LocalizationUtility again, when core localization services can do
98  // similar things in FE as well.
99  $frontendTypoScript = clone $frontendTypoScript;
100  $frontendTypoScript->setSetupArray([]);
101  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
102  $backupRequest = ‪$GLOBALS['TYPO3_REQUEST'];
103  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
104  }
105 
106  $view = GeneralUtility::makeInstance(StandaloneView::class);
107  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Main.html';
108  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
109  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
110  $view->setLayoutRootPaths(['EXT:adminpanel/Resources/Private/Layouts']);
111 
112  $view->assignMultiple([
113  'toggleActiveUrl' => $this->‪generateBackendUrl('ajax_adminPanel_toggle'),
114  'resources' => $resources,
115  'adminPanelActive' => ‪StateUtility::isOpen(),
116  'languageKey' => $this->‪getBackendUser()->user['lang'] ?? null,
117  ]);
118  if (‪StateUtility::isOpen()) {
119  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('adminpanel_requestcache');
120  $requestId = $request->getAttribute('adminPanelRequestId');
121  $data = $cache->get($requestId);
122  $moduleResources = ResourceUtility::getAdditionalResourcesForModules($this->modules, ['nonce' => $this->requestId->nonce]);
123  $settingsModules = array_filter($this->modules, static function (‪ModuleInterface $module): bool {
124  return $module instanceof ‪PageSettingsProviderInterface;
125  });
126  $parentModules = array_filter(
127  $this->modules,
128  static function (‪ModuleInterface $module): bool {
129  return $module instanceof ‪SubmoduleProviderInterface && $module instanceof ‪ShortInfoProviderInterface;
130  }
131  );
132  foreach ($parentModules as $parentModule) {
133  $parentModule->setModuleData($data);
134  }
135 
136  $routeIdentifier = 'web_layout';
137  $arguments = [
138  'id' => $request->getAttribute('frontend.page.information')->getId(),
139  ];
140  $backendUrl = (string)$this->uriBuilder->buildUriFromRoute(
141  $routeIdentifier,
142  $arguments,
144  );
145 
146  $view->assignMultiple([
147  'modules' => $this->modules,
148  'settingsModules' => $settingsModules,
149  'parentModules' => $parentModules,
150  'saveUrl' => $this->‪generateBackendUrl('ajax_adminPanel_saveForm'),
151  'moduleResources' => $moduleResources,
152  'requestId' => $requestId,
153  'data' => $data ?? [],
154  'backendUrl' => $backendUrl,
155  ]);
156  }
157  $result = $view->render();
158  if ($backupRequest) {
159  ‪$GLOBALS['TYPO3_REQUEST'] = $backupRequest;
160  }
161  return $result;
162  }
163 
169  public function ‪storeData(ServerRequestInterface $request): void
170  {
171  if (‪StateUtility::isOpen()) {
172  $data = $this->‪storeDataPerModule(
173  $request,
174  $this->modules,
175  GeneralUtility::makeInstance(ModuleDataStorageCollection::class)
176  );
177  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('adminpanel_requestcache');
178  $cache->set($request->getAttribute('adminPanelRequestId'), $data);
179  $cache->collectGarbage();
180  }
181  }
182 
186  protected function ‪generateBackendUrl(string $route): string
187  {
188  return (string)$this->uriBuilder->buildUriFromRoute($route);
189  }
190 
194  protected function ‪initializeModules(ServerRequestInterface $request, array ‪$modules): ServerRequestInterface
195  {
196  foreach (‪$modules as $module) {
197  if (
198  ($module instanceof ‪RequestEnricherInterface)
199  && (
200  (($module instanceof ‪ConfigurableInterface) && $module->isEnabled())
201  || (!($module instanceof ‪ConfigurableInterface))
202  )
203  ) {
204  $request = $module->enrich($request);
205  }
206  if ($module instanceof ‪SubmoduleProviderInterface) {
207  $request = $this->‪initializeModules($request, $module->getSubModules());
208  }
209  }
210  return $request;
211  }
212 
216  protected function ‪storeDataPerModule(ServerRequestInterface $request, array ‪$modules, ‪ModuleDataStorageCollection $data): ‪ModuleDataStorageCollection
217  {
218  foreach (‪$modules as $module) {
219  if (
220  ($module instanceof ‪DataProviderInterface)
221  && (
222  (($module instanceof ‪ConfigurableInterface) && $module->isEnabled())
223  || (!($module instanceof ‪ConfigurableInterface))
224  )
225  ) {
226  $data->‪addModuleData($module, $module->getDataToStore($request));
227  }
228 
229  if ($module instanceof ‪SubmoduleProviderInterface) {
230  $this->‪storeDataPerModule($request, $module->getSubModules(), $data);
231  }
232  }
233  return $data;
234  }
235 
237  {
238  return ‪$GLOBALS['BE_USER'];
239  }
240 }
‪TYPO3\CMS\Adminpanel\Controller\MainController
Definition: MainController.php:49
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleDataStorageCollection\addModuleData
‪addModuleData(DataProviderInterface $module, ModuleData $moduleData)
Definition: ModuleDataStorageCollection.php:25
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleDataStorageCollection
Definition: ModuleDataStorageCollection.php:24
‪TYPO3\CMS\Adminpanel\Controller\MainController\render
‪render(ServerRequestInterface $request)
Definition: MainController.php:81
‪TYPO3\CMS\Adminpanel\Controller
Definition: AjaxController.php:18
‪TYPO3\CMS\Adminpanel\Service\ModuleLoader
Definition: ModuleLoader.php:32
‪TYPO3\CMS\Adminpanel\Controller\MainController\generateBackendUrl
‪generateBackendUrl(string $route)
Definition: MainController.php:186
‪TYPO3\CMS\Adminpanel\ModuleApi\ConfigurableInterface
Definition: ConfigurableInterface.php:29
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\storeDataPerModule
‪storeDataPerModule(ServerRequestInterface $request, array $modules, ModuleDataStorageCollection $data)
Definition: MainController.php:216
‪TYPO3\CMS\Adminpanel\Utility\StateUtility
Definition: StateUtility.php:28
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isActivatedForUser
‪static isActivatedForUser()
Definition: StateUtility.php:32
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:58
‪TYPO3\CMS\Adminpanel\ModuleApi\ShortInfoProviderInterface
Definition: ShortInfoProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\__construct
‪__construct(private readonly ModuleLoader $moduleLoader, private readonly UriBuilder $uriBuilder, private readonly RequestId $requestId,)
Definition: MainController.php:54
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isOpen
‪static isOpen()
Definition: StateUtility.php:49
‪TYPO3\CMS\Adminpanel\Controller\MainController\initializeModules
‪initializeModules(ServerRequestInterface $request, array $modules)
Definition: MainController.php:194
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Core\RequestId
Definition: RequestId.php:26
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface
Definition: ModuleInterface.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Adminpanel\Controller\MainController\$adminPanelModuleConfiguration
‪array $adminPanelModuleConfiguration
Definition: MainController.php:52
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:30
‪TYPO3\CMS\Adminpanel\ModuleApi\PageSettingsProviderInterface
Definition: PageSettingsProviderInterface.php:32
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\Controller\MainController\getBackendUser
‪getBackendUser()
Definition: MainController.php:236
‪TYPO3\CMS\Adminpanel\Controller\MainController\$modules
‪array $modules
Definition: MainController.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface
Definition: SubmoduleProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\storeData
‪storeData(ServerRequestInterface $request)
Definition: MainController.php:169
‪TYPO3\CMS\Adminpanel\Controller\MainController\initialize
‪initialize(ServerRequestInterface $request)
Definition: MainController.php:65
‪TYPO3\CMS\Adminpanel\ModuleApi\RequestEnricherInterface
Definition: RequestEnricherInterface.php:37