[+FEATURE] Extbase (Configuration): Configuration is available in Backend modules...
[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 return $frameworkConfiguration;
117 }
118
119 /**
120 * Returns TypoScript Setup array from current Environment.
121 *
122 * @return array the TypoScript setup
123 */
124 abstract public function loadTypoScriptSetup();
125
126 /**
127 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
128 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
129 * needs to be resolved separately.
130 *
131 * @param array $pluginConfiguration The whole plugin configuration
132 * @param string $setting The key inside the $pluginConfiguration to check
133 * @return array The modified plugin configuration
134 */
135 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
136 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
137 $key = trim(substr($pluginConfiguration[$setting], 1));
138 $setup = $this->loadTypoScriptSetup();
139 list(, $newValue) = $this->typoScriptParser->getVal($key, $setup);
140
141 unset($pluginConfiguration[$setting]);
142 $pluginConfiguration[$setting . '.'] = $newValue;
143 }
144 return $pluginConfiguration;
145 }
146
147 /**
148 * Loads the settings defined in the specified extensions and merges them with
149 * those potentially existing in the global configuration folders.
150 *
151 * The result is stored in the configuration manager's settings registry
152 * and can be retrieved with the getSettings() method.
153 *
154 * @param string $extensionName
155 * @return void
156 * @see getSettings()
157 */
158 protected function loadSettings($extensionName) {
159 $settings = array();
160 foreach ($this->configurationSources as $configurationSource) {
161 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load($extensionName));
162 }
163 $this->settings[$extensionName] = $settings;
164 }
165
166
167 }
168 ?>