e8c5e8459859dc6e050c887ef1281574a468a842
[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 if ($extensionName !== NULL) {
143 $pluginConfiguration = $this->getPluginConfiguration($extensionName, $pluginName);
144 $pluginConfiguration['controllerConfiguration'] = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName][$pluginName]['controllers'];
145 } else {
146 $pluginConfiguration = $this->getPluginConfiguration($this->extensionName, $this->pluginName);
147 $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, $this->configuration);
148 $pluginConfiguration['controllerConfiguration'] = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$this->extensionName][$this->pluginName]['controllers'];
149 if (isset($this->configuration['switchableControllerActions'])) {
150 $this->overrideSwitchableControllerActions($pluginConfiguration, $this->configuration['switchableControllerActions']);
151 }
152 }
153 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $pluginConfiguration);
154
155 // only load context specific configuration when retrieving configuration of the current plugin
156 if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
157 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
158 }
159
160 // 1st level cache
161 $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
162 return $frameworkConfiguration;
163 }
164
165 /**
166 * Returns the TypoScript configuration found in config.tx_extbase
167 *
168 * @return array
169 */
170 protected function getExtbaseConfiguration() {
171 $setup = $this->getTypoScriptSetup();
172 $extbaseConfiguration = array();
173 if (isset($setup['config.']['tx_extbase.'])) {
174 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['config.']['tx_extbase.']);
175 }
176 return $extbaseConfiguration;
177 }
178
179 /**
180 * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin
181 * merged with the global configuration of your extension from plugin.tx_yourextension
182 *
183 * @param string $extensionName
184 * @param string $pluginName
185 * @return array
186 */
187 protected function getPluginConfiguration($extensionName, $pluginName) {
188 $setup = $this->getTypoScriptSetup();
189 $pluginConfiguration = array();
190 if (is_array($setup['plugin.']['tx_' . strtolower($extensionName) . '.'])) {
191 $pluginConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . strtolower($extensionName) . '.']);
192 }
193 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
194 if (is_array($setup['plugin.']['tx_' . $pluginSignature . '.'])) {
195 $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . $pluginSignature . '.']));
196 }
197 return $pluginConfiguration;
198 }
199
200 /**
201 * @param array $frameworkConfiguration
202 * @param array $overriddenSwitchableControllerActions in the format array('Controller1' => array('action1', 'action2'), 'Controller2' => ...)
203 * @return void
204 */
205 protected function overrideSwitchableControllerActions(array &$frameworkConfiguration, array $switchableControllerActions) {
206 $overriddenSwitchableControllerActions = array();
207 foreach ($switchableControllerActions as $controllerName => $actions) {
208 $overriddenSwitchableControllerActions[$controllerName] = array('actions' => $actions);
209 $nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'];
210 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
211 if (!empty($overriddenNonCacheableActions)) {
212 $overriddenSwitchableControllerActions[$controllerName]['nonCacheableActions'] = $overriddenNonCacheableActions;
213 }
214 }
215 $frameworkConfiguration['controllerConfiguration'] = $overriddenSwitchableControllerActions;
216 }
217
218 /**
219 * The context specific configuration returned by this method
220 * will override the framework configuration which was
221 * obtained from TypoScript. This can be used f.e. to override the storagePid
222 * with the value set inside the Plugin Instance.
223 *
224 * WARNING: Make sure this method ALWAYS returns an array!
225 *
226 * @param array $frameworkConfiguration The framework configuration until now
227 * @return array context specific configuration which will override the configuration obtained by TypoScript
228 */
229 abstract protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration);
230
231 /**
232 * Returns TypoScript Setup array from current Environment.
233 *
234 * @return array the TypoScript setup
235 */
236 abstract protected function getTypoScriptSetup();
237 }
238 ?>