TYPO3 CMS  TYPO3_8-7
ExtensionUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 {
23  const PLUGIN_TYPE_PLUGIN = 'list_type';
24  const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
25 
44  public static function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)
45  {
46  self::checkPluginNameFormat($pluginName);
47  self::checkExtensionNameFormat($extensionName);
48 
49  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
50  $vendorName = null;
51  $delimiterPosition = strrpos($extensionName, '.');
52  if ($delimiterPosition !== false) {
53  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
54  $extensionName = substr($extensionName, $delimiterPosition + 1);
55 
56  if (!empty($vendorName)) {
57  self::checkVendorNameFormat($vendorName, $extensionName);
58  }
59  }
60  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
61 
62  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
63  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] ?? false)) {
64  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = [];
65  }
66  foreach ($controllerActions as $controllerName => $actionsList) {
67  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName] = ['actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList)];
68  if (!empty($nonCacheableControllerActions[$controllerName])) {
69  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $nonCacheableControllerActions[$controllerName]);
70  }
71  }
72 
73  switch ($pluginType) {
74  case self::PLUGIN_TYPE_PLUGIN:
75  $pluginContent = trim('
76 tt_content.list.20.' . $pluginSignature . ' = USER
77 tt_content.list.20.' . $pluginSignature . ' {
78  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
79  extensionName = ' . $extensionName . '
80  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t" . 'vendorName = ' . $vendorName) : '') . '
81 }');
82  break;
83  case self::PLUGIN_TYPE_CONTENT_ELEMENT:
84  $pluginContent = trim('
85 tt_content.' . $pluginSignature . ' =< lib.contentElement
86 tt_content.' . $pluginSignature . ' {
87  templateName = Generic
88  20 = USER
89  20 {
90  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
91  extensionName = ' . $extensionName . '
92  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t\t" . 'vendorName = ' . $vendorName) : '') . '
93  }
94 }');
95  break;
96  default:
97  throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not suported', 1289858856);
98  }
99  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
101 # Setting ' . $extensionName . ' plugin TypoScript
102 ' . $pluginContent, 'defaultContentRendering');
103  }
104 
115  public static function registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon = null)
116  {
117  self::checkPluginNameFormat($pluginName);
118  self::checkExtensionNameFormat($extensionName);
119 
120  $delimiterPosition = strrpos($extensionName, '.');
121  if ($delimiterPosition !== false) {
122  $extensionName = substr($extensionName, $delimiterPosition + 1);
123  }
124  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
125  $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
126 
127  // At this point $extensionName is normalized, no matter which format the method was feeded with.
128  // Calculate the original extensionKey from this again.
130 
131  // pluginType is usually defined by configurePlugin() in the global array. Use this or fall back to default "list_type".
132  $pluginType = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'])
133  ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType']
134  : 'list_type';
135 
137  [$pluginTitle, $pluginSignature, $pluginIcon],
138  $pluginType,
139  $extensionKey
140  );
141  }
142 
155  public static function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = [], array $moduleConfiguration = [])
156  {
157  self::checkExtensionNameFormat($extensionName);
158 
159  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
160  $vendorName = null;
161  if (false !== $delimiterPosition = strrpos($extensionName, '.')) {
162  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
163  $extensionName = substr($extensionName, $delimiterPosition + 1);
164 
165  if (!empty($vendorName)) {
166  self::checkVendorNameFormat($vendorName, $extensionName);
167  }
168  }
169 
170  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
171  $defaultModuleConfiguration = [
172  'access' => 'admin',
173  'icon' => 'EXT:extbase/Resources/Public/Icons/Extension.png',
174  'labels' => ''
175  ];
176  if ($mainModuleName !== '' && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
177  $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
178  } else {
179  $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
180  }
181  // add mandatory parameter to use new pagetree
182  if ($mainModuleName === 'web') {
183  $defaultModuleConfiguration['navigationComponentId'] = 'typo3-pagetree';
184  }
185  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
186  $moduleConfiguration = $defaultModuleConfiguration;
187  $moduleSignature = $mainModuleName;
188  if ($subModuleName !== '') {
189  $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
190  $moduleSignature .= '_' . $subModuleName;
191  }
192  $moduleConfiguration['name'] = $moduleSignature;
193  if (null !== $vendorName) {
194  $moduleConfiguration['vendorName'] = $vendorName;
195  }
196  $moduleConfiguration['extensionName'] = $extensionName;
197  $moduleConfiguration['configureModuleFunction'] = [\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class, 'configureModule'];
198  $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature] = $moduleConfiguration;
199  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature])) {
200  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = [];
201  }
202  foreach ($controllerActions as $controllerName => $actions) {
203  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = [
204  'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
205  ];
206  }
207  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position);
208  }
209 
216  public static function registerTypeConverter($typeConverterClassName)
217  {
218  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
219  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
220  }
221  if (!in_array($typeConverterClassName, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
222  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
223  }
224  }
225 
233  protected static function checkVendorNameFormat($vendorName, $extensionName)
234  {
235  if (preg_match('/^[A-Z]/', $vendorName) !== 1) {
236  \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('The vendor name from tx_' . $extensionName . ' must begin with a capital letter.');
237  }
238  }
239 
246  protected static function checkExtensionNameFormat($extensionName)
247  {
248  if (empty($extensionName)) {
249  throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
250  }
251  }
252 
259  protected static function checkPluginNameFormat($pluginName)
260  {
261  if (empty($pluginName)) {
262  throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
263  }
264  }
265 }
static registerModule($extensionName, $mainModuleName='', $subModuleName='', $position='', array $controllerActions=[], array $moduleConfiguration=[])
static addTypoScript($key, $type, $content, $afterStaticUid=0)
static checkVendorNameFormat($vendorName, $extensionName)
static configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions=[], $pluginType=self::PLUGIN_TYPE_PLUGIN)
static registerTypeConverter($typeConverterClassName)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon=null)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static addPlugin($itemArray, $type='list_type', $extensionKey=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static addModule($main, $sub='', $position='', $path=null, $moduleConfiguration=[])