[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / ExtensionUtility.php
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
17 /**
18 * Utilities to manage plugins and modules of an extension. Also useful to auto-generate the autoloader registry
19 * file ext_autoload.php.
20 */
21 class ExtensionUtility
22 {
23 const PLUGIN_TYPE_PLUGIN = 'list_type';
24 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
25
26 /**
27 * Add auto-generated TypoScript to configure the Extbase Dispatcher.
28 *
29 * When adding a frontend plugin you will have to add both an entry to the TCA definition
30 * of tt_content table AND to the TypoScript template which must initiate the rendering.
31 * Including the plugin code after "defaultContentRendering" adds the necessary TypoScript
32 * for calling the appropriate controller and action of your plugin.
33 * FOR USE IN ext_localconf.php FILES
34 * Usage: 2
35 *
36 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
37 * @param string $pluginName must be a unique id for your plugin in UpperCamelCase (the string length of the extension key added to the length of the plugin name should be less than 32!)
38 * @param array $controllerActions is an array of allowed combinations of controller and action stored in an array (controller name as key and a comma separated list of action names as value, the first controller and its first action is chosen as default)
39 * @param array $nonCacheableControllerActions is an optional array of controller name and action names which should not be cached (array as defined in $controllerActions)
40 * @param string $pluginType either \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_PLUGIN (default) or \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT
41 * @throws \InvalidArgumentException
42 */
43 public static function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)
44 {
45 self::checkPluginNameFormat($pluginName);
46 self::checkExtensionNameFormat($extensionName);
47
48 // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
49 $vendorName = null;
50 $delimiterPosition = strrpos($extensionName, '.');
51 if ($delimiterPosition !== false) {
52 trigger_error(
53 'Calling method ' . __METHOD__ . 'with argument $extensionName containing the vendor name is deprecated and will stop working in TYPO3 11.0.',
54 E_USER_DEPRECATED
55 );
56 $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
57 $extensionName = substr($extensionName, $delimiterPosition + 1);
58
59 if (!empty($vendorName)) {
60 self::checkVendorNameFormat($vendorName, $extensionName);
61 }
62 }
63 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
64
65 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
66 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] ?? false)) {
67 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = [];
68 }
69 foreach ($controllerActions as $controllerClassName => $actionsList) {
70 if (class_exists($controllerClassName)) {
71 $controllerAlias = self::resolveControllerAliasFromControllerClassName($controllerClassName);
72 $vendorName = self::resolveVendorFromExtensionAndControllerClassName($extensionName, $controllerClassName);
73 if (!empty($vendorName)) {
74 self::checkVendorNameFormat($vendorName, $extensionName);
75 }
76 } else {
77 trigger_error(
78 'Calling ' . __METHOD__ . ' with controller aliases in argument $controllerActions is deprecated and will stop working in TYPO3 11.0.',
79 E_USER_DEPRECATED
80 );
81 $controllerAlias = $controllerClassName;
82 $controllerClassName = static::getControllerClassName((string)$vendorName, $extensionName, '', $controllerAlias);
83 }
84
85 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerClassName] = [
86 'className' => $controllerClassName,
87 'alias' => $controllerAlias,
88 'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList)
89 ];
90
91 if (isset($nonCacheableControllerActions[$controllerAlias]) && !empty($nonCacheableControllerActions[$controllerAlias])) {
92 trigger_error(
93 'Calling ' . __METHOD__ . ' with controller aliases in argument $nonCacheableControllerActions is deprecated and will stop working in TYPO3 11.0.',
94 E_USER_DEPRECATED
95 );
96 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerClassName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(
97 ',',
98 $nonCacheableControllerActions[$controllerAlias]
99 );
100 }
101
102 if (isset($nonCacheableControllerActions[$controllerClassName]) && !empty($nonCacheableControllerActions[$controllerClassName])) {
103 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerClassName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(
104 ',',
105 $nonCacheableControllerActions[$controllerClassName]
106 );
107 }
108 }
109
110 switch ($pluginType) {
111 case self::PLUGIN_TYPE_PLUGIN:
112 $pluginContent = trim('
113 tt_content.list.20.' . $pluginSignature . ' = USER
114 tt_content.list.20.' . $pluginSignature . ' {
115 userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
116 extensionName = ' . $extensionName . '
117 pluginName = ' . $pluginName . '
118 }');
119 break;
120 case self::PLUGIN_TYPE_CONTENT_ELEMENT:
121 $pluginContent = trim('
122 tt_content.' . $pluginSignature . ' =< lib.contentElement
123 tt_content.' . $pluginSignature . ' {
124 templateName = Generic
125 20 = USER
126 20 {
127 userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
128 extensionName = ' . $extensionName . '
129 pluginName = ' . $pluginName . '
130 }
131 }');
132 break;
133 default:
134 throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not suported', 1289858856);
135 }
136 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
137 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScript($extensionName, 'setup', '
138 # Setting ' . $extensionName . ' plugin TypoScript
139 ' . $pluginContent, 'defaultContentRendering');
140 }
141
142 /**
143 * Register an Extbase PlugIn into backend's list of plugins
144 * FOR USE IN Configuration/TCA/Overrides/tt_content.php
145 *
146 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
147 * @param string $pluginName must be a unique id for your plugin in UpperCamelCase (the string length of the extension key added to the length of the plugin name should be less than 32!)
148 * @param string $pluginTitle is a speaking title of the plugin that will be displayed in the drop down menu in the backend
149 * @param string $pluginIcon is an icon identifier or file path prepended with "EXT:", that will be displayed in the drop down menu in the backend (optional)
150 * @throws \InvalidArgumentException
151 */
152 public static function registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon = null)
153 {
154 self::checkPluginNameFormat($pluginName);
155 self::checkExtensionNameFormat($extensionName);
156
157 $delimiterPosition = strrpos($extensionName, '.');
158 if ($delimiterPosition !== false) {
159 trigger_error(
160 'Calling method ' . __METHOD__ . ' with argument $extensionName containing the vendor name is deprecated and will stop working in TYPO3 11.0.',
161 E_USER_DEPRECATED
162 );
163 $extensionName = substr($extensionName, $delimiterPosition + 1);
164 }
165 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
166 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
167
168 // At this point $extensionName is normalized, no matter which format the method was fed with.
169 // Calculate the original extensionKey from this again.
170 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
171
172 // pluginType is usually defined by configurePlugin() in the global array. Use this or fall back to default "list_type".
173 $pluginType = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] ?? 'list_type';
174
175 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPlugin(
176 [$pluginTitle, $pluginSignature, $pluginIcon],
177 $pluginType,
178 $extensionKey
179 );
180 }
181
182 /**
183 * Registers an Extbase module (main or sub) to the backend interface.
184 * FOR USE IN ext_tables.php FILES
185 *
186 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
187 * @param string $mainModuleName The main module key. So $main would be an index in the $TBE_MODULES array and $sub could be an element in the lists there. If $subModuleName is not set a blank $extensionName module is created
188 * @param string $subModuleName The submodule key.
189 * @param string $position This can be used to set the position of the $sub module within the list of existing submodules for the main module. $position has this syntax: [cmd]:[submodule-key]. cmd can be "after", "before" or "top" (or blank which is default). If "after"/"before" then submodule will be inserted after/before the existing submodule with [submodule-key] if found. If not found, the bottom of list. If "top" the module is inserted in the top of the submodule list.
190 * @param array $controllerActions is an array of allowed combinations of controller and action stored in an array (controller name as key and a comma separated list of action names as value, the first controller and its first action is chosen as default)
191 * @param array $moduleConfiguration The configuration options of the module (icon, locallang.xlf file)
192 * @throws \InvalidArgumentException
193 */
194 public static function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = [], array $moduleConfiguration = [])
195 {
196 self::checkExtensionNameFormat($extensionName);
197
198 // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
199 $vendorName = null;
200 if (false !== $delimiterPosition = strrpos($extensionName, '.')) {
201 trigger_error(
202 'Calling method ' . __METHOD__ . 'with argument $extensionName containing the vendor name is deprecated and will stop working in TYPO3 11.0.',
203 E_USER_DEPRECATED
204 );
205 $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
206 $extensionName = substr($extensionName, $delimiterPosition + 1);
207
208 if (!empty($vendorName)) {
209 self::checkVendorNameFormat($vendorName, $extensionName);
210 }
211 }
212
213 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
214 $defaultModuleConfiguration = [
215 'access' => 'admin',
216 'icon' => 'EXT:extbase/Resources/Public/Icons/Extension.png',
217 'labels' => ''
218 ];
219 if ($mainModuleName !== '' && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
220 $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
221 } else {
222 $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
223 }
224 // add mandatory parameter to use new pagetree
225 if ($mainModuleName === 'web') {
226 $defaultModuleConfiguration['navigationComponentId'] = 'TYPO3/CMS/Backend/PageTree/PageTreeElement';
227 }
228 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
229 $moduleConfiguration = $defaultModuleConfiguration;
230 $moduleSignature = $mainModuleName;
231 if ($subModuleName !== '') {
232 $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
233 $moduleSignature .= '_' . $subModuleName;
234 }
235 $moduleConfiguration['name'] = $moduleSignature;
236 $moduleConfiguration['extensionName'] = $extensionName;
237 $moduleConfiguration['routeTarget'] = \TYPO3\CMS\Extbase\Core\Bootstrap::class . '::handleBackendRequest';
238 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] ?? false)) {
239 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = [];
240 }
241 foreach ($controllerActions as $controllerClassName => $actionsList) {
242 if (class_exists($controllerClassName)) {
243 $controllerAlias = self::resolveControllerAliasFromControllerClassName($controllerClassName);
244 $vendorName = self::resolveVendorFromExtensionAndControllerClassName($extensionName, $controllerClassName);
245 if (!empty($vendorName)) {
246 self::checkVendorNameFormat($vendorName, $extensionName);
247 }
248 } else {
249 trigger_error(
250 'Calling ' . __METHOD__ . ' with controller aliases is deprecated and will stop working in TYPO3 11.0.',
251 E_USER_DEPRECATED
252 );
253 $controllerAlias = $controllerClassName;
254 $controllerClassName = static::getControllerClassName($vendorName, $extensionName, '', $controllerAlias);
255 }
256
257 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerClassName] = [
258 'className' => $controllerClassName,
259 'alias' => $controllerAlias,
260 'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList)
261 ];
262 }
263 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position, null, $moduleConfiguration);
264 }
265
266 /**
267 * Returns the object name of the controller defined by the extension name and
268 * controller name
269 *
270 * @param string $vendor
271 * @param string $extensionKey
272 * @param string $subPackageKey
273 * @param string $controllerAlias
274 * @return string The controller's Object Name
275 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchControllerException if the controller does not exist
276 */
277 public static function getControllerClassName(
278 string $vendor,
279 string $extensionKey,
280 string $subPackageKey,
281 string $controllerAlias
282 ): string {
283 $objectName = str_replace(
284 [
285 '@extension',
286 '@subpackage',
287 '@controller',
288 '@vendor',
289 '\\\\'
290 ],
291 [
292 $extensionKey,
293 $subPackageKey,
294 $controllerAlias,
295 $vendor,
296 '\\'
297 ],
298 '@vendor\@extension\@subpackage\Controller\@controllerController'
299 );
300
301 if ($objectName === false) {
302 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchControllerException('The controller object "' . $objectName . '" does not exist.', 1220884009);
303 }
304 return trim($objectName, '\\');
305 }
306
307 /**
308 * @param string $controllerClassName
309 * @return string
310 */
311 public static function resolveControllerAliasFromControllerClassName(string $controllerClassName): string
312 {
313 if (strrpos($controllerClassName, 'Controller') === false) {
314 return '';
315 }
316
317 return trim(substr(
318 $controllerClassName,
319 (int)strrpos($controllerClassName, '\\'),
320 -strlen('Controller')
321 ), '\\');
322 }
323
324 /**
325 * @param string $extensionName
326 * @param string $controllerClassName
327 * @return string
328 */
329 public static function resolveVendorFromExtensionAndControllerClassName(string $extensionName, string $controllerClassName): string
330 {
331 if (strpos($controllerClassName, '\\') === false) {
332 // Does not work with non namespaced classes
333 return '';
334 }
335
336 if (false === $extensionNamePosition = strpos($controllerClassName, $extensionName)) {
337 // Does not work for classes that do not include the extension name as namespace part
338 return '';
339 }
340
341 if (--$extensionNamePosition < 0) {
342 return '';
343 }
344
345 return substr(
346 $controllerClassName,
347 0,
348 $extensionNamePosition
349 );
350 }
351
352 /**
353 * Register a type converter by class name.
354 *
355 * @param string $typeConverterClassName
356 */
357 public static function registerTypeConverter($typeConverterClassName)
358 {
359 if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']) ||
360 !is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])
361 ) {
362 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
363 }
364 if (!in_array($typeConverterClassName, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
365 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
366 }
367 }
368
369 /**
370 * Check a given vendor name for CGL compliance.
371 * Log a deprecation message if it is not.
372 *
373 * @param string $vendorName The vendor name to check
374 * @param string $extensionName The extension name that is affected
375 */
376 protected static function checkVendorNameFormat($vendorName, $extensionName)
377 {
378 if (preg_match('/^[A-Z]/', $vendorName) !== 1) {
379 trigger_error('The vendor name from tx_' . $extensionName . ' must begin with a capital letter.', E_USER_DEPRECATED);
380 }
381 }
382
383 /**
384 * Check a given extension name for validity.
385 *
386 * @param string $extensionName The name of the extension
387 * @throws \InvalidArgumentException
388 */
389 protected static function checkExtensionNameFormat($extensionName)
390 {
391 if (empty($extensionName)) {
392 throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
393 }
394 }
395
396 /**
397 * Check a given plugin name for validity.
398 *
399 * @param string $pluginName The name of the plugin
400 * @throws \InvalidArgumentException
401 */
402 protected static function checkPluginNameFormat($pluginName)
403 {
404 if (empty($pluginName)) {
405 throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
406 }
407 }
408 }