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