TYPO3 CMS  TYPO3_7-6
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 
45  public static function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)
46  {
47  self::checkPluginNameFormat($pluginName);
48  self::checkExtensionNameFormat($extensionName);
49 
50  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
51  $vendorName = null;
52  $delimiterPosition = strrpos($extensionName, '.');
53  if ($delimiterPosition !== false) {
54  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
55  $extensionName = substr($extensionName, $delimiterPosition + 1);
56 
57  if (!empty($vendorName)) {
58  self::checkVendorNameFormat($vendorName, $extensionName);
59  }
60  }
61  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
62 
63  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
64  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName])) {
65  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = [];
66  }
67  foreach ($controllerActions as $controllerName => $actionsList) {
68  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName] = ['actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList)];
69  if (!empty($nonCacheableControllerActions[$controllerName])) {
70  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $nonCacheableControllerActions[$controllerName]);
71  }
72  }
73 
74  switch ($pluginType) {
75  case self::PLUGIN_TYPE_PLUGIN:
76  $pluginContent = trim('
77 tt_content.list.20.' . $pluginSignature . ' = USER
78 tt_content.list.20.' . $pluginSignature . ' {
79  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
80  extensionName = ' . $extensionName . '
81  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t" . 'vendorName = ' . $vendorName) : '') . '
82 }');
83  break;
84  case self::PLUGIN_TYPE_CONTENT_ELEMENT:
85  $pluginContent = trim('
86 tt_content.' . $pluginSignature . ' = COA
87 tt_content.' . $pluginSignature . ' {
88  10 = < lib.stdheader
89  20 = USER
90  20 {
91  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
92  extensionName = ' . $extensionName . '
93  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t\t" . 'vendorName = ' . $vendorName) : '') . '
94  }
95 }');
96  break;
97  default:
98  throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not suported', 1289858856);
99  }
100  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
102 # Setting ' . $extensionName . ' plugin TypoScript
103 ' . $pluginContent, 'defaultContentRendering');
104  }
105 
117  public static function registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIconPathAndFilename = null)
118  {
119  self::checkPluginNameFormat($pluginName);
120  self::checkExtensionNameFormat($extensionName);
121 
122  $delimiterPosition = strrpos($extensionName, '.');
123  if ($delimiterPosition !== false) {
124  $extensionName = substr($extensionName, $delimiterPosition + 1);
125  }
126  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
127  $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
128 
129  // At this point $extensionName is normalized, no matter which format the method was feeded with.
130  // Calculate the original extensionKey from this again.
132 
133  // pluginType is usually defined by configurePlugin() in the global array. Use this or fall back to default "list_type".
134  $pluginType = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'])
135  ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType']
136  : 'list_type';
137 
139  [$pluginTitle, $pluginSignature, $pluginIconPathAndFilename],
140  $pluginType,
141  $extensionKey
142  );
143  }
144 
154  public static function configureModule($moduleSignature, $modulePath)
155  {
157  return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::configureModule($moduleSignature, $modulePath);
158  }
159 
173  public static function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = [], array $moduleConfiguration = [])
174  {
175  self::checkExtensionNameFormat($extensionName);
176 
177  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
178  $vendorName = null;
179  if (false !== $delimiterPosition = strrpos($extensionName, '.')) {
180  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
181  $extensionName = substr($extensionName, $delimiterPosition + 1);
182 
183  if (!empty($vendorName)) {
184  self::checkVendorNameFormat($vendorName, $extensionName);
185  }
186  }
188  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
189  $defaultModuleConfiguration = [
190  'access' => 'admin',
191  'icon' => 'EXT:extbase/ext_icon.png',
192  'labels' => '',
193  'extRelPath' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath($extensionKey) . 'Classes/'
194  ];
195  if ($mainModuleName !== '' && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
196  $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
197  } else {
198  $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
199  }
200  // add mandatory parameter to use new pagetree
201  if ($mainModuleName === 'web') {
202  $defaultModuleConfiguration['navigationComponentId'] = 'typo3-pagetree';
203  }
204  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
205  $moduleConfiguration = $defaultModuleConfiguration;
206  $moduleSignature = $mainModuleName;
207  if ($subModuleName !== '') {
208  $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
209  $moduleSignature .= '_' . $subModuleName;
210  }
211  $moduleConfiguration['name'] = $moduleSignature;
212  if (null !== $vendorName) {
213  $moduleConfiguration['vendorName'] = $vendorName;
214  }
215  $moduleConfiguration['extensionName'] = $extensionName;
216  $moduleConfiguration['configureModuleFunction'] = [\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class, 'configureModule'];
217  $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature] = $moduleConfiguration;
218  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature])) {
219  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = [];
220  }
221  foreach ($controllerActions as $controllerName => $actions) {
222  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = [
223  'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
224  ];
225  }
226  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position);
227  }
228 
236  public static function registerTypeConverter($typeConverterClassName)
237  {
238  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
239  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
240  }
241  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
242  }
243 
252  protected static function checkVendorNameFormat($vendorName, $extensionName)
253  {
254  if (preg_match('/^[A-Z]/', $vendorName) !== 1) {
255  \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('The vendor name from tx_' . $extensionName . ' must begin with a capital letter.');
256  }
257  }
258 
266  protected static function checkExtensionNameFormat($extensionName)
267  {
268  if (empty($extensionName)) {
269  throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
270  }
271  }
272 
280  protected static function checkPluginNameFormat($pluginName)
281  {
282  if (empty($pluginName)) {
283  throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
284  }
285  }
286 }
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 configureModule($moduleSignature, $modulePath)
static registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIconPathAndFilename=null)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static addModule($main, $sub='', $position='', $path='', $moduleConfiguration=[])
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']