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