[CLEANUP] Adjust code to coding guidelines
[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) 2011 Extbase Team
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Utilities to process FlexForms
28 */
29 class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface {
30
31 const PLUGIN_TYPE_PLUGIN = 'list_type';
32 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
41 */
42 protected $configurationManager;
43
44 /**
45 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
46 * @return void
47 */
48 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
49 $this->objectManager = $objectManager;
50 }
51
52 /**
53 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
54 * @return void
55 */
56 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
57 $this->configurationManager = $configurationManager;
58 }
59
60 /**
61 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
62 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
63 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
64 *
65 * @param string $extensionName name of the extension to retrieve the namespace for
66 * @param string $pluginName name of the plugin to retrieve the namespace for
67 * @return string plugin namespace
68 */
69 public function getPluginNamespace($extensionName, $pluginName) {
70 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
71 $defaultPluginNamespace = 'tx_' . $pluginSignature;
72 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
73 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
74 return $defaultPluginNamespace;
75 }
76 return $frameworkConfiguration['view']['pluginNamespace'];
77 }
78
79 /**
80 * Iterates through the global TypoScript configuration and returns the name of the plugin
81 * that matches specified extensionName, controllerName and actionName.
82 * If no matching plugin was found, NULL is returned.
83 * If more than one plugin matches and the current plugin is not configured to handle the action,
84 * an Exception will be thrown
85 *
86 * @param string $extensionName name of the target extension (UpperCamelCase)
87 * @param string $controllerName name of the target controller (UpperCamelCase)
88 * @param string $actionName name of the target action (lowerCamelCase)
89 * @throws \TYPO3\CMS\Extbase\Exception
90 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
91 */
92 public function getPluginNameByAction($extensionName, $controllerName, $actionName) {
93 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
94 // check, whether the current plugin is configured to handle the action
95 if ($extensionName === $frameworkConfiguration['extensionName']) {
96 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
97 return $frameworkConfiguration['pluginName'];
98 }
99 }
100 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'])) {
101 return NULL;
102 }
103 $pluginNames = array();
104 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] as $pluginName => $pluginConfiguration) {
105 if (!is_array($pluginConfiguration['controllers'])) {
106 continue;
107 }
108 foreach ($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
109 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
110 continue;
111 }
112 if (in_array($actionName, $pluginControllerActions['actions'])) {
113 $pluginNames[] = $pluginName;
114 }
115 }
116 }
117 if (count($pluginNames) > 1) {
118 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);
119 }
120 return count($pluginNames) > 0 ? $pluginNames[0] : NULL;
121 }
122
123 /**
124 * Checks if the given action is cacheable or not.
125 *
126 * @param string $extensionName Name of the target extension, without underscores
127 * @param string $pluginName Name of the target plugin
128 * @param string $controllerName Name of the target controller
129 * @param string $actionName Name of the action to be called
130 * @return boolean TRUE if the specified plugin action is cacheable, otherwise FALSE
131 */
132 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName) {
133 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
134 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions']) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
135 return FALSE;
136 }
137 return TRUE;
138 }
139
140 /**
141 * Determines the target page of the specified plugin.
142 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
143 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
144 * If the page could not be determined, NULL is returned
145 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
146 *
147 * @param string $extensionName name of the extension to retrieve the target PID for
148 * @param string $pluginName name of the plugin to retrieve the target PID for
149 * @throws \TYPO3\CMS\Extbase\Exception
150 * @return integer uid of the target page or NULL if target page could not be determined
151 */
152 public function getTargetPidByPlugin($extensionName, $pluginName) {
153 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
154 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
155 return NULL;
156 }
157 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
158 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
159 $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);
160 if (count($pages) > 1) {
161 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);
162 }
163 return count($pages) > 0 ? $pages[0]['pid'] : NULL;
164 }
165 return (integer) $frameworkConfiguration['view']['defaultPid'];
166 }
167
168 /**
169 * This returns the name of the first controller of the given plugin.
170 *
171 * @param string $extensionName name of the extension to retrieve the target PID for
172 * @param string $pluginName name of the plugin to retrieve the target PID for
173 * @return string|NULL
174 */
175 public function getDefaultControllerNameByPlugin($extensionName, $pluginName) {
176 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'])) {
177 return NULL;
178 }
179 $controllers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'];
180 return key($controllers);
181 }
182
183 /**
184 * This returns the name of the first action of the given plugin controller.
185 *
186 * @param string $extensionName name of the extension to retrieve the target PID for
187 * @param string $pluginName name of the plugin to retrieve the target PID for
188 * @param string $controllerName name of the controller to retrieve default action for
189 * @return string|NULL
190 */
191 public function getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerName) {
192 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'])) {
193 return NULL;
194 }
195 $actions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'];
196 return current($actions);
197 }
198 }
199
200 ?>