ee9dcc1806179b94888b3363c81ddf4a032271b2
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / Manager.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 * A general purpose configuration manager
27 *
28 * Should NOT be singleton, as a new configuration manager is needed per plugin.
29 *
30 * @package Extbase
31 * @subpackage Configuration
32 * @version $ID:$
33 */
34 class Tx_Extbase_Configuration_Manager {
35
36 /**
37 * Default backend storage PID
38 */
39 const DEFAULT_BACKEND_STORAGE_PID = 0;
40
41 /**
42 * The TypoScript parser
43 *
44 * @var t3lib_TSparser
45 */
46 protected $typoScriptParser;
47
48 /**
49 * Storage for the settings, loaded by loadSettings()
50 *
51 * @var array
52 */
53 protected $settings;
54
55 /**
56 * The configuration source instances used for loading the raw configuration
57 *
58 * @var array
59 */
60 protected $configurationSources = array();
61
62 /**
63 * Constructs the configuration manager
64 *
65 * @param array $configurationSources An array of configuration sources
66 */
67 public function __construct($configurationSources = NULL) {
68 $this->typoScriptParser = t3lib_div::makeInstance('t3lib_TSparser');
69 if (is_array($configurationSources)) {
70 $this->configurationSources = $configurationSources;
71 }
72 }
73
74 /**
75 * Returns an array with the settings defined for the specified extension.
76 *
77 * @param string $extensionName Name of the extension to return the settings for
78 * @return array The settings of the specified extension
79 */
80 public function getSettings($extensionName) {
81 if (empty($this->settings[$extensionName])) {
82 $this->loadSettings($extensionName);
83 }
84 return $this->settings[$extensionName];
85 }
86
87 /**
88 * Loads the settings defined in the specified extensions and merges them with
89 * those potentially existing in the global configuration folders.
90 *
91 * The result is stored in the configuration manager's settings registry
92 * and can be retrieved with the getSettings() method.
93 *
94 * @param string $extensionName
95 * @return void
96 * @see getSettings()
97 */
98 protected function loadSettings($extensionName) {
99 $settings = array();
100 foreach ($this->configurationSources as $configurationSource) {
101 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load($extensionName));
102 }
103 $this->settings[$extensionName] = $settings;
104 }
105
106 /**
107 * Loads the Extbase Framework configuration.
108 *
109 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
110 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
111 *
112 * @param array $pluginConfiguration The current incoming extbase configuration
113 * @param tslib_cObj $cObj The current Content Object
114 * @return array the Extbase framework configuration
115 */
116 public function getFrameworkConfiguration($pluginConfiguration, $cObj) {
117 $frameworkConfiguration = array();
118 $frameworkConfiguration['persistence']['storagePid'] = $this->getDefaultStoragePageId($cObj);
119 $frameworkConfiguration['contentObjectData'] = $cObj->data;
120
121 // TODO Support BE modules by parsing the file "manually" and all files EXT:myext/Configuration/Objects/setup.txt
122 $extbaseConfiguration = $GLOBALS['TSFE']->tmpl->setup['config.']['tx_extbase.'];
123 if (is_array($extbaseConfiguration)) {
124 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($extbaseConfiguration);
125 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $extbaseConfiguration);
126 }
127
128 if (isset($pluginConfiguration['persistence'])) {
129 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'persistence');
130 }
131 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
132 return $frameworkConfiguration;
133 }
134
135 /**
136 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
137 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
138 * needs to be resolved separately.
139 *
140 * @param array $pluginConfiguration The whole plugin configuration
141 * @param string $setting The key inside the $pluginConfiguration to check
142 * @return array The modified plugin configuration
143 */
144 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
145 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
146 $key = trim(substr($pluginConfiguration[$setting], 1));
147 list(, $newValue) = $this->typoScriptParser->getVal($key, $GLOBALS['TSFE']->tmpl->setup);
148
149 unset($pluginConfiguration[$setting]);
150 $pluginConfiguration[$setting . '.'] = $newValue;
151 }
152 return $pluginConfiguration;
153 }
154
155 /**
156 * Extracts the default storage PID from $this->cObj->data['pages'].
157 * If this one is empty, tries to use $this->cObj->data['storage_pid'].
158 * If this one is empty, tries to use $this->cObj->parentRecord->data['storage_pid'].
159 * If all three are empty, uses getStorageSiterootPids() in FE, and 0 in BE.
160 *
161 * @param tslib_cObj $cObj The current Content Object
162 * @return string a comma separated list of integers to be used to fetch records from.
163 */
164 protected function getDefaultStoragePageId($cObj) {
165 if (is_string($cObj->data['pages']) && strlen($cObj->data['pages']) > 0) {
166 return $cObj->data['pages'];
167 }
168
169 if ($cObj->data['storage_pid'] > 0) {
170 return $cObj->data['storage_pid'];
171 }
172
173 if ($cObj->parentRecord->data['storage_pid'] > 0) {
174 return $cObj->parentRecord->data['storage_pid'];
175 }
176 if (TYPO3_MODE === 'FE') {
177 $storageSiterootPids = $GLOBALS['TSFE']->getStorageSiterootPids();
178 if (isset($storageSiterootPids['_STORAGE_PID'])) {
179 return $storageSiterootPids['_STORAGE_PID'];
180 }
181 }
182 return self::DEFAULT_BACKEND_STORAGE_PID;
183 }
184
185 // /**
186 // * Scans all configuration directories in the extension directories.
187 // *
188 // * @return void
189 // */
190 // protected function scanAvailableTyposcriptConfigurations() {
191 // foreach (new DirectoryIterator(t3lib_extMgm::) as $parentFileInfo) {
192 // $parentFilename = $parentFileInfo->getFilename();
193 // if ($parentFilename[0] === '.' || !$parentFileInfo->isDir()) continue;
194 //
195 // foreach (new DirectoryIterator($parentFileInfo->getPathname()) as $childFileInfo) {
196 // $childFilename = $childFileInfo->getFilename();
197 // if ($childFilename[0] !== '.' && $childFilename !== 'FLOW3') {
198 // $packagePath = \F3\FLOW3\Utility\Files::getUnixStylePath($childFileInfo->getPathName()) . '/';
199 // $this->packages[$childFilename] = $this->objectFactory->create('F3\FLOW3\Package\Package', $childFilename, $packagePath);
200 // }
201 // }
202 // }
203 // foreach (array_keys($this->packages) as $upperCamelCasedPackageKey) {
204 // $this->packageKeys[strtolower($upperCamelCasedPackageKey)] = $upperCamelCasedPackageKey;
205 // }
206 // }
207
208 }
209 ?>