[!!!][~TASK] Extbase (Configuration): Major rework of the ConfigurationManager
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / Extension.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Utilities to manage plugins and modules of an extension. Also useful to auto-generate the autoloader registry
27 * file ext_autoload.php.
28 *
29 * @package Extbase
30 * @subpackage Utility
31 * @version $ID:$
32 */
33 class Tx_Extbase_Utility_Extension {
34
35 /**
36 * Add auto-generated TypoScript to configure the Extbase Dispatcher.
37 *
38 * When adding a frontend plugin you will have to add both an entry to the TCA definition
39 * of tt_content table AND to the TypoScript template which must initiate the rendering.
40 * Since the static template with uid 43 is the "content.default" and practically always
41 * used for rendering the content elements it's very useful to have this function automatically
42 * adding the necessary TypoScript for calling the appropriate controller and action of your plugin.
43 * It will also work for the extension "css_styled_content"
44 * FOR USE IN ext_localconf.php FILES
45 * Usage: 2
46 *
47 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
48 * @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!)
49 * @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)
50 * @param string $nonCacheableControllerActions is an optional array of controller name and action names which should not be cached (array as defined in $controllerActions)
51 * @param string $defaultControllerAction is an optional array controller name (as array key) and action name (as array value) that should be called as default
52 * @return void
53 */
54 static public function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = array()) {
55 if (empty($pluginName)) {
56 throw new InvalidArgumentException('The plugin name must not be empty', 1239891987);
57 }
58 if (empty($extensionName)) {
59 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
60 }
61 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
62 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
63 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName][$pluginName])) {
64 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName][$pluginName] = array();
65 }
66
67 foreach ($controllerActions as $controllerName => $actionsList) {
68 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName][$pluginName]['controllers'][$controllerName] = array('actions' => t3lib_div::trimExplode(',', $actionsList));
69 if (!empty($nonCacheableControllerActions[$controllerName])) {
70 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName][$pluginName]['controllers'][$controllerName]['nonCacheableActions'] = t3lib_div::trimExplode(',', $nonCacheableControllerActions[$controllerName]);
71 }
72 }
73
74 $pluginTemplate = 'plugin.tx_' . strtolower($extensionName) . ' {
75 settings {
76 }
77 persistence {
78 storagePid =
79 classes {
80 }
81 }
82 view {
83 templateRootPath =
84 layoutRootPath =
85 partialRootPath =
86 # with defaultPid you can specify the default page uid of this plugin. If you set this to the string "auto" the target page will be determined automatically. Defaults to an empty string that expects the target page to be the current page.
87 defaultPid =
88 }
89 }';
90 t3lib_extMgm::addTypoScript($extensionName, 'setup', '
91 # Setting ' . $extensionName . ' plugin TypoScript
92 ' . $pluginTemplate);
93
94 $pluginContent = trim('
95 tt_content.list.20.' . $pluginSignature . ' = USER
96 tt_content.list.20.' . $pluginSignature . ' {
97 userFunc = tx_extbase_core_bootstrap->run
98 extensionName = ' . $extensionName . '
99 pluginName = ' . $pluginName . '
100 }');
101
102 t3lib_extMgm::addTypoScript($extensionName, 'setup', '
103 # Setting ' . $extensionName . ' plugin TypoScript
104 ' . $pluginContent, 43);
105 }
106
107 /**
108 * Register an Extbase PlugIn into backend's list of plugins
109 * FOR USE IN ext_tables.php FILES
110 *
111 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
112 * @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!)
113 * @param string $pluginTitle is a speaking title of the plugin that will be displayed in the drop down menu in the backend
114 * @return void
115 */
116 static public function registerPlugin($extensionName, $pluginName, $pluginTitle) {
117 if (empty($pluginName)) {
118 throw new InvalidArgumentException('The plugin name must not be empty', 1239891987);
119 }
120 if (empty($extensionName)) {
121 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
122 }
123 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
124 $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
125
126 t3lib_extMgm::addPlugin(array($pluginTitle, $pluginSignature), 'list_type');
127 }
128
129 /**
130 * This method is called from t3lib_loadModules::checkMod and it replaces old conf.php.
131 *
132 * @param string $key The module name
133 * @param string $fullpath Absolute path to module
134 * @param array $MCONF Reference to the array holding the configuration of the module
135 * @param array $MLANG Reference to the array holding the localized module labels
136 * @return array Configuration of the module
137 */
138 public function configureModule($key, $fullpath, array $MCONF = array(), array $MLANG = array()) {
139 $path = preg_replace('/\/[^\/.]+\/\.\.\//', '/', $fullpath); // because 'path/../path' does not work
140 $config = $GLOBALS['TBE_MODULES']['_configuration'][$key]['config'];
141 define('TYPO3_MOD_PATH', $config['extRelPath']);
142
143 // Fill $MCONF
144 $MCONF['name'] = $key;
145 $MCONF['access'] = $config['access'];
146 $MCONF['script'] = '_DISPATCH';
147
148 if (substr($config['icon'], 0, 4) === 'EXT:') {
149 list($extKey, $local) = explode('/', substr($config['icon'], 4), 2);
150 $config['icon'] = t3lib_extMgm::extRelPath($extKey) . $local;
151 }
152
153 // Initialize search for alternative icon:
154 $altIconKey = 'MOD:' . $key . '/' . $config['icon']; // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
155 $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? t3lib_div::resolveBackPath(PATH_typo3.$GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
156
157 // Set icon, either default or alternative:
158 if ($altIconAbsPath && @is_file($altIconAbsPath)) {
159 $tabImage = $altIconAbsPath;
160 } else {
161 // Setting default icon:
162 $tabImage = $config['icon'];
163 }
164
165 // Fill $MLANG
166 $MLANG['default']['ll_ref'] = $config['labels'];
167
168 // Finally, set the icon with correct path:
169 if (substr($tabImage, 0 ,3) === '../') {
170 $MLANG['default']['tabs_images']['tab'] = PATH_site . substr($tabImage, 3);
171 } else {
172 $MLANG['default']['tabs_images']['tab'] = PATH_typo3 . $tabImage;
173 }
174
175 // If LOCAL_LANG references are used for labels of the module:
176 if ($MLANG['default']['ll_ref']) {
177 // Now the 'default' key is loaded with the CURRENT language - not the english translation...
178 $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_labels_tablabel');
179 $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_labels_tabdescr');
180 $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_tabs_tab');
181 $GLOBALS['LANG']->addModuleLabels($MLANG['default'], $key . '_');
182 } else { // ... otherwise use the old way:
183 $GLOBALS['LANG']->addModuleLabels($MLANG['default'], $key . '_');
184 $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang], $key . '_');
185 }
186
187 // Fill $modconf
188 $modconf['script'] = 'mod.php?M=' . rawurlencode($key);
189 $modconf['name'] = $key;
190
191 // Default tab setting
192 if ($MCONF['defaultMod']) {
193 $modconf['defaultMod'] = $MCONF['defaultMod'];
194 }
195 // Navigation Frame Script (GET params could be added)
196 if ($MCONF['navFrameScript']) {
197 $navFrameScript = explode('?', $MCONF['navFrameScript']);
198 $navFrameScript = $navFrameScript[0];
199 if (file_exists($path . '/' . $navFrameScript)) {
200 $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3, $fullpath . '/' . $MCONF['navFrameScript']);
201 }
202 }
203
204 // Additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
205 if ($MCONF['navFrameScriptParam']) {
206 $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
207 }
208
209 return $modconf;
210 }
211
212 /**
213 * Registers an Extbase module (main or sub) to the backend interface.
214 * FOR USE IN ext_tables.php FILES
215 *
216 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
217 * @param string $main The main module key, $sub is the submodule key. So $main would be an index in the $TBE_MODULES array and $sub could be an element in the lists there. If $main is not set a blank $extensionName module is created
218 * @param string $sub The submodule key. If $sub is not set a blank $main module is created
219 * @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.
220 * @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)
221 * @param array $config The configuration options of the module (icon, locallang.xml file)
222 * @return void
223 */
224 static public function registerModule($extensionName, $main = '', $sub = '', $position = '', array $controllerActions, $config = array()) {
225 if (empty($extensionName)) {
226 throw new InvalidArgumentException('The extension name was invalid (must not be empty and must match /[A-Za-z][_A-Za-z0-9]/)', 1239891989);
227 }
228 $extensionKey = $extensionName; // FIXME This will break if the $extensionName is given as BlogExample
229 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
230
231 $path = t3lib_extMgm::extPath($extensionKey, 'Classes/');
232 $relPath = t3lib_extMgm::extRelPath($extensionKey) . 'Classes/';
233
234 if (!is_array($config) || count($config) == 0) {
235 $config['access'] = 'admin';
236 $config['icon'] = '';
237 $config['labels'] = '';
238 $config['extRelPath'] = $relPath;
239 }
240
241 if ((strlen($main) > 0) && !array_key_exists($main, $GLOBALS['TBE_MODULES'])) {
242 $main = $extensionName . self::convertLowerUnderscoreToUpperCamelCase($main);
243 } else {
244 $main = (strlen($main) > 0) ? $main : 'web'; // TODO By now, $main must default to 'web'
245 }
246
247 if ((strlen($sub) > 0)) {
248 //$sub = $extensionName . self::convertLowerUnderscoreToUpperCamelCase($sub);
249 $key = $main . '_' . $sub;
250 } else {
251 $key = $main;
252 }
253
254 $moduleConfig = array(
255 'name' => $key,
256 'extensionKey' => $extensionKey,
257 'extensionName' => $extensionName,
258 'controllerActions' => $controllerActions,
259 'config' => $config,
260 );
261 $GLOBALS['TBE_MODULES']['_configuration'][$key] = $moduleConfig;
262 $GLOBALS['TBE_MODULES']['_configuration'][$key]['configureModuleFunction'] = array('Tx_Extbase_Utility_Extension', 'configureModule');
263
264 t3lib_extMgm::addModule($main, $sub, $position);
265 }
266
267 /**
268 * Returns a given CamelCasedString as an lowercase string with underscores.
269 * Example: Converts BlogExample to blog_example, and minimalValue to minimal_value
270 *
271 * @param string $string
272 * @return mixed
273 * @see t3lib_div::underscoredToLowerCamelCase()
274 * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
275 */
276 static public function convertCamelCaseToLowerCaseUnderscored($string) {
277 return t3lib_div::camelCaseToLowerCaseUnderscored($string);
278 }
279
280 /**
281 * Returns a given string with underscores as lowerCamelCase.
282 * Example: Converts minimal_value to minimalValue
283 *
284 * @param string $string
285 * @return mixed
286 * @see t3lib_div::underscoredToLowerCamelCase()
287 * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
288 */
289 static public function convertUnderscoredToLowerCamelCase($string) {
290 return t3lib_div::underscoredToLowerCamelCase($string);
291 }
292
293 /**
294 * Returns a given string with underscores as UpperCamelCase.
295 * Example: Converts blog_example to BlogExample
296 *
297 * @param string $string
298 * @return string
299 * @see t3lib_div::underscoredToUpperCamelCase()
300 * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
301 */
302 static public function convertLowerUnderscoreToUpperCamelCase($string) {
303 return t3lib_div::underscoredToUpperCamelCase($string);
304 }
305
306 /**
307 * Build the autoload registry for a given extension and place it ext_autoload.php.
308 *
309 * @param string $extensionKey Key of the extension
310 * @param string $extensionPath full path of the extension
311 * @param array $additionalAutoloadClasses additional classes to be added to the autoloader. The key must be the classname all-lowercase, the value must be the entry to be inserted
312 * @return string HTML string which should be outputted
313 */
314 static public function createAutoloadRegistryForExtension($extensionKey, $extensionPath, $additionalAutoloadClasses = array()) {
315 $classNameToFileMapping = array();
316 $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionKey)));
317 $errors = self::buildAutoloadRegistryForSinglePath($classNameToFileMapping, $extensionPath . 'Classes/', '.*tslib.*', '$extensionClassesPath . \'|\'');
318 if ($errors) {
319 return $errors;
320 }
321 $globalPrefix = '$extensionClassesPath = t3lib_extMgm::extPath(\'' . $extensionKey . '\') . \'Classes/\';';
322
323 $errors = array();
324 foreach ($classNameToFileMapping as $className => $fileName) {
325 if (!(strpos($className, 'tx_' . strtolower($extensionName)) === 0)) {
326 $errors[] = $className . ' does not start with Tx_' . $extensionName . ' and was not added to the autoloader registry.';
327 unset($classNameToFileMapping[$className]);
328 }
329 }
330 $classNameToFileMapping = array_merge($classNameToFileMapping, $additionalAutoloadClasses);
331 $autoloadFileString = self::generateAutoloadPHPFileData($classNameToFileMapping, $globalPrefix);
332 if (!@file_put_contents($extensionPath . 'ext_autoload.php', $autoloadFileString)) {
333 $errors[] = '<b>' . $extensionPath . 'ext_autoload.php could not be written!</b>';
334 }
335 $errors[] = 'Wrote the following data: <pre>' . htmlspecialchars($autoloadFileString) . '</pre>';
336 return implode('<br />', $errors);
337 }
338
339 /**
340 * Generate autoload PHP file data. Takes an associative array with class name to file mapping, and outputs it as PHP.
341 * Does NOT escape the values in the associative array. Includes the <?php ... ?> syntax and an optional global prefix.
342 *
343 * @param array $classNameToFileMapping class name to file mapping
344 * @param string $globalPrefix Global prefix which is prepended to all code.
345 * @return string The full PHP string
346 */
347 protected function generateAutoloadPHPFileData($classNameToFileMapping, $globalPrefix = '') {
348 $output = '<?php' . PHP_EOL;
349 $output .= '// DO NOT CHANGE THIS FILE! It is automatically generated by Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension.' . PHP_EOL;
350 $output .= '// This file was generated on ' . date('Y-m-d H:i') . PHP_EOL;
351 $output .= PHP_EOL;
352 $output .= $globalPrefix . PHP_EOL;
353 $output .= 'return array(' . PHP_EOL;
354 foreach ($classNameToFileMapping as $className => $quotedFileName) {
355 $output .= ' \'' . $className . '\' => ' . $quotedFileName . ',' . PHP_EOL;
356 }
357 $output .= ');' . PHP_EOL;
358 $output .= '?>';
359 return $output;
360 }
361
362 /**
363 * Generate the $classNameToFileMapping for a given filePath.
364 *
365 * @param array $classNameToFileMapping (Reference to array) All values are appended to this array.
366 * @param string $path Path which should be crawled
367 * @param string $excludeRegularExpression Exclude regular expression, to exclude certain files from being processed
368 * @param string $valueWrap Wrap for the file name
369 * @return void
370 */
371 static protected function buildAutoloadRegistryForSinglePath(&$classNameToFileMapping, $path, $excludeRegularExpression = '', $valueWrap = '\'|\'') {
372 // if (file_exists($path . 'Classes/')) {
373 // return "<b>This appears to be a new-style extension which has its PHP classes inside the Classes/ subdirectory. It is not needed to generate the autoload registry for these extensions.</b>";
374 // }
375 $extensionFileNames = t3lib_div::removePrefixPathFromList(t3lib_div::getAllFilesAndFoldersInPath(array(), $path, 'php', FALSE, 99, $excludeRegularExpression), $path);
376
377 foreach ($extensionFileNames as $extensionFileName) {
378 $classNamesInFile = self::extractClassNames($path . $extensionFileName);
379 if (!count($classNamesInFile)) continue;
380 foreach ($classNamesInFile as $className) {
381 $classNameToFileMapping[strtolower($className)] = str_replace('|', $extensionFileName, $valueWrap);
382 }
383 }
384 }
385
386 /**
387 * Extracts class names from the given file.
388 *
389 * @param string $filePath File path (absolute)
390 * @return array Class names
391 */
392 static protected function extractClassNames($filePath) {
393 $fileContent = php_strip_whitespace($filePath);
394 $classNames = array();
395 if (FALSE) {
396 $tokens = token_get_all($fileContent);
397 while(1) {
398 // look for "class" or "interface"
399 $token = self::findToken($tokens, array(T_ABSTRACT, T_CLASS, T_INTERFACE));
400 // fetch "class" token if "abstract" was found
401 if ($token === 'abstract') {
402 $token = self::findToken($tokens, array(T_CLASS));
403 }
404 if ($token === false) {
405 // end of file
406 break;
407 }
408 // look for the name (a string) skipping only whitespace and comments
409 $token = self::findToken($tokens, array(T_STRING), array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT));
410 if ($token === false) {
411 // unexpected end of file or token: remove found names because of parse error
412 t3lib_div::sysLog('Parse error in "' . $filePath. '".', 'Core', 2);
413 $classNames = array();
414 break;
415 }
416 $token = t3lib_div::strtolower($token);
417 // exclude XLASS classes
418 if (strncmp($token, 'ux_', 3)) {
419 $classNames[] = $token;
420 }
421 }
422 } else {
423 // TODO: parse PHP - skip coments and strings, apply regexp only on the remaining PHP code
424 $matches = array();
425 preg_match_all('/^[ \t]*(?:(?:abstract|final)?[ \t]*(?:class|interface))[ \t\n\r]+([a-zA-Z][a-zA-Z_0-9]*)/mS', $fileContent, $matches);
426 $classNames = array_map('t3lib_div::strtolower', $matches[1]);
427 }
428 return $classNames;
429 }
430
431 /**
432 * Find tokens in the tokenList
433 *
434 * @param array $tokenList list of tokens as returned by token_get_all()
435 * @param array $wantedToken the tokens to be found
436 * @param array $intermediateTokens optional: list of tokens that are allowed to skip when looking for the wanted token
437 * @return mixed
438 */
439 static protected function findToken(array &$tokenList, array $wantedTokens, array $intermediateTokens = array()) {
440 $skipAllTokens = count($intermediateTokens) ? false : true;
441
442 $returnValue = false;
443 // Iterate with while since we need the current array position:
444 foreach ($tokenList as $token) {
445 // parse token (see http://www.php.net/manual/en/function.token-get-all.php for format of token list)
446 if (is_array($token)) {
447 list($id, $text) = $token;
448 } else {
449 $id = $text = $token;
450 }
451 if (in_array($id, $wantedTokens)) {
452 $returnValue = $text;
453 break;
454 }
455 // look for another token
456 if ($skipAllTokens || in_array($id, $intermediateTokens)) {
457 continue;
458 }
459 break;
460 }
461 return $returnValue;
462 }
463
464 /**
465 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
466 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
467 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
468 *
469 * @param string $extensionName name of the extension to retrieve the namespace for
470 * @param string $pluginName name of the plugin to retrieve the namespace for
471 * @return string plugin namespace
472 */
473 static public function getPluginNamespace($extensionName, $pluginName) {
474 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
475 $defaultPluginNamespace = 'tx_' . $pluginSignature;
476 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
477 $configurationManager = $objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
478 $frameworkConfiguration = $configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
479 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
480 return $defaultPluginNamespace;
481 }
482 return $frameworkConfiguration['view']['pluginNamespace'];
483 }
484
485 /**
486 * Iterates through the global TypoScript configuration and returns the name of the plugin
487 * that matches specified extensionName, controllerName and actionName.
488 * If no matching plugin was found, NULL is returned.
489 * If more than one plugin matches, an Exception will be thrown
490 *
491 * @param string $extensionName name of the target extension (UpperCamelCase)
492 * @param string $controllerName name of the target controller (UpperCamelCase)
493 * @param string $actionName name of the target action (lowerCamelCase)
494 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
495 */
496 static public function getPluginNameByAction($extensionName, $controllerName, $actionName) {
497 // TODO use ConfigurationManager to retrieve controllerConfiguration
498 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName])) {
499 return NULL;
500 }
501 $pluginNames = array();
502 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName] as $pluginName => $pluginConfiguration) {
503 if (!is_array($pluginConfiguration['controllers'])) {
504 continue;
505 }
506 foreach($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
507 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
508 continue;
509 }
510 if (in_array($actionName, $pluginControllerActions['actions'])) {
511 $pluginNames[] = $pluginName;
512 }
513 }
514 }
515 if (count($pluginNames) > 1) {
516 throw new Tx_Extbase_Exception('There is more than one plugin that can handle this request (Extension: "' . $extensionName . '", Controller: "' . $controllerName . '", action: "' . $actionName . '"). Please specify "pluginName" argument' , 1280825466);
517 }
518 return count($pluginNames) > 0 ? $pluginNames[0] : NULL;
519 }
520
521 /**
522 * Determines the target page of the specified plugin.
523 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
524 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
525 * If the page could not be determined, NULL is returned
526 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
527 *
528 * @param string $extensionName name of the extension to retrieve the target PID for
529 * @param string $pluginName name of the plugin to retrieve the target PID for
530 * @return integer uid of the target page or NULL if target page could not be determined
531 */
532 static public function getTargetPidByPlugin($extensionName, $pluginName) {
533 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
534 $configurationManager = $objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
535 $frameworkConfiguration = $configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
536 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
537 return NULL;
538 }
539 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
540 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
541 $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
542 'pid',
543 'tt_content',
544 'list_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($pluginSignature, 'tt_content') . $GLOBALS['TSFE']->sys_page->enableFields('tt_content'),
545 '',
546 '',
547 2
548 );
549 if (count($pages) > 1) {
550 throw new Tx_Extbase_Exception('There is more than one "' . $pluginSignature . '" plugin in the current page tree. Please remove one plugin or set the TypoScript configuration "plugin.tx_' . $pluginSignature . '.view.defaultPid" to a fixed page id' , 1280773643);
551 }
552 return count($pages) > 0 ? $pages[0]['pid'] : NULL;
553 }
554 return (integer)$frameworkConfiguration['view']['defaultPid'];
555 }
556 }
557
558 ?>