[FEATURE] Fluid (TemplateView): Made templateRootPath configurable via TypoScript...
[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 if (isset($pluginConfiguration['view'])) {
132 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'view');
133 }
134 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
135 return $frameworkConfiguration;
136 }
137
138 /**
139 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
140 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
141 * needs to be resolved separately.
142 *
143 * @param array $pluginConfiguration The whole plugin configuration
144 * @param string $setting The key inside the $pluginConfiguration to check
145 * @return array The modified plugin configuration
146 */
147 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
148 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
149 $key = trim(substr($pluginConfiguration[$setting], 1));
150 list(, $newValue) = $this->typoScriptParser->getVal($key, $GLOBALS['TSFE']->tmpl->setup);
151
152 unset($pluginConfiguration[$setting]);
153 $pluginConfiguration[$setting . '.'] = $newValue;
154 }
155 return $pluginConfiguration;
156 }
157
158 /**
159 * Extracts the default storage PID from $this->cObj->data['pages'].
160 * If this one is empty, tries to use $this->cObj->data['storage_pid'].
161 * If this one is empty, tries to use $this->cObj->parentRecord->data['storage_pid'].
162 * If all three are empty, uses getStorageSiterootPids() in FE, and 0 in BE.
163 *
164 * @param tslib_cObj $cObj The current Content Object
165 * @return string a comma separated list of integers to be used to fetch records from.
166 */
167 protected function getDefaultStoragePageId($cObj) {
168 if (is_string($cObj->data['pages']) && strlen($cObj->data['pages']) > 0) {
169 return $cObj->data['pages'];
170 }
171
172 if ($cObj->data['storage_pid'] > 0) {
173 return $cObj->data['storage_pid'];
174 }
175
176 if ($cObj->parentRecord->data['storage_pid'] > 0) {
177 return $cObj->parentRecord->data['storage_pid'];
178 }
179 if (TYPO3_MODE === 'FE') {
180 $storageSiterootPids = $GLOBALS['TSFE']->getStorageSiterootPids();
181 if (isset($storageSiterootPids['_STORAGE_PID'])) {
182 return $storageSiterootPids['_STORAGE_PID'];
183 }
184 }
185 return self::DEFAULT_BACKEND_STORAGE_PID;
186 }
187
188 // /**
189 // * Scans all configuration directories in the extension directories.
190 // *
191 // * @return void
192 // */
193 // protected function scanAvailableTyposcriptConfigurations() {
194 // foreach (new DirectoryIterator(t3lib_extMgm::) as $parentFileInfo) {
195 // $parentFilename = $parentFileInfo->getFilename();
196 // if ($parentFilename[0] === '.' || !$parentFileInfo->isDir()) continue;
197 //
198 // foreach (new DirectoryIterator($parentFileInfo->getPathname()) as $childFileInfo) {
199 // $childFilename = $childFileInfo->getFilename();
200 // if ($childFilename[0] !== '.' && $childFilename !== 'FLOW3') {
201 // $packagePath = \F3\FLOW3\Utility\Files::getUnixStylePath($childFileInfo->getPathName()) . '/';
202 // $this->packages[$childFilename] = $this->objectFactory->create('F3\FLOW3\Package\Package', $childFilename, $packagePath);
203 // }
204 // }
205 // }
206 // foreach (array_keys($this->packages) as $upperCamelCasedPackageKey) {
207 // $this->packageKeys[strtolower($upperCamelCasedPackageKey)] = $upperCamelCasedPackageKey;
208 // }
209 // }
210
211 }
212 ?>