TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ExtensionUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Utility;
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 
158  public static function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = [], array $moduleConfiguration = [])
159  {
160  self::checkExtensionNameFormat($extensionName);
161 
162  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
163  $vendorName = null;
164  if (false !== $delimiterPosition = strrpos($extensionName, '.')) {
165  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
166  $extensionName = substr($extensionName, $delimiterPosition + 1);
167 
168  if (!empty($vendorName)) {
169  self::checkVendorNameFormat($vendorName, $extensionName);
170  }
171  }
172 
173  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
174  $defaultModuleConfiguration = [
175  'access' => 'admin',
176  'icon' => 'EXT:extbase/Resources/Public/Icons/Extension.png',
177  'labels' => ''
178  ];
179  if ($mainModuleName !== '' && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
180  $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
181  } else {
182  $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
183  }
184  // add mandatory parameter to use new pagetree
185  if ($mainModuleName === 'web') {
186  $defaultModuleConfiguration['navigationComponentId'] = 'typo3-pagetree';
187  }
188  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
189  $moduleConfiguration = $defaultModuleConfiguration;
190  $moduleSignature = $mainModuleName;
191  if ($subModuleName !== '') {
192  $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
193  $moduleSignature .= '_' . $subModuleName;
194  }
195  $moduleConfiguration['name'] = $moduleSignature;
196  if (null !== $vendorName) {
197  $moduleConfiguration['vendorName'] = $vendorName;
198  }
199  $moduleConfiguration['extensionName'] = $extensionName;
200  $moduleConfiguration['configureModuleFunction'] = [\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class, 'configureModule'];
201  $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature] = $moduleConfiguration;
202  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature])) {
203  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = [];
204  }
205  foreach ($controllerActions as $controllerName => $actions) {
206  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = [
207  'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
208  ];
209  }
210  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position);
211  }
212 
220  public static function registerTypeConverter($typeConverterClassName)
221  {
222  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
223  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
224  }
225  $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
226  }
227 
236  protected static function checkVendorNameFormat($vendorName, $extensionName)
237  {
238  if (preg_match('/^[A-Z]/', $vendorName) !== 1) {
239  \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('The vendor name from tx_' . $extensionName . ' must begin with a capital letter.');
240  }
241  }
242 
250  protected static function checkExtensionNameFormat($extensionName)
251  {
252  if (empty($extensionName)) {
253  throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
254  }
255  }
256 
264  protected static function checkPluginNameFormat($pluginName)
265  {
266  if (empty($pluginName)) {
267  throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
268  }
269  }
270 }
static addPlugin($itemArray, $type= 'list_type', $extensionKey=null)
static addModule($main, $sub= '', $position= '', $path=null, $moduleConfiguration=[])
static registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIconPathAndFilename=null)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static registerModule($extensionName, $mainModuleName= '', $subModuleName= '', $position= '', array $controllerActions=[], array $moduleConfiguration=[])
static addTypoScript($key, $type, $content, $afterStaticUid=0)
static registerTypeConverter($typeConverterClassName)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static checkVendorNameFormat($vendorName, $extensionName)
static configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions=[], $pluginType=self::PLUGIN_TYPE_PLUGIN)