[BUGFIX] ConfigurationManager use FrontendSimulator only if neccessary
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / ExtensionService.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Extbase Team
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 process FlexForms
27 *
28 * @package Extbase
29 * @subpackage Service
30 */
31 class Tx_Extbase_Service_ExtensionService implements t3lib_Singleton {
32
33 const PLUGIN_TYPE_PLUGIN = 'list_type';
34 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
35
36 /**
37 * @var Tx_Extbase_Object_ObjectManagerInterface
38 */
39 protected $objectManager;
40
41 /**
42 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
43 */
44 protected $configurationManager;
45
46 /**
47 * @param Tx_Extbase_Object_ManagerInterface $objectManager
48 * @return void
49 */
50 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
51 $this->objectManager = $objectManager;
52 }
53
54 /**
55 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
56 * @return void
57 */
58 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
59 $this->configurationManager = $configurationManager;
60 }
61
62 /**
63 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
64 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
65 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
66 *
67 * @param string $extensionName name of the extension to retrieve the namespace for
68 * @param string $pluginName name of the plugin to retrieve the namespace for
69 * @return string plugin namespace
70 */
71 public function getPluginNamespace($extensionName, $pluginName) {
72 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
73 $defaultPluginNamespace = 'tx_' . $pluginSignature;
74 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
75 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
76 return $defaultPluginNamespace;
77 }
78 return $frameworkConfiguration['view']['pluginNamespace'];
79 }
80
81 /**
82 * Iterates through the global TypoScript configuration and returns the name of the plugin
83 * that matches specified extensionName, controllerName and actionName.
84 * If no matching plugin was found, NULL is returned.
85 * If more than one plugin matches and the current plugin is not configured to handle the action,
86 * an Exception will be thrown
87 *
88 * @param string $extensionName name of the target extension (UpperCamelCase)
89 * @param string $controllerName name of the target controller (UpperCamelCase)
90 * @param string $actionName name of the target action (lowerCamelCase)
91 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
92 */
93 public function getPluginNameByAction($extensionName, $controllerName, $actionName) {
94 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
95 // check, whether the current plugin is configured to handle the action
96 if ($extensionName === $frameworkConfiguration['extensionName']) {
97 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName])
98 && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
99 return $frameworkConfiguration['pluginName'];
100 }
101 }
102 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'])) {
103 return NULL;
104 }
105 $pluginNames = array();
106 foreach($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] as $pluginName => $pluginConfiguration) {
107 if (!is_array($pluginConfiguration['controllers'])) {
108 continue;
109 }
110 foreach($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
111 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
112 continue;
113 }
114 if (in_array($actionName, $pluginControllerActions['actions'])) {
115 $pluginNames[] = $pluginName;
116 }
117 }
118 }
119 if (count($pluginNames) > 1) {
120 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);
121 }
122 return count($pluginNames) > 0 ? $pluginNames[0] : NULL;
123 }
124
125 /**
126 * Checks if the given action is cacheable or not.
127 *
128 * @param string $extensionName Name of the target extension, without underscores
129 * @param string $pluginName Name of the target plugin
130 * @param string $controllerName Name of the target controller
131 * @param string $actionName Name of the action to be called
132 * @return boolean TRUE if the specified plugin action is cacheable, otherwise FALSE
133 */
134 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName) {
135 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
136
137 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName])
138 && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName])
139 && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])
140 && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
141 return FALSE;
142 }
143 return TRUE;
144 }
145
146 /**
147 * Determines the target page of the specified plugin.
148 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
149 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
150 * If the page could not be determined, NULL is returned
151 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
152 *
153 * @param string $extensionName name of the extension to retrieve the target PID for
154 * @param string $pluginName name of the plugin to retrieve the target PID for
155 * @return integer uid of the target page or NULL if target page could not be determined
156 */
157 public function getTargetPidByPlugin($extensionName, $pluginName) {
158 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
159 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
160 return NULL;
161 }
162 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
163 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
164 $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
165 'pid',
166 'tt_content',
167 'list_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($pluginSignature, 'tt_content') . ' AND CType="list"' . $GLOBALS['TSFE']->sys_page->enableFields('tt_content'),
168 '',
169 '',
170 2
171 );
172 if (count($pages) > 1) {
173 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);
174 }
175 return count($pages) > 0 ? $pages[0]['pid'] : NULL;
176 }
177 return (integer)$frameworkConfiguration['view']['defaultPid'];
178 }
179
180 }
181 ?>