14ac1495a95f64504ae5490cc2af3cd3bfeec373
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / Plugin.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Utilities to manage the plugins of an extension
30 *
31 * @package Extbase
32 * @subpackage Utility
33 * @version $ID:$
34 */
35 class Tx_Extbase_Utility_Plugin {
36
37 /**
38 * Add auto-generated TypoScript to configure the Extbase Dispatcher.
39 *
40 * When adding a frontend plugin you will have to add both an entry to the TCA definition
41 * of tt_content table AND to the TypoScript template which must initiate the rendering.
42 * Since the static template with uid 43 is the "content.default" and practically always
43 * used for rendering the content elements it's very useful to have this function automatically
44 * adding the necessary TypoScript for calling the appropriate controller and action of your plugin.
45 * It will also work for the extension "css_styled_content"
46 * FOR USE IN ext_localconf.php FILES
47 * Usage: 2
48 *
49 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
50 * @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!)
51 * @param string $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)
52 * @param string $nonCachableControllerActions is an optional array of controller name and action names which should not be cached (array as defined in $controllerActions)
53 * @param string $defaultControllerAction is an optional array controller name (as array key) and action name (as array value) that should be called as default
54 * @return void
55 */
56 public static function configureDispatcher($extensionName, $pluginName, array $controllerActions, array $nonCachableControllerActions = array()) {
57 if (empty($pluginName)) {
58 throw new InvalidArgumentException('The plugin name must not be empty', 1239891987);
59 }
60 if (empty($extensionName)) {
61 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
62 }
63 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
64 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
65
66 $controllerCounter = 1;
67 $hasMultipleActionsCounter = 0;
68 $controllers = '';
69 foreach ($controllerActions as $controller => $actionsList) {
70 $controllers .= '
71 ' . $controllerCounter . '.controller = ' . $controller . '
72 ' . $controllerCounter . '.actions = ' . $actionsList;
73 $controllerCounter++;
74 if (strpos($actionsList, ',') !== FALSE) {
75 $hasMultipleActionsCounter++;
76 }
77 }
78
79 $switchableControllerActions = '';
80 if ($controllerCounter > 1 || $hasMultipleActionsCounter > 0) {
81 $switchableControllerActions = '
82 switchableControllerActions {' . $controllers . '
83 }';
84 }
85
86 reset($controllerActions);
87 $defaultController = key($controllerActions);
88 $controller = '
89 controller = ' . $defaultController;
90 $defaultAction = array_shift(t3lib_div::trimExplode(',', current($controllerActions)));
91 $action = '
92 action = ' . $defaultAction;
93
94 $nonCachableActions = array();
95 if (!empty($nonCachableControllerActions[$defaultController])) {
96 $nonCachableActions = t3lib_div::trimExplode(',', $nonCachableControllerActions[$defaultController]);
97 }
98 $cachableActions = array_diff(t3lib_div::trimExplode(',', $controllerActions[$defaultController]), $nonCachableActions);
99
100 $contentObjectType = in_array($defaultAction, $nonCachableActions) ? 'USER_INT' : 'USER';
101
102 $conditions = '';
103 foreach ($controllerActions as $controllerName => $actionsList) {
104 if (!empty($nonCachableControllerActions[$controllerName])) {
105 $nonCachableActions = t3lib_div::trimExplode(',', $nonCachableControllerActions[$controllerName]);
106 $cachableActions = array_diff(t3lib_div::trimExplode(',', $controllerActions[$controllerName]), $nonCachableActions);
107 if (($contentObjectType == 'USER' && count($nonCachableActions) > 0)
108 || ($contentObjectType == 'USER_INT' && count($cachableActions) > 0)) {
109
110 $conditions .= '
111 [globalString = GP:tx_' . $pluginSignature . '|controller = ' . $controllerName . '] && [globalString = GP:tx_' . $pluginSignature . '|action = /' . implode('|', $contentObjectType === 'USER' ? $nonCachableActions : $cachableActions) . '/]
112 tt_content.list.20.' . $pluginSignature . ' = ' . ($contentObjectType === 'USER' ? 'USER_INT' : 'USER') . '
113 [global]
114 ';
115 }
116 }
117 }
118
119 $pluginTemplate = trim('plugin.tx_' . strtolower($extensionName) . '.settings {
120 }
121 plugin.tx_' . strtolower($extensionName) . '.persistence {
122 enableAutomaticCacheClearing = 1
123 # storagePid
124 classes {
125 }
126 }');
127 t3lib_extMgm::addTypoScript($extensionName, 'setup', '
128 # Setting ' . $extensionName . ' plugin TypoScript
129 ' . $pluginTemplate);
130
131 $pluginContent = trim('
132 tt_content.list.20.' . $pluginSignature . ' = ' . $contentObjectType . '
133 tt_content.list.20.' . $pluginSignature . ' {
134 userFunc = tx_extbase_dispatcher->dispatch
135 pluginName = ' . $pluginName . '
136 extensionName = ' . $extensionName . '
137 ' . $controller .
138 $action .
139 $switchableControllerActions . '
140
141 persistence =< plugin.tx_' . strtolower($extensionName) . '.persistence
142 }
143 ' . $conditions);
144
145 t3lib_extMgm::addTypoScript($extensionName, 'setup', '
146 # Setting ' . $extensionName . ' plugin TypoScript
147 ' . $pluginContent, 43);
148 }
149
150 /**
151 * Register an Extbase PlugIn into backend's list of plugins
152 * FOR USE IN ext_tables.php FILES
153 *
154 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
155 * @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!)
156 * @param string $pluginTitle is a speaking title of the plugin that will be displayed in the drop down menu in the backend
157 * @return void
158 */
159 public static function registerPlugin($extensionName, $pluginName, $pluginTitle) {
160 if (empty($pluginName)) {
161 throw new InvalidArgumentException('The plugin name must not be empty', 1239891987);
162 }
163 if (empty($extensionName)) {
164 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
165 }
166 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
167 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
168
169 t3lib_extMgm::addPlugin(array($pluginTitle, $pluginSignature), 'list_type');
170 }
171
172 // TODO Remove the next 3 functions; only for compatibility resons
173
174 public static function convertCamelCaseToLowerCaseUnderscored($string) {
175 static $conversionMap = array();
176 if (!isset($conversionMap[$string])) {
177 $conversionMap[$string] = strtolower(preg_replace('/(?<=\w)([A-Z])/', '_\\1', $string));
178 }
179 return $conversionMap[$string];
180 }
181
182 public static function convertUnderscoredToLowerCamelCase($string) {
183 $string = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($string))));
184 $string[0] = strtolower($string[0]);
185 return $string;
186 }
187
188 public static function convertLowerUnderscoreToUpperCamelCase($camelCasedString) {
189 return t3lib_div::underscoredToUpperCamelCase($camelCasedString);
190 }
191
192 }
193 ?>