[+BUGFIX] Extbase (Configuration): Slightly tweaked AbstractConfigurationManager
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / AbstractConfigurationManager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Abstract base class for a general purpose configuration manager
27 *
28 * @package Extbase
29 * @subpackage Configuration
30 * @version $ID:$
31 */
32 abstract class Tx_Extbase_Configuration_AbstractConfigurationManager implements t3lib_Singleton {
33
34 /**
35 * Default backend storage PID
36 */
37 const DEFAULT_BACKEND_STORAGE_PID = 0;
38
39 /**
40 * Storage of the raw TypoScript configuration
41 * @var array
42 */
43 protected $configuration = array();
44
45 /**
46 * @var tslib_cObj
47 */
48 protected $contentObject;
49
50 /**
51 * @var Tx_Extbase_Object_ObjectManagerInterface
52 */
53 protected $objectManager;
54
55 /**
56 * name of the extension this Configuration Manager instance belongs to
57 * @var string
58 */
59 protected $extensionName;
60
61 /**
62 * name of the plugin this Configuration Manager instance belongs to
63 * @var string
64 */
65 protected $pluginName;
66
67 /**
68 * 1st level configuration cache
69 *
70 * @var array
71 */
72 protected $configurationCache = array();
73
74 /**
75 * @param Tx_Extbase_Object_ManagerInterface $objectManager
76 * @return void
77 */
78 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
79 $this->objectManager = $objectManager;
80 }
81
82 /**
83 * @param tslib_cObj $contentObject
84 * @return void
85 */
86 public function setContentObject(tslib_cObj $contentObject = NULL) {
87 $this->contentObject = $contentObject;
88 }
89
90 /**
91 * @return tslib_cObj
92 */
93 public function getContentObject() {
94 return $this->contentObject;
95 }
96
97 /**
98 * Sets the specified raw configuration coming from the outside.
99 * Note that this is a low level method and only makes sense to be used by Extbase internally.
100 *
101 * @param array $configuration The new configuration
102 * @return void
103 */
104 public function setConfiguration(array $configuration = array()) {
105 // reset 1st level cache
106 $this->configurationCache = array();
107
108 $this->extensionName = $configuration['extensionName'];
109 $this->pluginName = $configuration['pluginName'];
110 $this->configuration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($configuration);
111 }
112
113 /**
114 * Loads the Extbase Framework configuration.
115 *
116 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
117 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
118 *
119 * @param string $extensionName if specified, the configuration for the given extension will be returned (plugin.tx_extensionname)
120 * @param string $pluginName if specified, the configuration for the given plugin will be returned (plugin.tx_extensionname_pluginname)
121 * @return array the Extbase framework configuration
122 */
123 public function getConfiguration($extensionName = NULL, $pluginName = NULL) {
124 // 1st level cache
125 if ($extensionName !== NULL) {
126 if ($pluginName === NULL) {
127 throw new Tx_Extbase_Configuration_Exception('You\'ll have to specify either both, extensionName and pluginName, or neither.', 1289852422);
128 }
129 $configurationCacheKey = strtolower($extensionName . '_' . $pluginName);
130 } else {
131 $configurationCacheKey = strtolower($this->extensionName . '_' . $this->pluginName);
132 }
133 if (isset($this->configurationCache[$configurationCacheKey])) {
134 return $this->configurationCache[$configurationCacheKey];
135 }
136
137 $frameworkConfiguration = $this->getExtbaseConfiguration();
138 if (!isset($frameworkConfiguration['persistence']['storagePid'])) {
139 $frameworkConfiguration['persistence']['storagePid'] = self::DEFAULT_BACKEND_STORAGE_PID;
140 }
141
142 // only merge $this->configuration and override switchableControllerActions when retrieving configuration of the current plugin
143 if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
144 $pluginConfiguration = $this->getPluginConfiguration($this->extensionName, $this->pluginName);
145 $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, $this->configuration);
146 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($this->extensionName, $this->pluginName);
147 if (isset($this->configuration['switchableControllerActions'])) {
148 $this->overrideSwitchableControllerActions($pluginConfiguration, $this->configuration['switchableControllerActions']);
149 }
150 } else {
151 $pluginConfiguration = $this->getPluginConfiguration($extensionName, $pluginName);
152 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($extensionName, $pluginName);
153 }
154 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $pluginConfiguration);
155
156 // only load context specific configuration when retrieving configuration of the current plugin
157 if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
158 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
159 }
160
161 // 1st level cache
162 $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
163 return $frameworkConfiguration;
164 }
165
166 /**
167 * Returns the TypoScript configuration found in config.tx_extbase
168 *
169 * @return array
170 */
171 protected function getExtbaseConfiguration() {
172 $setup = $this->getTypoScriptSetup();
173 $extbaseConfiguration = array();
174 if (isset($setup['config.']['tx_extbase.'])) {
175 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['config.']['tx_extbase.']);
176 }
177 return $extbaseConfiguration;
178 }
179
180 /**
181 * @param array $frameworkConfiguration
182 * @param array $overriddenSwitchableControllerActions in the format array('Controller1' => array('action1', 'action2'), 'Controller2' => ...)
183 * @return void
184 */
185 protected function overrideSwitchableControllerActions(array &$frameworkConfiguration, array $switchableControllerActions) {
186 $overriddenSwitchableControllerActions = array();
187 foreach ($switchableControllerActions as $controllerName => $actions) {
188 if (!isset($frameworkConfiguration['controllerConfiguration'][$controllerName])) {
189 continue;
190 }
191 $overriddenSwitchableControllerActions[$controllerName] = array('actions' => $actions);
192 $nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'];
193 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
194 if (!empty($overriddenNonCacheableActions)) {
195 $overriddenSwitchableControllerActions[$controllerName]['nonCacheableActions'] = $overriddenNonCacheableActions;
196 }
197 }
198 $frameworkConfiguration['controllerConfiguration'] = $overriddenSwitchableControllerActions;
199 }
200
201 /**
202 * The context specific configuration returned by this method
203 * will override the framework configuration which was
204 * obtained from TypoScript. This can be used f.e. to override the storagePid
205 * with the value set inside the Plugin Instance.
206 *
207 * WARNING: Make sure this method ALWAYS returns an array!
208 *
209 * @param array $frameworkConfiguration The framework configuration until now
210 * @return array context specific configuration which will override the configuration obtained by TypoScript
211 */
212 abstract protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration);
213
214 /**
215 * Returns TypoScript Setup array from current Environment.
216 *
217 * @return array the TypoScript setup
218 */
219 abstract protected function getTypoScriptSetup();
220
221 /**
222 * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin / module.tx_yourextension_yourmodule
223 * merged with the global configuration of your extension from plugin.tx_yourextension / module.tx_yourextension
224 *
225 * @param string $extensionName
226 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
227 * @return array
228 */
229 abstract protected function getPluginConfiguration($extensionName, $pluginName);
230
231 /**
232 * Returns the configured controller/action pairs of the specified plugin/module in the format
233 * array(
234 * 'Controller1' => array('action1', 'action2'),
235 * 'Controller2' => array('action3', 'action4')
236 * )
237 *
238 * @param string $extensionName
239 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
240 * @return array
241 */
242 abstract protected function getSwitchableControllerActions($extensionName, $pluginName);
243 }
244 ?>