[CLEANUP] Replace wrong/old file copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / AbstractConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2012 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 * Abstract base class for a general purpose configuration manager
32 */
33 abstract class AbstractConfigurationManager implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * Default backend storage PID
37 */
38 const DEFAULT_BACKEND_STORAGE_PID = 0;
39
40 /**
41 * Storage of the raw TypoScript configuration
42 *
43 * @var array
44 */
45 protected $configuration = array();
46
47 /**
48 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
49 */
50 protected $contentObject;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
54 */
55 protected $objectManager;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Service\TypoScriptService
59 */
60 protected $typoScriptService;
61
62 /**
63 * name of the extension this Configuration Manager instance belongs to
64 *
65 * @var string
66 */
67 protected $extensionName;
68
69 /**
70 * name of the plugin this Configuration Manager instance belongs to
71 *
72 * @var string
73 */
74 protected $pluginName;
75
76 /**
77 * 1st level configuration cache
78 *
79 * @var array
80 */
81 protected $configurationCache = array();
82
83 /**
84 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
85 * @return void
86 */
87 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
88 $this->objectManager = $objectManager;
89 }
90
91 /**
92 * @param \TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService
93 * @return void
94 */
95 public function injectTypoScriptService(\TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService) {
96 $this->typoScriptService = $typoScriptService;
97 }
98
99 /**
100 * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject
101 * @return void
102 */
103 public function setContentObject(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject = NULL) {
104 $this->contentObject = $contentObject;
105 }
106
107 /**
108 * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer|NULL
109 */
110 public function getContentObject() {
111 if ($this->contentObject !== NULL) {
112 return $this->contentObject;
113 }
114 return NULL;
115 }
116
117 /**
118 * Sets the specified raw configuration coming from the outside.
119 * Note that this is a low level method and only makes sense to be used by Extbase internally.
120 *
121 * @param array $configuration The new configuration
122 * @return void
123 */
124 public function setConfiguration(array $configuration = array()) {
125 // reset 1st level cache
126 $this->configurationCache = array();
127 $this->extensionName = isset($configuration['extensionName']) ? $configuration['extensionName'] : NULL;
128 $this->pluginName = isset($configuration['pluginName']) ? $configuration['pluginName'] : NULL;
129 $this->configuration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($configuration);
130 }
131
132 /**
133 * Loads the Extbase Framework configuration.
134 *
135 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
136 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
137 *
138 * @param string $extensionName if specified, the configuration for the given extension will be returned (plugin.tx_extensionname)
139 * @param string $pluginName if specified, the configuration for the given plugin will be returned (plugin.tx_extensionname_pluginname)
140 * @return array the Extbase framework configuration
141 */
142 public function getConfiguration($extensionName = NULL, $pluginName = NULL) {
143 // 1st level cache
144 $configurationCacheKey = strtolower(($extensionName ?: $this->extensionName) . '_' . ($pluginName ?: $this->pluginName));
145 if (isset($this->configurationCache[$configurationCacheKey])) {
146 return $this->configurationCache[$configurationCacheKey];
147 }
148 $frameworkConfiguration = $this->getExtbaseConfiguration();
149 if (!isset($frameworkConfiguration['persistence']['storagePid'])) {
150 $frameworkConfiguration['persistence']['storagePid'] = $this->getDefaultBackendStoragePid();
151 }
152 // only merge $this->configuration and override switchableControllerActions when retrieving configuration of the current plugin
153 if ($extensionName === NULL || $extensionName === $this->extensionName && $pluginName === $this->pluginName) {
154 $pluginConfiguration = $this->getPluginConfiguration($this->extensionName, $this->pluginName);
155 $pluginConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($pluginConfiguration, $this->configuration);
156 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($this->extensionName, $this->pluginName);
157 if (isset($this->configuration['switchableControllerActions'])) {
158 $this->overrideSwitchableControllerActions($pluginConfiguration, $this->configuration['switchableControllerActions']);
159 }
160 } else {
161 $pluginConfiguration = $this->getPluginConfiguration($extensionName, $pluginName);
162 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($extensionName, $pluginName);
163 }
164 $frameworkConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($frameworkConfiguration, $pluginConfiguration);
165 // only load context specific configuration when retrieving configuration of the current plugin
166 if ($extensionName === NULL || $extensionName === $this->extensionName && $pluginName === $this->pluginName) {
167 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
168 }
169
170 if (!empty($frameworkConfiguration['persistence']['storagePid'])) {
171 if (is_array($frameworkConfiguration['persistence']['storagePid'])) {
172 /**
173 * We simulate the frontend to enable the use of cObjects in
174 * stdWrap. Than we convert the configuration to normal TypoScript
175 * and apply the stdWrap to the storagePid
176 */
177 if (TYPO3_MODE !== 'FE') {
178 Tx_Extbase_Utility_FrontendSimulator::simulateFrontendEnvironment($this->getContentObject());
179 }
180 $conf = $this->typoScriptService->convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']);
181 $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrap($conf['storagePid'], $conf['storagePid.']);
182 if (TYPO3_MODE !== 'FE') {
183 Tx_Extbase_Utility_FrontendSimulator::resetFrontendEnvironment();
184 }
185 }
186
187 if (!empty($frameworkConfiguration['persistence']['recursive'])) {
188 $frameworkConfiguration['persistence']['storagePid'] = $this->getRecursiveStoragePids($frameworkConfiguration['persistence']['storagePid'], (int) $frameworkConfiguration['persistence']['recursive']);
189 }
190 }
191 // 1st level cache
192 $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
193 return $frameworkConfiguration;
194 }
195
196 /**
197 * Returns the TypoScript configuration found in config.tx_extbase
198 *
199 * @return array
200 */
201 protected function getExtbaseConfiguration() {
202 $setup = $this->getTypoScriptSetup();
203 $extbaseConfiguration = array();
204 if (isset($setup['config.']['tx_extbase.'])) {
205 $extbaseConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['config.']['tx_extbase.']);
206 }
207 return $extbaseConfiguration;
208 }
209
210 /**
211 * Returns the default backend storage pid
212 *
213 * @return string
214 */
215 public function getDefaultBackendStoragePid() {
216 return self::DEFAULT_BACKEND_STORAGE_PID;
217 }
218
219 /**
220 * @param array &$frameworkConfiguration
221 * @param array $switchableControllerActions
222 * @return void
223 */
224 protected function overrideSwitchableControllerActions(array &$frameworkConfiguration, array $switchableControllerActions) {
225 $overriddenSwitchableControllerActions = array();
226 foreach ($switchableControllerActions as $controllerName => $actions) {
227 if (!isset($frameworkConfiguration['controllerConfiguration'][$controllerName])) {
228 continue;
229 }
230 $overriddenSwitchableControllerActions[$controllerName] = array('actions' => $actions);
231 $nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'];
232 if (!is_array($nonCacheableActions)) {
233 // There are no non-cacheable actions, thus we can directly continue
234 // with the next controller name.
235 continue;
236 }
237 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
238 if (!empty($overriddenNonCacheableActions)) {
239 $overriddenSwitchableControllerActions[$controllerName]['nonCacheableActions'] = $overriddenNonCacheableActions;
240 }
241 }
242 $frameworkConfiguration['controllerConfiguration'] = $overriddenSwitchableControllerActions;
243 }
244
245 /**
246 * The context specific configuration returned by this method
247 * will override the framework configuration which was
248 * obtained from TypoScript. This can be used f.e. to override the storagePid
249 * with the value set inside the Plugin Instance.
250 *
251 * WARNING: Make sure this method ALWAYS returns an array!
252 *
253 * @param array $frameworkConfiguration The framework configuration until now
254 * @return array context specific configuration which will override the configuration obtained by TypoScript
255 */
256 abstract protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration);
257
258 /**
259 * Returns TypoScript Setup array from current Environment.
260 *
261 * @return array the TypoScript setup
262 */
263 abstract public function getTypoScriptSetup();
264
265 /**
266 * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin / module.tx_yourextension_yourmodule
267 * merged with the global configuration of your extension from plugin.tx_yourextension / module.tx_yourextension
268 *
269 * @param string $extensionName
270 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
271 * @return array
272 */
273 abstract protected function getPluginConfiguration($extensionName, $pluginName = NULL);
274
275 /**
276 * Returns the configured controller/action pairs of the specified plugin/module in the format
277 * array(
278 * 'Controller1' => array('action1', 'action2'),
279 * 'Controller2' => array('action3', 'action4')
280 * )
281 *
282 * @param string $extensionName
283 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
284 * @return array
285 */
286 abstract protected function getSwitchableControllerActions($extensionName, $pluginName);
287
288 /**
289 * The implementation of the methods to return a list of storagePid that are below a certain
290 * storage pid.
291 *
292 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
293 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
294 * @return string storage PIDs
295 */
296 abstract protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0);
297
298 }
299
300 ?>