[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / ExtensionService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Service for determining basic extension params
23 */
24 class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface
25 {
26 const PLUGIN_TYPE_PLUGIN = 'list_type';
27 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
31 */
32 protected $objectManager;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
36 */
37 protected $configurationManager;
38
39 /**
40 * Cache of result for getTargetPidByPlugin()
41 * @var array
42 */
43 protected $targetPidPluginCache = [];
44
45 /**
46 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
47 */
48 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
49 {
50 $this->objectManager = $objectManager;
51 }
52
53 /**
54 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
55 */
56 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
57 {
58 $this->configurationManager = $configurationManager;
59 }
60
61 /**
62 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
63 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
64 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
65 *
66 * @param string $extensionName name of the extension to retrieve the namespace for
67 * @param string $pluginName name of the plugin to retrieve the namespace for
68 * @return string plugin namespace
69 */
70 public function getPluginNamespace($extensionName, $pluginName)
71 {
72 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
73 $defaultPluginNamespace = 'tx_' . $pluginSignature;
74 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\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 * @throws \TYPO3\CMS\Extbase\Exception
92 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
93 */
94 public function getPluginNameByAction($extensionName, $controllerName, $actionName)
95 {
96 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
97 // check, whether the current plugin is configured to handle the action
98 if ($extensionName === $frameworkConfiguration['extensionName']) {
99 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
100 return $frameworkConfiguration['pluginName'];
101 }
102 }
103 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'])) {
104 return null;
105 }
106 $pluginNames = [];
107 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] as $pluginName => $pluginConfiguration) {
108 if (!is_array($pluginConfiguration['controllers'])) {
109 continue;
110 }
111 foreach ($pluginConfiguration['controllers'] as $pluginControllerName => $pluginControllerActions) {
112 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
113 continue;
114 }
115 if (in_array($actionName, $pluginControllerActions['actions'])) {
116 $pluginNames[] = $pluginName;
117 }
118 }
119 }
120 if (count($pluginNames) > 1) {
121 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);
122 }
123 return !empty($pluginNames) ? $pluginNames[0] : null;
124 }
125
126 /**
127 * Checks if the given action is cacheable or not.
128 *
129 * @param string $extensionName Name of the target extension, without underscores
130 * @param string $pluginName Name of the target plugin
131 * @param string $controllerName Name of the target controller
132 * @param string $actionName Name of the action to be called
133 * @return bool TRUE if the specified plugin action is cacheable, otherwise FALSE
134 */
135 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName)
136 {
137 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
138 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions']) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
139 return false;
140 }
141 return true;
142 }
143
144 /**
145 * Determines the target page of the specified plugin.
146 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
147 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
148 * If the page could not be determined, NULL is returned
149 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
150 *
151 * @param string $extensionName name of the extension to retrieve the target PID for
152 * @param string $pluginName name of the plugin to retrieve the target PID for
153 * @throws \TYPO3\CMS\Extbase\Exception
154 * @return int uid of the target page or NULL if target page could not be determined
155 */
156 public function getTargetPidByPlugin($extensionName, $pluginName)
157 {
158 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\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 if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
165 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
166 ->getQueryBuilderForTable('tt_content');
167 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
168
169 $pages = $queryBuilder
170 ->select('pid')
171 ->from('tt_content')
172 ->where(
173 $queryBuilder->expr()->eq(
174 'list_type',
175 $queryBuilder->createNamedParameter($pluginSignature, \PDO::PARAM_STR)
176 ),
177 $queryBuilder->expr()->eq(
178 'CType',
179 $queryBuilder->createNamedParameter('list', \PDO::PARAM_STR)
180 ),
181 $queryBuilder->expr()->eq(
182 'sys_language_uid',
183 $queryBuilder->createNamedParameter($GLOBALS['TSFE']->sys_language_uid, \PDO::PARAM_INT)
184 )
185 )
186 ->setMaxResults(2)
187 ->execute()
188 ->fetchAll();
189
190 if (count($pages) > 1) {
191 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);
192 }
193 $this->targetPidPluginCache[$pluginSignature] = !empty($pages) ? $pages[0]['pid'] : null;
194 }
195 return $this->targetPidPluginCache[$pluginSignature];
196 }
197 return (int)$frameworkConfiguration['view']['defaultPid'];
198 }
199
200 /**
201 * This returns the name of the first controller of the given plugin.
202 *
203 * @param string $extensionName name of the extension to retrieve the target PID for
204 * @param string $pluginName name of the plugin to retrieve the target PID for
205 * @return string|null
206 */
207 public function getDefaultControllerNameByPlugin($extensionName, $pluginName)
208 {
209 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'])) {
210 return null;
211 }
212 $controllers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'];
213 return key($controllers);
214 }
215
216 /**
217 * This returns the name of the first action of the given plugin controller.
218 *
219 * @param string $extensionName name of the extension to retrieve the target PID for
220 * @param string $pluginName name of the plugin to retrieve the target PID for
221 * @param string $controllerName name of the controller to retrieve default action for
222 * @return string|null
223 */
224 public function getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerName)
225 {
226 if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'])) {
227 return null;
228 }
229 $actions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'];
230 return current($actions);
231 }
232
233 /**
234 * Resolve the page type number to use for building a link for a specific format
235 *
236 * @param string $extensionName name of the extension that has defined the target page type
237 * @param string $format The format for which to look up the page type
238 * @return int Page type number for target page
239 */
240 public function getTargetPageTypeByFormat($extensionName, $format)
241 {
242 $targetPageType = 0;
243 $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, $extensionName);
244 $formatToPageTypeMapping = isset($settings['view']['formatToPageTypeMapping']) ? $settings['view']['formatToPageTypeMapping'] : [];
245 if (is_array($formatToPageTypeMapping) && array_key_exists($format, $formatToPageTypeMapping)) {
246 $targetPageType = (int)$formatToPageTypeMapping[$format];
247 }
248 return $targetPageType;
249 }
250 }