[+BUGFIX] Extbase (Configuration): Fixed the configuration manager so that the storag...
[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 {
33
34 /**
35 * Default backend storage PID
36 */
37 const DEFAULT_BACKEND_STORAGE_PID = 0;
38
39 /**
40 * The TypoScript parser
41 *
42 * @var t3lib_TSparser
43 */
44 protected $typoScriptParser;
45
46 /**
47 * Storage for the settings, loaded by loadSettings()
48 *
49 * @var array
50 */
51 protected $settings;
52
53 /**
54 * The configuration source instances used for loading the raw configuration
55 *
56 * @var array
57 */
58 protected $configurationSources = array();
59
60 /**
61 * Constructs the configuration manager
62 *
63 * @param array $configurationSources An array of configuration sources
64 */
65 public function __construct($configurationSources = NULL) {
66 $this->typoScriptParser = t3lib_div::makeInstance('t3lib_TSparser');
67 if (is_array($configurationSources)) {
68 $this->configurationSources = $configurationSources;
69 }
70 }
71
72 /**
73 * Returns an array with the settings defined for the specified extension.
74 *
75 * @param string $extensionName Name of the extension to return the settings for
76 * @return array The settings of the specified extension
77 */
78 public function getSettings($extensionName) {
79 if (empty($this->settings[$extensionName])) {
80 $this->loadSettings($extensionName);
81 }
82 return $this->settings[$extensionName];
83 }
84
85 /**
86 * Loads the Extbase Framework configuration.
87 *
88 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
89 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
90 *
91 * @param array $pluginConfiguration The current incoming extbase configuration
92 * @return array the Extbase framework configuration
93 */
94 public function getFrameworkConfiguration($pluginConfiguration) {
95 $frameworkConfiguration = array();
96 $frameworkConfiguration['persistence']['storagePid'] = self::DEFAULT_BACKEND_STORAGE_PID;
97
98 $setup = $this->loadTypoScriptSetup();
99 $extbaseConfiguration = $setup['config.']['tx_extbase.'];
100 if (is_array($extbaseConfiguration)) {
101 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($extbaseConfiguration);
102 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $extbaseConfiguration);
103 }
104
105 if (isset($pluginConfiguration['settings'])) {
106 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'settings');
107 }
108 if (isset($pluginConfiguration['persistence'])) {
109 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'persistence');
110 }
111 if (isset($pluginConfiguration['view'])) {
112 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'view');
113 }
114 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
115
116 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $this->getContextSpecificFrameworkConfiguration());
117 return $frameworkConfiguration;
118 }
119
120 /**
121 * The context specific configuration returned by this method
122 * will override the framework configuration which was
123 * obtained from TypoScript. This can be used f.e. to override the storagePid
124 * with the value set inside the Plugin Instance.
125 *
126 * WARNING: Make sure this method ALWAYS returns an array!
127 *
128 * @return array context specific configuration which will override the configuration obtained by TypoScript
129 */
130 abstract protected function getContextSpecificFrameworkConfiguration();
131
132 /**
133 * Returns TypoScript Setup array from current Environment.
134 *
135 * @return array the TypoScript setup
136 */
137 abstract public function loadTypoScriptSetup();
138
139 /**
140 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
141 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
142 * needs to be resolved separately.
143 *
144 * @param array $pluginConfiguration The whole plugin configuration
145 * @param string $setting The key inside the $pluginConfiguration to check
146 * @return array The modified plugin configuration
147 */
148 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
149 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
150 $key = trim(substr($pluginConfiguration[$setting], 1));
151 $setup = $this->loadTypoScriptSetup();
152 list(, $newValue) = $this->typoScriptParser->getVal($key, $setup);
153
154 unset($pluginConfiguration[$setting]);
155 $pluginConfiguration[$setting . '.'] = $newValue;
156 }
157 return $pluginConfiguration;
158 }
159
160 /**
161 * Loads the settings defined in the specified extensions and merges them with
162 * those potentially existing in the global configuration folders.
163 *
164 * The result is stored in the configuration manager's settings registry
165 * and can be retrieved with the getSettings() method.
166 *
167 * @param string $extensionName
168 * @return void
169 * @see getSettings()
170 */
171 protected function loadSettings($extensionName) {
172 $settings = array();
173 foreach ($this->configurationSources as $configurationSource) {
174 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load($extensionName));
175 }
176 $this->settings[$extensionName] = $settings;
177 }
178
179
180 }
181 ?>