[TASK] Replace inject methods with @inject
[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 * @inject
41 */
42 protected $objectManager;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
46 * @inject
47 */
48 protected $configurationManager;
49
50 /**
51 * Cache of result for getTargetPidByPlugin()
52 * @var array
53 */
54 protected $targetPidPluginCache = array();
55
56 /**
57 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
58 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
59 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
60 *
61 * @param string $extensionName name of the extension to retrieve the namespace for
62 * @param string $pluginName name of the plugin to retrieve the namespace for
63 * @return string plugin namespace
64 */
65 public function getPluginNamespace($extensionName, $pluginName) {
66 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
67 $defaultPluginNamespace = 'tx_' . $pluginSignature;
68 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
69 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
70 return $defaultPluginNamespace;
71 }
72 return $frameworkConfiguration['view']['pluginNamespace'];
73 }
74
75 /**
76 * Iterates through the global TypoScript configuration and returns the name of the plugin
77 * that matches specified extensionName, controllerName and actionName.
78 * If no matching plugin was found, NULL is returned.
79 * If more than one plugin matches and the current plugin is not configured to handle the action,
80 * an Exception will be thrown
81 *
82 * @param string $extensionName name of the target extension (UpperCamelCase)
83 * @param string $controllerName name of the target controller (UpperCamelCase)
84 * @param string $actionName name of the target action (lowerCamelCase)
85 * @throws \TYPO3\CMS\Extbase\Exception
86 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
87 */
88 public function getPluginNameByAction($extensionName, $controllerName, $actionName) {
89 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
90 // check, whether the current plugin is configured to handle the action
91 if ($extensionName === $frameworkConfiguration['extensionName']) {
92 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
93 return $frameworkConfiguration['pluginName'];
94 }
95 }
96 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'])) {
97 return NULL;
98 }
99 $pluginNames = array();
100 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] as $pluginName => $pluginConfiguration) {
101 if (!is_array($pluginConfiguration['controllers'])) {
102 continue;
103 }
104 foreach ($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
105 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
106 continue;
107 }
108 if (in_array($actionName, $pluginControllerActions['actions'])) {
109 $pluginNames[] = $pluginName;
110 }
111 }
112 }
113 if (count($pluginNames) > 1) {
114 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);
115 }
116 return count($pluginNames) > 0 ? $pluginNames[0] : NULL;
117 }
118
119 /**
120 * Checks if the given action is cacheable or not.
121 *
122 * @param string $extensionName Name of the target extension, without underscores
123 * @param string $pluginName Name of the target plugin
124 * @param string $controllerName Name of the target controller
125 * @param string $actionName Name of the action to be called
126 * @return boolean TRUE if the specified plugin action is cacheable, otherwise FALSE
127 */
128 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName) {
129 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
130 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions']) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
131 return FALSE;
132 }
133 return TRUE;
134 }
135
136 /**
137 * Determines the target page of the specified plugin.
138 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
139 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
140 * If the page could not be determined, NULL is returned
141 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
142 *
143 * @param string $extensionName name of the extension to retrieve the target PID for
144 * @param string $pluginName name of the plugin to retrieve the target PID for
145 * @throws \TYPO3\CMS\Extbase\Exception
146 * @return integer uid of the target page or NULL if target page could not be determined
147 */
148 public function getTargetPidByPlugin($extensionName, $pluginName) {
149 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
150 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
151 return NULL;
152 }
153 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
154 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
155 if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
156 $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);
157 if (count($pages) > 1) {
158 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);
159 }
160 $this->targetPidPluginCache[$pluginSignature] = count($pages) > 0 ? $pages[0]['pid'] : NULL;
161 }
162 return $this->targetPidPluginCache[$pluginSignature];
163
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 * Resolve the page type number to use for building a link for a specific format
201 *
202 * @param string $extensionName name of the extension that has defined the target page type
203 * @param string $format The format for which to look up the page type
204 * @return integer Page type number for target page
205 */
206 public function getTargetPageTypeByFormat($extensionName, $format) {
207 $targetPageType = 0;
208 $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, $extensionName);
209 $formatToPageTypeMapping = isset($settings['view']['formatToPageTypeMapping']) ? $settings['view']['formatToPageTypeMapping'] : array();
210 if (is_array($formatToPageTypeMapping) && array_key_exists($format, $formatToPageTypeMapping)) {
211 $targetPageType = (integer) $formatToPageTypeMapping[$format];
212 }
213 return $targetPageType;
214 }
215
216 }
217
218 ?>