[TASK] Deprecate configureModule in extbase ExtensionUtility
[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 * This means, 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, 'defaultContentRendering');
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 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, please use the according method in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::configureModule
153 */
154 static public function configureModule($moduleSignature, $modulePath) {
155 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
156 return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::configureModule($moduleSignature, $modulePath);
157 }
158
159 /**
160 * Registers an Extbase module (main or sub) to the backend interface.
161 * FOR USE IN ext_tables.php FILES
162 *
163 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
164 * @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
165 * @param string $subModuleName The submodule key.
166 * @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.
167 * @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)
168 * @param array $moduleConfiguration The configuration options of the module (icon, locallang.xlf file)
169 * @throws \InvalidArgumentException
170 * @return void
171 */
172 static public function registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = array(), array $moduleConfiguration = array()) {
173 if (empty($extensionName)) {
174 throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
175 }
176 // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
177 $vendorName = NULL;
178 if (FALSE !== $delimiterPosition = strrpos($extensionName, '.')) {
179 $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
180 $extensionName = substr($extensionName, $delimiterPosition + 1);
181 }
182 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
183 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
184 $defaultModuleConfiguration = array(
185 'access' => 'admin',
186 'icon' => 'EXT:extbase/ext_icon.png',
187 'labels' => '',
188 'extRelPath' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath($extensionKey) . 'Classes/'
189 );
190 if ($mainModuleName !== '' && !array_key_exists($mainModuleName, $GLOBALS['TBE_MODULES'])) {
191 $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
192 } else {
193 $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
194 }
195 // add mandatory parameter to use new pagetree
196 if ($mainModuleName === 'web') {
197 $defaultModuleConfiguration['navigationComponentId'] = 'typo3-pagetree';
198 }
199 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
200 $moduleConfiguration = $defaultModuleConfiguration;
201 $moduleSignature = $mainModuleName;
202 if ($subModuleName !== '') {
203 $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
204 $moduleSignature .= '_' . $subModuleName;
205 }
206 $moduleConfiguration['name'] = $moduleSignature;
207 if (NULL !== $vendorName) {
208 $moduleConfiguration['vendorName'] = $vendorName;
209 }
210 $moduleConfiguration['extensionName'] = $extensionName;
211 $moduleConfiguration['configureModuleFunction'] = array(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::class, 'configureModule');
212 $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature] = $moduleConfiguration;
213 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature])) {
214 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = array();
215 }
216 foreach ($controllerActions as $controllerName => $actions) {
217 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = array(
218 'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
219 );
220 }
221 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position);
222 }
223
224 /**
225 * Register a type converter by class name.
226 *
227 * @param string $typeConverterClassName
228 * @return void
229 * @api
230 */
231 static public function registerTypeConverter($typeConverterClassName) {
232 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
233 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = array();
234 }
235 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
236 }
237
238 }