[~TAKS] Extbase (MVC): FrontendRequestHandler now retrieves the current cObject throu...
[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 * The TypoScript parser
52 *
53 * @var t3lib_TSparser
54 */
55 protected $typoScriptParser;
56
57 /**
58 * @var Tx_Extbase_Object_ObjectManagerInterface
59 */
60 protected $objectManager;
61
62 /**
63 * name of the extension this Configuration Manager instance belongs to
64 * @var string
65 */
66 protected $extensionName;
67
68 /**
69 * name of the plugin this Configuration Manager instance belongs to
70 * @var string
71 */
72 protected $pluginName;
73
74 /**
75 * 1st level configuration cache
76 *
77 * @var array
78 */
79 protected $configurationCache = array();
80
81 /**
82 * @param Tx_Extbase_Object_ManagerInterface $objectManager
83 * @return void
84 */
85 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
86 $this->objectManager = $objectManager;
87 $this->typoScriptParser = t3lib_div::makeInstance('t3lib_TSparser');
88 }
89
90 /**
91 * @param tslib_cObj $contentObject
92 * @return void
93 */
94 public function setContentObject(tslib_cObj $contentObject = NULL) {
95 $this->contentObject = $contentObject;
96 }
97
98 /**
99 * @return tslib_cObj
100 */
101 public function getContentObject() {
102 return $this->contentObject;
103 }
104
105 /**
106 * Sets the specified raw configuration coming from the outside.
107 * Note that this is a low level method and only makes sense to be used by Extbase internally.
108 *
109 * @param array $configuration The new configuration
110 * @return void
111 */
112 public function setConfiguration(array $configuration = array()) {
113 // reset 1st level cache
114 $this->configurationCache = array();
115
116 $this->extensionName = $configuration['extensionName'];
117 $this->pluginName = $configuration['pluginName'];
118 $this->configuration = $configuration;
119 }
120
121 /**
122 * Loads the Extbase Framework configuration.
123 *
124 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
125 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
126 *
127 * @param string $extensionName if specified, the configuration for the given extension will be returned (plugin.tx_extensionname)
128 * @param string $pluginName if specified, the configuration for the given plugin will be returned (plugin.tx_extensionname_pluginname)
129 * @return array the Extbase framework configuration
130 */
131 public function getConfiguration($extensionName = NULL, $pluginName = NULL) {
132 // 1st level cache
133 if ($extensionName !== NULL) {
134 $configurationCacheKey = strtolower($extensionName);
135 if ($pluginName !== NULL) {
136 $configurationCacheKey .= '_' . strtolower($pluginName);
137 }
138 } else {
139 $configurationCacheKey = strtolower($this->extensionName . '_' . $this->pluginName);
140 }
141 if (isset($this->configurationCache[$configurationCacheKey])) {
142 return $this->configurationCache[$configurationCacheKey];
143 }
144
145 $frameworkConfiguration = array();
146 $frameworkConfiguration['persistence']['storagePid'] = self::DEFAULT_BACKEND_STORAGE_PID;
147
148 $setup = $this->getTypoScriptSetup();
149 if ($extensionName !== NULL) {
150 $pluginConfiguration = $setup['plugin.']['tx_' . strtolower($extensionName) . '.'];
151 if ($pluginName !== NULL) {
152 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
153 if (is_array($setup['plugin.']['tx_' . $pluginSignature . '.'])) {
154 $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, $setup['plugin.']['tx_' . $pluginSignature . '.']);
155 }
156 }
157 } else {
158 $pluginConfiguration = $this->configuration;
159 }
160 $extbaseConfiguration = $setup['config.']['tx_extbase.'];
161 if (is_array($extbaseConfiguration)) {
162 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($extbaseConfiguration);
163 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $extbaseConfiguration);
164 }
165
166 if (isset($pluginConfiguration['settings'])) {
167 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'settings');
168 }
169 if (!is_array($pluginConfiguration['settings.'])) $pluginConfiguration['settings.'] = array(); // We expect that the settings are arrays on various places
170 if (isset($pluginConfiguration['persistence'])) {
171 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'persistence');
172 }
173 if (isset($pluginConfiguration['view'])) {
174 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'view');
175 }
176 if (isset($pluginConfiguration['_LOCAL_LANG'])) {
177 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, '_LOCAL_LANG');
178 }
179 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
180
181 // only load context specific configuration when retrieving configuration of the current plugin
182 if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
183 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
184 }
185
186 // 1st level cache
187 $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
188 return $frameworkConfiguration;
189 }
190
191 /**
192 * The context specific configuration returned by this method
193 * will override the framework configuration which was
194 * obtained from TypoScript. This can be used f.e. to override the storagePid
195 * with the value set inside the Plugin Instance.
196 *
197 * WARNING: Make sure this method ALWAYS returns an array!
198 *
199 * @param array $frameworkConfiguration The framework configuration until now
200 * @return array context specific configuration which will override the configuration obtained by TypoScript
201 */
202 abstract protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration);
203
204 /**
205 * Returns TypoScript Setup array from current Environment.
206 *
207 * @return array the TypoScript setup
208 */
209 abstract protected function getTypoScriptSetup();
210
211 /**
212 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
213 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
214 * needs to be resolved separately.
215 *
216 * @param array $pluginConfiguration The whole plugin configuration
217 * @param string $setting The key inside the $pluginConfiguration to check
218 * @return array The modified plugin configuration
219 */
220 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
221 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
222 $key = trim(substr($pluginConfiguration[$setting], 1));
223 $setup = $this->getTypoScriptSetup();
224 list(, $newValue) = $this->typoScriptParser->getVal($key, $setup);
225
226 unset($pluginConfiguration[$setting]);
227 $pluginConfiguration[$setting . '.'] = $newValue;
228 }
229 return $pluginConfiguration;
230 }
231 }
232 ?>