‪TYPO3CMS  ‪main
ExtensionUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
27 {
28  public const ‪PLUGIN_TYPE_PLUGIN = 'list_type';
29  public const ‪PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
30 
48  public static function ‪configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)
49  {
50  self::checkPluginNameFormat($pluginName);
51  self::checkExtensionNameFormat($extensionName);
52 
53  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
54 
55  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
56 
57  $controllerActions = self::actionCommaListToArray($controllerActions);
58  $nonCacheableControllerActions = self::actionCommaListToArray($nonCacheableControllerActions);
59  ‪self::registerControllerActions($extensionName, $pluginName, $controllerActions, $nonCacheableControllerActions);
60 
61  switch ($pluginType) {
63  $pluginContent = trim('
64 tt_content.list.20.' . $pluginSignature . ' = EXTBASEPLUGIN
65 tt_content.list.20.' . $pluginSignature . ' {
66  extensionName = ' . $extensionName . '
67  pluginName = ' . $pluginName . '
68 }');
69  break;
71  $pluginContent = trim('
72 tt_content.' . $pluginSignature . ' =< lib.contentElement
73 tt_content.' . $pluginSignature . ' {
74  templateName = Generic
75  20 = EXTBASEPLUGIN
76  20 {
77  extensionName = ' . $extensionName . '
78  pluginName = ' . $pluginName . '
79  }
80 }');
81  break;
82  default:
83  throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not supported', 1289858856);
84  }
85  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
86  ‪ExtensionManagementUtility::addTypoScript($extensionName, 'setup', '
87 # Setting ' . $extensionName . ' plugin TypoScript
88 ' . $pluginContent, 'defaultContentRendering');
89  }
90 
96  public static function ‪registerControllerActions(string $extensionName, string $pluginName, array $controllerActions, array $nonCacheableControllerActions): void
97  {
98  if (!is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] ?? false)) {
99  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = [];
100  }
101  foreach ($controllerActions as $controllerClassName => $actionsList) {
102  $controllerAlias = ‪self::resolveControllerAliasFromControllerClassName($controllerClassName);
103 
104  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerClassName] = [
105  'className' => $controllerClassName,
106  'alias' => $controllerAlias,
107  'actions' => $actionsList,
108  ];
109 
110  if (!empty($nonCacheableControllerActions[$controllerClassName])) {
111  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerClassName]['nonCacheableActions']
112  = $nonCacheableControllerActions[$controllerClassName];
113  }
114  }
115  }
116 
129  public static function ‪registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon = null, $group = 'default', string $pluginDescription = ''): string
130  {
131  self::checkPluginNameFormat($pluginName);
132  self::checkExtensionNameFormat($extensionName);
133 
134  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
135  $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
136 
137  // At this point $extensionName is normalized, no matter which format the method was fed with.
138  // Calculate the original extensionKey from this again.
139  $extensionKey = ‪GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
140 
141  // pluginType is usually defined by configurePlugin() in the global array. Use this or fall back to default "list_type".
142  $pluginType = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] ?? 'list_type';
143 
144  $selectItem = new ‪SelectItem(
145  'select',
146  // set pluginName as default pluginTitle
147  $pluginTitle ?: $pluginName,
148  $pluginSignature,
149  $pluginIcon,
150  $group,
151  $pluginDescription,
152  );
154  $selectItem,
155  $pluginType,
156  $extensionKey
157  );
158  return $pluginSignature;
159  }
160 
164  public static function ‪resolveControllerAliasFromControllerClassName(string $controllerClassName): string
165  {
166  // This method has been adjusted for TYPO3 10.3 to mitigate the issue that controller aliases
167  // could not longer be calculated from controller classes when calling
168  // \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin().
169  //
170  // The idea for version 11 is to let the user choose a controller alias and to check for its
171  // uniqueness per plugin. That way, the core does no longer rely on the namespace of
172  // controller classes to be in a specific format.
173  //
174  // todo: Change the way plugins are registered and enforce a controller alias to be set by
175  // the user to also free the core from guessing a simple alias by looking at the
176  // class name. This makes it possible to choose controller class names without a
177  // controller suffix.
178 
179  $strLen = strlen('Controller');
180 
181  if (!str_ends_with($controllerClassName, 'Controller')) {
182  return '';
183  }
184 
185  $controllerClassNameWithoutControllerSuffix = substr($controllerClassName, 0, -$strLen);
186 
187  if (strrpos($controllerClassNameWithoutControllerSuffix, 'Controller\\') === false) {
188  $positionOfLastSlash = (int)strrpos($controllerClassNameWithoutControllerSuffix, '\\');
189  $positionOfLastSlash += $positionOfLastSlash === 0 ? 0 : 1;
190 
191  return substr($controllerClassNameWithoutControllerSuffix, $positionOfLastSlash);
192  }
193 
194  $positionOfControllerNamespacePart = (int)strrpos(
195  $controllerClassNameWithoutControllerSuffix,
196  'Controller\\'
197  );
198 
199  return substr(
200  $controllerClassNameWithoutControllerSuffix,
201  $positionOfControllerNamespacePart + $strLen + 1
202  );
203  }
204 
209  protected static function actionCommaListToArray(array $controllerActions): array
210  {
211  foreach ($controllerActions as $controllerClassName => $actionsList) {
212  if (is_array($actionsList)) {
213  continue;
214  }
215  $actionsListArray = ‪GeneralUtility::trimExplode(',', (string)$actionsList);
216  $controllerActions[$controllerClassName] = $actionsListArray;
217  }
218  return $controllerActions;
219  }
220 
227  protected static function checkExtensionNameFormat($extensionName)
228  {
229  if (empty($extensionName)) {
230  throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
231  }
232  }
233 
240  protected static function checkPluginNameFormat($pluginName)
241  {
242  if (empty($pluginName)) {
243  throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
244  }
245  }
246 }
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility
Definition: ExtensionUtility.php:27
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\PLUGIN_TYPE_CONTENT_ELEMENT
‪const PLUGIN_TYPE_CONTENT_ELEMENT
Definition: ExtensionUtility.php:29
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\PLUGIN_TYPE_PLUGIN
‪const PLUGIN_TYPE_PLUGIN
Definition: ExtensionUtility.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility\camelCaseToLowerCaseUnderscored
‪static string camelCaseToLowerCaseUnderscored($string)
Definition: GeneralUtility.php:678
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\configurePlugin
‪static configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions=[], $pluginType=self::PLUGIN_TYPE_PLUGIN)
Definition: ExtensionUtility.php:48
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Core\Schema\Struct\SelectItem
Definition: SelectItem.php:21
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerPlugin
‪static registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon=null, $group='default', string $pluginDescription='')
Definition: ExtensionUtility.php:129
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addPlugin
‪static addPlugin(array|SelectItem $itemArray, string $type='list_type', ?string $extensionKey=null)
Definition: ExtensionManagementUtility.php:814
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Utility
Definition: DebuggerUtility.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\resolveControllerAliasFromControllerClassName
‪static resolveControllerAliasFromControllerClassName(string $controllerClassName)
Definition: ExtensionUtility.php:164
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerControllerActions
‪static registerControllerActions(string $extensionName, string $pluginName, array $controllerActions, array $nonCacheableControllerActions)
Definition: ExtensionUtility.php:96
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTypoScript
‪static addTypoScript(string $key, string $type, string $content, int|string $afterStaticUid=0)
Definition: ExtensionManagementUtility.php:1071