4837adfd4c76eefae3e1e718d797fa17a5be45f8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / ExtensionService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Service for determining basic extension params
32 */
33 class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface {
34
35 const PLUGIN_TYPE_PLUGIN = 'list_type';
36 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
40 */
41 protected $objectManager;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
45 */
46 protected $configurationManager;
47
48 /**
49 * Cache of result for getTargetPidByPlugin()
50 * @var array
51 */
52 protected $targetPidPluginCache = array();
53
54 /**
55 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
56 * @return void
57 */
58 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
59 $this->objectManager = $objectManager;
60 }
61
62 /**
63 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
64 * @return void
65 */
66 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
67 $this->configurationManager = $configurationManager;
68 }
69
70 /**
71 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
72 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
73 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
74 *
75 * @param string $extensionName name of the extension to retrieve the namespace for
76 * @param string $pluginName name of the plugin to retrieve the namespace for
77 * @return string plugin namespace
78 */
79 public function getPluginNamespace($extensionName, $pluginName) {
80 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
81 $defaultPluginNamespace = 'tx_' . $pluginSignature;
82 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
83 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
84 return $defaultPluginNamespace;
85 }
86 return $frameworkConfiguration['view']['pluginNamespace'];
87 }
88
89 /**
90 * Iterates through the global TypoScript configuration and returns the name of the plugin
91 * that matches specified extensionName, controllerName and actionName.
92 * If no matching plugin was found, NULL is returned.
93 * If more than one plugin matches and the current plugin is not configured to handle the action,
94 * an Exception will be thrown
95 *
96 * @param string $extensionName name of the target extension (UpperCamelCase)
97 * @param string $controllerName name of the target controller (UpperCamelCase)
98 * @param string $actionName name of the target action (lowerCamelCase)
99 * @throws \TYPO3\CMS\Extbase\Exception
100 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
101 */
102 public function getPluginNameByAction($extensionName, $controllerName, $actionName) {
103 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
104 // check, whether the current plugin is configured to handle the action
105 if ($extensionName === $frameworkConfiguration['extensionName']) {
106 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
107 return $frameworkConfiguration['pluginName'];
108 }
109 }
110 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'])) {
111 return NULL;
112 }
113 $pluginNames = array();
114 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] as $pluginName => $pluginConfiguration) {
115 if (!is_array($pluginConfiguration['controllers'])) {
116 continue;
117 }
118 foreach ($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
119 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
120 continue;
121 }
122 if (in_array($actionName, $pluginControllerActions['actions'])) {
123 $pluginNames[] = $pluginName;
124 }
125 }
126 }
127 if (count($pluginNames) > 1) {
128 throw new \TYPO3\CMS\Extbase\Exception('There is more than one plugin that can handle this request (Extension: "' . $extensionName . '", Controller: "' . $controllerName . '", action: "' . $actionName . '"). Please specify "pluginName" argument', 1280825466);
129 }
130 return count($pluginNames) > 0 ? $pluginNames[0] : NULL;
131 }
132
133 /**
134 * Checks if the given action is cacheable or not.
135 *
136 * @param string $extensionName Name of the target extension, without underscores
137 * @param string $pluginName Name of the target plugin
138 * @param string $controllerName Name of the target controller
139 * @param string $actionName Name of the action to be called
140 * @return boolean TRUE if the specified plugin action is cacheable, otherwise FALSE
141 */
142 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName) {
143 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
144 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions']) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
145 return FALSE;
146 }
147 return TRUE;
148 }
149
150 /**
151 * Determines the target page of the specified plugin.
152 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
153 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
154 * If the page could not be determined, NULL is returned
155 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
156 *
157 * @param string $extensionName name of the extension to retrieve the target PID for
158 * @param string $pluginName name of the plugin to retrieve the target PID for
159 * @throws \TYPO3\CMS\Extbase\Exception
160 * @return integer uid of the target page or NULL if target page could not be determined
161 */
162 public function getTargetPidByPlugin($extensionName, $pluginName) {
163 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
164 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
165 return NULL;
166 }
167 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
168 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
169 if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
170 $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('pid', 'tt_content', 'list_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($pluginSignature, 'tt_content') . ' AND CType="list"' . $GLOBALS['TSFE']->sys_page->enableFields('tt_content') . ' AND sys_language_uid=' . $GLOBALS['TSFE']->sys_language_uid, '', '', 2);
171 if (count($pages) > 1) {
172 throw new \TYPO3\CMS\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);
173 }
174 $this->targetPidPluginCache[$pluginSignature] = count($pages) > 0 ? $pages[0]['pid'] : NULL;
175 }
176 return $this->targetPidPluginCache[$pluginSignature];
177
178 }
179 return (integer) $frameworkConfiguration['view']['defaultPid'];
180 }
181
182 /**
183 * This returns the name of the first controller of the given plugin.
184 *
185 * @param string $extensionName name of the extension to retrieve the target PID for
186 * @param string $pluginName name of the plugin to retrieve the target PID for
187 * @return string|NULL
188 */
189 public function getDefaultControllerNameByPlugin($extensionName, $pluginName) {
190 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'])) {
191 return NULL;
192 }
193 $controllers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'];
194 return key($controllers);
195 }
196
197 /**
198 * This returns the name of the first action of the given plugin controller.
199 *
200 * @param string $extensionName name of the extension to retrieve the target PID for
201 * @param string $pluginName name of the plugin to retrieve the target PID for
202 * @param string $controllerName name of the controller to retrieve default action for
203 * @return string|NULL
204 */
205 public function getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerName) {
206 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'])) {
207 return NULL;
208 }
209 $actions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'];
210 return current($actions);
211 }
212
213 /**
214 * Resolve the page type number to use for building a link for a specific format
215 *
216 * @param string $extensionName name of the extension that has defined the target page type
217 * @param string $format The format for which to look up the page type
218 * @return integer Page type number for target page
219 */
220 public function getTargetPageTypeByFormat($extensionName, $format) {
221 $targetPageType = 0;
222 $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, $extensionName);
223 $formatToPageTypeMapping = isset($settings['view']['formatToPageTypeMapping']) ? $settings['view']['formatToPageTypeMapping'] : array();
224 if (is_array($formatToPageTypeMapping) && array_key_exists($format, $formatToPageTypeMapping)) {
225 $targetPageType = (integer) $formatToPageTypeMapping[$format];
226 }
227 return $targetPageType;
228 }
229
230 }
231
232 ?>