[TASK] Use name-resolution instead of strings where possible: 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / BackendConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
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 * A general purpose configuration manager used in backend mode.
18 */
19 class BackendConfigurationManager extends \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager {
20
21 /**
22 * Needed to recursively fetch a page tree
23 *
24 * @var \TYPO3\CMS\Core\Database\QueryGenerator
25 * @inject
26 */
27 protected $queryGenerator;
28
29 /**
30 * @var array
31 */
32 protected $typoScriptSetupCache = array();
33
34 /**
35 * stores the current page ID
36 * @var int
37 */
38 protected $currentPageId;
39
40 /**
41 * Returns TypoScript Setup array from current Environment.
42 *
43 * @return array the raw TypoScript setup
44 */
45 public function getTypoScriptSetup() {
46 $pageId = $this->getCurrentPageId();
47
48 if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
49 /** @var $template \TYPO3\CMS\Core\TypoScript\TemplateService */
50 $template = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\TemplateService::class);
51 // do not log time-performance information
52 $template->tt_track = 0;
53 // Explicitly trigger processing of extension static files
54 $template->setProcessExtensionStatics(TRUE);
55 $template->init();
56 // Get the root line
57 $rootline = array();
58 if ($pageId > 0) {
59 /** @var $sysPage \TYPO3\CMS\Frontend\Page\PageRepository */
60 $sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
61 // Get the rootline for the current page
62 $rootline = $sysPage->getRootLine($pageId, '', TRUE);
63 }
64 // This generates the constants/config + hierarchy info for the template.
65 $template->runThroughTemplates($rootline, 0);
66 $template->generateConfig();
67 $this->typoScriptSetupCache[$pageId] = $template->setup;
68 }
69 return $this->typoScriptSetupCache[$pageId];
70 }
71
72 /**
73 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
74 * merged with the global configuration of your extension from module.tx_yourextension
75 *
76 * @param string $extensionName
77 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
78 * @return array
79 */
80 protected function getPluginConfiguration($extensionName, $pluginName = NULL) {
81 $setup = $this->getTypoScriptSetup();
82 $pluginConfiguration = array();
83 if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'])) {
84 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
85 }
86 if ($pluginName !== NULL) {
87 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
88 if (is_array($setup['module.']['tx_' . $pluginSignature . '.'])) {
89 $overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
90 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
91 }
92 }
93 return $pluginConfiguration;
94 }
95
96 /**
97 * Returns the configured controller/action pairs of the specified module in the format
98 * array(
99 * 'Controller1' => array('action1', 'action2'),
100 * 'Controller2' => array('action3', 'action4')
101 * )
102 *
103 * @param string $extensionName
104 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
105 * @return array
106 */
107 protected function getSwitchableControllerActions($extensionName, $pluginName) {
108 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'];
109 if (!is_array($switchableControllerActions)) {
110 $switchableControllerActions = array();
111 }
112 return $switchableControllerActions;
113 }
114
115 /**
116 * Returns the page uid of the current page.
117 * If no page is selected, we'll return the uid of the first root page.
118 *
119 * @return int current page id. If no page is selected current root page id is returned
120 */
121 protected function getCurrentPageId() {
122 if ($this->currentPageId !== NULL) {
123 return $this->currentPageId;
124 }
125
126 $this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
127 $this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
128 $this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;
129
130 return $this->currentPageId;
131 }
132
133 /**
134 * Gets the current page ID from the GET/POST data.
135 *
136 * @return int the page UID, will be 0 if none has been set
137 */
138 protected function getCurrentPageIdFromGetPostData() {
139 return (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
140 }
141
142 /**
143 * Gets the current page ID from the first site root in tree.
144 *
145 * @return int the page UID, will be 0 if none has been set
146 */
147 protected function getCurrentPageIdFromCurrentSiteRoot() {
148 $rootPage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
149 'uid', 'pages', 'deleted=0 AND hidden=0 AND is_siteroot=1', '', 'sorting'
150 );
151 if (empty($rootPage)) {
152 return 0;
153 }
154
155 return (int)$rootPage['uid'];
156 }
157
158 /**
159 * Gets the current page ID from the first created root template.
160 *
161 * @return int the page UID, will be 0 if none has been set
162 */
163 protected function getCurrentPageIdFromRootTemplate() {
164 $rootTemplate = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
165 'pid', 'sys_template', 'deleted=0 AND hidden=0 AND root=1', '', 'crdate'
166 );
167 if (empty($rootTemplate)) {
168 return 0;
169 }
170
171 return (int)$rootTemplate['pid'];
172 }
173
174 /**
175 * Returns the default backend storage pid
176 *
177 * @return string
178 */
179 public function getDefaultBackendStoragePid() {
180 return $this->getCurrentPageId();
181 }
182
183 /**
184 * We need to set some default request handler if the framework configuration
185 * could not be loaded; to make sure Extbase also works in Backend modules
186 * in all contexts.
187 *
188 * @param array $frameworkConfiguration
189 * @return array
190 */
191 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
192 if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
193 $frameworkConfiguration['mvc']['requestHandlers'] = array(
194 \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::class,
195 \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class => \TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler::class
196 );
197 }
198 return $frameworkConfiguration;
199 }
200
201
202 /**
203 * Returns a comma separated list of storagePid that are below a certain storage pid.
204 *
205 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
206 * @param int $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
207 * @return string storage PIDs
208 */
209 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0) {
210 if ($recursionDepth <= 0) {
211 return $storagePid;
212 }
213
214 $recursiveStoragePids = '';
215 $storagePids = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $storagePid);
216 foreach ($storagePids as $startPid) {
217 $pids = $this->queryGenerator->getTreeList($startPid, $recursionDepth, 0, 1);
218 if (strlen($pids) > 0) {
219 $recursiveStoragePids .= $pids . ',';
220 }
221 }
222
223 return rtrim($recursiveStoragePids, ',');
224 }
225
226 }