[BUGFIX] Fix wrong phpdoc for extbase plugin types
[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 * Utilities to manage plugins and modules of an extension. Also useful to auto-generate the autoloader registry
18 * file ext_autoload.php.
19 */
20 class ExtensionUtility {
21
22 const PLUGIN_TYPE_PLUGIN = 'list_type';
23 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
24
25 /**
26 * Add auto-generated TypoScript to configure the Extbase Dispatcher.
27 *
28 * When adding a frontend plugin you will have to add both an entry to the TCA definition
29 * of tt_content table AND to the TypoScript template which must initiate the rendering.
30 * Since the static template with uid 43 is the "content.default" and practically always
31 * used for rendering the content elements it's very useful to have this function automatically
32 * adding the necessary TypoScript for calling the appropriate controller and action of your plugin.
33 * It will also work for the extension "css_styled_content"
34 * FOR USE IN ext_localconf.php FILES
35 * Usage: 2
36 *
37 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
38 * @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!)
39 * @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)
40 * @param array $nonCacheableControllerActions is an optional array of controller name and action names which should not be cached (array as defined in $controllerActions)
41 * @param string $pluginType either \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_PLUGIN (default) or \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT
42 * @throws \InvalidArgumentException
43 * @return void
44 */
45 static public function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = array(), $pluginType = self::PLUGIN_TYPE_PLUGIN) {
46 if (empty($pluginName)) {
47 throw new \InvalidArgumentException('The plugin name must not be empty', 1239891987);
48 }
49 if (empty($extensionName)) {
50 throw new \InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
51 }
52 // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
53 $vendorName = NULL;
54 $delimiterPosition = strrpos($extensionName, '.');
55 if ($delimiterPosition !== FALSE) {
56 $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
57 $extensionName = substr($extensionName, $delimiterPosition + 1);
58 }
59 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
60
61 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
62 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName])) {
63 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = array();
64 }
65 foreach ($controllerActions as $controllerName => $actionsList) {
66 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName] = array('actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList));
67 if (!empty($nonCacheableControllerActions[$controllerName])) {
68 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $nonCacheableControllerActions[$controllerName]);
69 }
70 }
71
72 switch ($pluginType) {
73 case self::PLUGIN_TYPE_PLUGIN:
74 $pluginContent = trim('
75 tt_content.list.20.' . $pluginSignature . ' = USER
76 tt_content.list.20.' . $pluginSignature . ' {
77 userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
78 extensionName = ' . $extensionName . '
79 pluginName = ' . $pluginName . (NULL !== $vendorName ? ("\n\t" . 'vendorName = ' . $vendorName) : '') . '
80 }');
81 break;
82 case self::PLUGIN_TYPE_CONTENT_ELEMENT:
83 $pluginContent = trim('
84 tt_content.' . $pluginSignature . ' = COA
85 tt_content.' . $pluginSignature . ' {
86 10 = < lib.stdheader
87 20 = USER
88 20 {
89 userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
90 extensionName = ' . $extensionName . '
91 pluginName = ' . $pluginName . (NULL !== $vendorName ? ("\n\t\t" . 'vendorName = ' . $vendorName) : '') . '
92 }
93 }');
94 break;
95 default:
96 throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not suported', 1289858856);
97 }
98 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
99 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScript($extensionName, 'setup', '
100 # Setting ' . $extensionName . ' plugin TypoScript
101 ' . $pluginContent, 43);
102 }
103
104 /**
105 * Register an Extbase PlugIn into backend's list of plugins
106 * FOR USE IN ext_tables.php FILES
107 *
108 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
109 * @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!)
110 * @param string $pluginTitle is a speaking title of the plugin that will be displayed in the drop down menu in the backend
111 * @param string $pluginIconPathAndFilename is a path to an icon file (relative to TYPO3_mainDir), that will be displayed in the drop down menu in the backend (optional)
112 * @throws \InvalidArgumentException
113 * @return void
114 */
115 static public function registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIconPathAndFilename = NULL) {
116 if (empty($pluginName)) {
117 throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
118 }
119 if (empty($extensionName)) {
120 throw new \InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891991);
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.
131 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
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
138 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPlugin(
139 array($pluginTitle, $pluginSignature, $pluginIconPathAndFilename),
140 $pluginType,
141 $extensionKey
142 );
143 }
144
145 /**
146 * This method is called from \TYPO3\CMS\Backend\Module\ModuleLoader::checkMod
147 * and it replaces old conf.php.
148 *
149 * @param string $moduleSignature The module name
150 * @param string $modulePath Absolute path to module (not used by Extbase currently)
151 * @return array Configuration of the module
152 */
153 static public function configureModule($moduleSignature, $modulePath) {
154 $moduleConfiguration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
155 $iconPathAndFilename = $moduleConfiguration['icon'];
156 if (substr($iconPathAndFilename, 0, 4) === 'EXT:') {
157 list($extensionKey, $relativePath) = explode('/', substr($iconPathAndFilename, 4), 2);
158 $iconPathAndFilename = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($extensionKey) . $relativePath;
159 }
160 // TODO: skin support
161 $moduleLabels = array(
162 'tabs_images' => array(
163 'tab' => $iconPathAndFilename
164 ),
165 'labels' => array(
166 'tablabel' => $GLOBALS['LANG']->sL($moduleConfiguration['labels'] . ':mlang_labels_tablabel'),
167 'tabdescr' => $GLOBALS['LANG']->sL($moduleConfiguration['labels'] . ':mlang_labels_tabdescr')
168 ),
169 'tabs' => array(
170 'tab' => $GLOBALS['LANG']->sL($moduleConfiguration['labels'] . ':mlang_tabs_tab')
171 )
172 );
173 $GLOBALS['LANG']->addModuleLabels($moduleLabels, $moduleSignature . '_');
174 return $moduleConfiguration;
175 }
176
177 /**
178 * Registers an Extbase module (main or sub) to the backend interface.
179 * FOR USE IN ext_tables.php FILES
180 *
181 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
182 * @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
183 * @param string $subModuleName The submodule key.
184 * @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.
185 * @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)
186 * @param array $moduleConfiguration The configuration options of the module (icon, locallang.xlf file)
187 * @throws \InvalidArgumentException
188 * @return void
189 */
190 static public function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = array(), array $moduleConfiguration = array()) {
191 if (empty($extensionName)) {
192 throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
193 }
194 // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
195 $vendorName = NULL;
196 if (FALSE !== $delimiterPosition = strrpos($extensionName, '.')) {
197 $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
198 $extensionName = substr($extensionName, $delimiterPosition + 1);
199 }
200 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
201 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
202 $defaultModuleConfiguration = array(
203 'access' => 'admin',
204 'icon' => 'EXT:extbase/ext_icon.gif',
205 'labels' => '',
206 'extRelPath' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath($extensionKey) . 'Classes/'
207 );
208 if (strlen($mainModuleName) > 0 && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
209 $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
210 } else {
211 $mainModuleName = strlen($mainModuleName) > 0 ? $mainModuleName : 'web';
212 }
213 // add mandatory parameter to use new pagetree
214 if ($mainModuleName === 'web') {
215 $defaultModuleConfiguration['navigationComponentId'] = 'typo3-pagetree';
216 }
217 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
218 $moduleConfiguration = $defaultModuleConfiguration;
219 $moduleSignature = $mainModuleName;
220 if (strlen($subModuleName) > 0) {
221 $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
222 $moduleSignature .= '_' . $subModuleName;
223 }
224 $moduleConfiguration['name'] = $moduleSignature;
225 if (NULL !== $vendorName) {
226 $moduleConfiguration['vendorName'] = $vendorName;
227 }
228 $moduleConfiguration['extensionName'] = $extensionName;
229 $moduleConfiguration['configureModuleFunction'] = array(\TYPO3\CMS\Extbase\Utility\ExtensionUtility::class, 'configureModule');
230 $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature] = $moduleConfiguration;
231 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature])) {
232 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = array();
233 }
234 foreach ($controllerActions as $controllerName => $actions) {
235 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = array(
236 'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
237 );
238 }
239 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position);
240 }
241
242 /**
243 * Register a type converter by class name.
244 *
245 * @param string $typeConverterClassName
246 * @return void
247 * @api
248 */
249 static public function registerTypeConverter($typeConverterClassName) {
250 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
251 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = array();
252 }
253 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
254 }
255 }