‪TYPO3CMS  10.4
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;
40 
47 {
51  protected ‪$modules = [];
52 
56  protected ‪$moduleLoader;
57 
61  protected ‪$uriBuilder;
62 
67 
72 
78  public function ‪__construct(
82  ) {
83  $this->moduleLoader = ‪$moduleLoader ?? GeneralUtility::makeInstance(ModuleLoader::class);
84  $this->uriBuilder = ‪$uriBuilder ?? GeneralUtility::makeInstance(UriBuilder::class);
85  $this->configurationService = ‪$configurationService
86  ??
87  GeneralUtility::makeInstance(ConfigurationService::class);
88  $this->adminPanelModuleConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules'] ?? [];
89  }
90 
97  public function ‪initialize(ServerRequestInterface $request): ServerRequestInterface
98  {
99  $this->modules = $this->moduleLoader->validateSortAndInitializeModules(
100  $this->adminPanelModuleConfiguration
101  );
102 
104  $request = $this->‪initializeModules($request, $this->modules);
105  }
106  return $request;
107  }
108 
116  public function ‪render(ServerRequestInterface $request): string
117  {
118  $resources = ‪ResourceUtility::getResources();
119 
120  $view = GeneralUtility::makeInstance(StandaloneView::class);
121  $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Main.html';
122  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
123  $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
124  $view->setLayoutRootPaths(['EXT:adminpanel/Resources/Private/Layouts']);
125 
126  $view->assignMultiple(
127  [
128  'toggleActiveUrl' => $this->‪generateBackendUrl('ajax_adminPanel_toggle'),
129  'resources' => $resources,
130  'adminPanelActive' => ‪StateUtility::isOpen(),
131  'languageKey' => $this->‪getBackendUser()->user['lang'],
132  ]
133  );
134  if (‪StateUtility::isOpen()) {
135  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('adminpanel_requestcache');
136  $requestId = $request->getAttribute('adminPanelRequestId');
137  $data = $cache->get($requestId);
138  $moduleResources = ‪ResourceUtility::getAdditionalResourcesForModules($this->modules);
139  $settingsModules = array_filter($this->modules, function (ModuleInterface $module) {
140  return $module instanceof PageSettingsProviderInterface;
141  });
142  $parentModules = array_filter(
143  $this->modules,
144  function (ModuleInterface $module) {
145  return $module instanceof SubmoduleProviderInterface && $module instanceof ShortInfoProviderInterface;
146  }
147  );
148  $view->assignMultiple(
149  [
150  'modules' => $this->modules,
151  'settingsModules' => $settingsModules,
152  'parentModules' => $parentModules,
153  'saveUrl' => $this->‪generateBackendUrl('ajax_adminPanel_saveForm'),
154  'moduleResources' => $moduleResources,
155  'requestId' => $requestId,
156  'data' => $data ?? [],
157  ]
158  );
159  }
160  return $view->render();
161  }
162 
170  public function ‪storeData(ServerRequestInterface $request): void
171  {
172  if (‪StateUtility::isOpen()) {
173  $data = $this->‪storeDataPerModule(
174  $request,
175  $this->modules,
176  GeneralUtility::makeInstance(ModuleDataStorageCollection::class)
177  );
178  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('adminpanel_requestcache');
179  $cache->set($request->getAttribute('adminPanelRequestId'), $data);
180  $cache->collectGarbage();
181  }
182  }
183 
190  protected function ‪generateBackendUrl(string $route): string
191  {
192  return (string)$this->uriBuilder->buildUriFromRoute($route);
193  }
194 
200  protected function ‪initializeModules(ServerRequestInterface $request, array ‪$modules): ServerRequestInterface
201  {
202  foreach (‪$modules as $module) {
203  if (
204  ($module instanceof InitializableInterface || $module instanceof RequestEnricherInterface)
205  && (
206  (($module instanceof ConfigurableInterface) && $module->isEnabled())
207  || (!($module instanceof ConfigurableInterface))
208  )
209  ) {
210  if ($module instanceof RequestEnricherInterface) {
211  $request = $module->enrich($request);
212  } elseif ($module instanceof InitializableInterface) {
213  $module->initializeModule($request);
214  trigger_error('Method initializeModule is deprecated use RequestEnricherInterface::enrich instead.', E_USER_DEPRECATED);
215  }
216  }
217  if ($module instanceof SubmoduleProviderInterface) {
218  $request = $this->‪initializeModules($request, $module->getSubModules());
219  }
220  }
221  return $request;
222  }
223 
230  protected function ‪storeDataPerModule(ServerRequestInterface $request, array ‪$modules, ModuleDataStorageCollection $data): ModuleDataStorageCollection
231  {
232  foreach (‪$modules as $module) {
233  if (
234  ($module instanceof DataProviderInterface)
235  && (
236  (($module instanceof ConfigurableInterface) && $module->isEnabled())
237  || (!($module instanceof ConfigurableInterface))
238  )
239  ) {
240  $data->addModuleData($module, $module->getDataToStore($request));
241  }
242 
243  if ($module instanceof SubmoduleProviderInterface) {
244  $this->‪storeDataPerModule($request, $module->getSubModules(), $data);
245  }
246  }
247  return $data;
248  }
249 
250  protected function ‪getBackendUser(): BackendUserAuthentication
251  {
252  return ‪$GLOBALS['BE_USER'];
253  }
254 }
‪TYPO3\CMS\Adminpanel\Controller\MainController
Definition: MainController.php:47
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleDataStorageCollection\addModuleData
‪addModuleData(DataProviderInterface $module, ModuleData $moduleData)
Definition: ModuleDataStorageCollection.php:25
‪TYPO3\CMS\Adminpanel\Controller\MainController\$moduleLoader
‪ModuleLoader $moduleLoader
Definition: MainController.php:54
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleDataStorageCollection
Definition: ModuleDataStorageCollection.php:24
‪TYPO3\CMS\Adminpanel\Controller\MainController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: MainController.php:58
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isActivatedForUser
‪static bool isActivatedForUser()
Definition: StateUtility.php:34
‪TYPO3\CMS\Adminpanel\Utility\ResourceUtility\getResources
‪static array getResources()
Definition: ResourceUtility.php:129
‪TYPO3\CMS\Adminpanel\Controller
Definition: AjaxController.php:18
‪TYPO3\CMS\Adminpanel\Service\ModuleLoader
Definition: ModuleLoader.php:33
‪TYPO3\CMS\Adminpanel\Controller\MainController\initializeModules
‪Psr Http Message ServerRequestInterface initializeModules(ServerRequestInterface $request, array $modules)
Definition: MainController.php:195
‪TYPO3\CMS\Adminpanel\ModuleApi\InitializableInterface
Definition: InitializableInterface.php:36
‪TYPO3\CMS\Adminpanel\ModuleApi\ConfigurableInterface
Definition: ConfigurableInterface.php:29
‪TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface
Definition: DataProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\__construct
‪__construct(ModuleLoader $moduleLoader=null, UriBuilder $uriBuilder=null, ConfigurationService $configurationService=null)
Definition: MainController.php:73
‪TYPO3\CMS\Adminpanel\Controller\MainController\render
‪string render(ServerRequestInterface $request)
Definition: MainController.php:111
‪TYPO3\CMS\Adminpanel\Utility\StateUtility
Definition: StateUtility.php:28
‪TYPO3\CMS\Adminpanel\ModuleApi\ShortInfoProviderInterface
Definition: ShortInfoProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\$configurationService
‪ConfigurationService $configurationService
Definition: MainController.php:62
‪TYPO3\CMS\Adminpanel\Controller\MainController\initialize
‪ServerRequestInterface initialize(ServerRequestInterface $request)
Definition: MainController.php:92
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪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:66
‪TYPO3\CMS\Adminpanel\Controller\MainController\storeDataPerModule
‪ModuleDataStorageCollection storeDataPerModule(ServerRequestInterface $request, array $modules, ModuleDataStorageCollection $data)
Definition: MainController.php:225
‪TYPO3\CMS\Adminpanel\Controller\MainController\$modules
‪TYPO3 CMS Adminpanel ModuleApi ModuleInterface[] $modules
Definition: MainController.php:50
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Adminpanel\ModuleApi\PageSettingsProviderInterface
Definition: PageSettingsProviderInterface.php:32
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Adminpanel\Controller\MainController\generateBackendUrl
‪string generateBackendUrl(string $route)
Definition: MainController.php:185
‪TYPO3\CMS\Adminpanel\Utility\StateUtility\isOpen
‪static bool isOpen()
Definition: StateUtility.php:53
‪TYPO3\CMS\Adminpanel\Controller\MainController\getBackendUser
‪getBackendUser()
Definition: MainController.php:245
‪TYPO3\CMS\Adminpanel\Utility\ResourceUtility\getAdditionalResourcesForModules
‪static array getAdditionalResourcesForModules(array $modules)
Definition: ResourceUtility.php:34
‪TYPO3\CMS\Adminpanel\Utility\ResourceUtility
Definition: ResourceUtility.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface
Definition: SubmoduleProviderInterface.php:30
‪TYPO3\CMS\Adminpanel\Controller\MainController\storeData
‪storeData(ServerRequestInterface $request)
Definition: MainController.php:165
‪TYPO3\CMS\Adminpanel\ModuleApi\RequestEnricherInterface
Definition: RequestEnricherInterface.php:37