‪TYPO3CMS  10.4
ExtensionService.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 
27 
33 {
37  protected ‪$configurationManager;
38 
43  protected ‪$targetPidPluginCache = [];
44 
49  {
50  $this->configurationManager = ‪$configurationManager;
51  }
52 
62  public function ‪getPluginNamespace(?string $extensionName, ?string $pluginName): string
63  {
64  // todo: with $extensionName and $pluginName being null, tx__ will be returned here which is questionable.
65  // todo: find out, if and why this case could happen and maybe avoid this methods being called with null
66  // todo: arguments afterwards.
67  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
68  $defaultPluginNamespace = 'tx_' . $pluginSignature;
69  $frameworkConfiguration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
70  if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
71  return $defaultPluginNamespace;
72  }
73  return $frameworkConfiguration['view']['pluginNamespace'];
74  }
75 
89  public function ‪getPluginNameByAction(string $extensionName, string $controllerName, ?string $actionName): ?string
90  {
91  // check, whether the current plugin is configured to handle the action
92  if (($pluginName = $this->‪getPluginNameFromFrameworkConfiguration($extensionName, $controllerName, $actionName)) !== null) {
93  return $pluginName;
94  }
95 
96  $plugins = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] ?? false;
97  if (!$plugins) {
98  return null;
99  }
100  $pluginNames = [];
101  foreach ($plugins as $pluginName => $pluginConfiguration) {
102  $controllers = $pluginConfiguration['controllers'] ?? [];
103  $controllerAliases = array_column($controllers, 'actions', 'alias');
104 
105  foreach ($controllerAliases as $pluginControllerName => $pluginControllerActions) {
106  if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
107  continue;
108  }
109  if (in_array($actionName, $pluginControllerActions, true)) {
110  $pluginNames[] = $pluginName;
111  }
112  }
113  }
114  if (count($pluginNames) > 1) {
115  throw new ‪Exception('There is more than one plugin that can handle this request (Extension: "' . $extensionName . '", Controller: "' . $controllerName . '", action: "' . $actionName . '"). Please specify "pluginName" argument', 1280825466);
116  }
117  return !empty($pluginNames) ? $pluginNames[0] : null;
118  }
119 
120  private function ‪getPluginNameFromFrameworkConfiguration(string $extensionName, string $controllerAlias, ?string $actionName): ?string
121  {
122  if ($actionName === null) {
123  return null;
124  }
125 
126  $frameworkConfiguration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
127 
128  if (!is_string($pluginName = ($frameworkConfiguration['pluginName'] ?? null))) {
129  return null;
130  }
131 
132  $configuredExtensionName = $frameworkConfiguration['extensionName'] ?? '';
133  $configuredExtensionName = is_string($configuredExtensionName) ? $configuredExtensionName : '';
134 
135  if ($configuredExtensionName === '' || $configuredExtensionName !== $extensionName) {
136  return null;
137  }
138 
139  $configuredControllers = $frameworkConfiguration['controllerConfiguration'] ?? [];
140  $configuredControllers = is_array($configuredControllers) ? $configuredControllers : [];
141 
142  $configuredActionsByControllerAliases = array_column($configuredControllers, 'actions', 'alias');
143 
144  $actions = $configuredActionsByControllerAliases[$controllerAlias] ?? [];
145  $actions = is_array($actions) ? $actions : [];
146 
147  return in_array($actionName, $actions, true) ? $pluginName : null;
148  }
149 
162  public function ‪getTargetPidByPlugin(string $extensionName, string $pluginName): ?int
163  {
164  $frameworkConfiguration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
165  if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
166  return null;
167  }
168  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
169  if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
170  if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
171  $languageId = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id', 0);
172  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
173  ->getQueryBuilderForTable('tt_content');
174  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
175 
176  $pages = $queryBuilder
177  ->select('pid')
178  ->from('tt_content')
179  ->where(
180  $queryBuilder->expr()->eq(
181  'list_type',
182  $queryBuilder->createNamedParameter($pluginSignature, \PDO::PARAM_STR)
183  ),
184  $queryBuilder->expr()->eq(
185  'CType',
186  $queryBuilder->createNamedParameter('list', \PDO::PARAM_STR)
187  ),
188  $queryBuilder->expr()->eq(
189  'sys_language_uid',
190  $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
191  )
192  )
193  ->setMaxResults(2)
194  ->execute()
195  ->fetchAll();
196 
197  if (count($pages) > 1) {
198  throw new Exception('There is more than one "' . $pluginSignature . '" plugin in the current page tree. Please remove one plugin or set the TypoScript configuration "plugin.tx_' . $pluginSignature . '.view.defaultPid" to a fixed page id', 1280773643);
199  }
200  $this->targetPidPluginCache[$pluginSignature] = !empty($pages) ? (int)$pages[0]['pid'] : null;
201  }
202  return $this->targetPidPluginCache[$pluginSignature];
203  }
204  return (int)$frameworkConfiguration['view']['defaultPid'];
205  }
206 
214  public function ‪getDefaultControllerNameByPlugin(string $extensionName, string $pluginName): ?string
215  {
216  $controllers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'] ?? [];
217  $controllerAliases = array_column($controllers, 'alias');
218  $defaultControllerName = (string)($controllerAliases[0] ?? '');
219  return $defaultControllerName !== '' ? $defaultControllerName : null;
220  }
221 
230  public function ‪getDefaultActionNameByPluginAndController(string $extensionName, string $pluginName, string $controllerName): ?string
231  {
232  $controllers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'] ?? [];
233  $controllerActionsByAlias = array_column($controllers, 'actions', 'alias');
234  $actions = $controllerActionsByAlias[$controllerName] ?? [];
235  $defaultActionName = (string)($actions[0] ?? '');
236  return $defaultActionName !== '' ? $defaultActionName : null;
237  }
238 
246  public function ‪getTargetPageTypeByFormat(?string $extensionName, string $format): int
247  {
248  // Default behaviour
249  $settings = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
250  $formatToPageTypeMapping = $settings['view']['formatToPageTypeMapping'] ?? [];
251  $formatToPageTypeMapping = is_array($formatToPageTypeMapping) ? $formatToPageTypeMapping : [];
252  return (int)($formatToPageTypeMapping[$format] ?? 0);
253  }
254 }
‪TYPO3\CMS\Extbase\Service\ExtensionService\getDefaultControllerNameByPlugin
‪string null getDefaultControllerNameByPlugin(string $extensionName, string $pluginName)
Definition: ExtensionService.php:212
‪TYPO3\CMS\Extbase\Service\ExtensionService\getTargetPidByPlugin
‪int null getTargetPidByPlugin(string $extensionName, string $pluginName)
Definition: ExtensionService.php:160
‪TYPO3\CMS\Extbase\Service\ExtensionService\getPluginNameByAction
‪string null getPluginNameByAction(string $extensionName, string $controllerName, ?string $actionName)
Definition: ExtensionService.php:87
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Service
Definition: CacheService.php:18
‪TYPO3\CMS\Extbase\Service\ExtensionService\injectConfigurationManager
‪injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
Definition: ExtensionService.php:46
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Extbase\Exception
Definition: Exception.php:26
‪TYPO3\CMS\Extbase\Service\ExtensionService\getPluginNameFromFrameworkConfiguration
‪getPluginNameFromFrameworkConfiguration(string $extensionName, string $controllerAlias, ?string $actionName)
Definition: ExtensionService.php:118
‪TYPO3\CMS\Extbase\Service\ExtensionService\$configurationManager
‪ConfigurationManagerInterface $configurationManager
Definition: ExtensionService.php:36
‪TYPO3\CMS\Extbase\Service\ExtensionService\$targetPidPluginCache
‪array $targetPidPluginCache
Definition: ExtensionService.php:41
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:33
‪TYPO3\CMS\Extbase\Service\ExtensionService\getTargetPageTypeByFormat
‪int getTargetPageTypeByFormat(?string $extensionName, string $format)
Definition: ExtensionService.php:244
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Service\ExtensionService\getPluginNamespace
‪string getPluginNamespace(?string $extensionName, ?string $pluginName)
Definition: ExtensionService.php:60
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Extbase\Service\ExtensionService\getDefaultActionNameByPluginAndController
‪string null getDefaultActionNameByPluginAndController(string $extensionName, string $pluginName, string $controllerName)
Definition: ExtensionService.php:228