[~CONF] Extbase (Configuration): The configuration is now stored in ext_typoscript_se...
[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 * @internal
34 */
35 class Tx_Extbase_Configuration_Manager {
36
37 const DEFAULT_STORAGE_PID = 0;
38
39 /**
40 * Storage for the settings, loaded by loadGlobalSettings()
41 *
42 * @var array
43 */
44 protected $settings;
45
46 /**
47 * The configuration source instances used for loading the raw configuration
48 *
49 * @var array
50 */
51 protected $configurationSources = array();
52
53 /**
54 * Constructs the configuration manager
55 *
56 * @param array $configurationSources An array of configuration sources
57 */
58 public function __construct($configurationSources = NULL) {
59 if (is_array($configurationSources)) {
60 $this->configurationSources = $configurationSources;
61 }
62 }
63
64 /**
65 * Returns an array with the settings defined for the specified extension.
66 *
67 * @param string $extensionName Name of the extension to return the settings for
68 * @param string $controllerName Name of the controller to return the settings for
69 * @param string $actionName Name of the action to return the settings for
70 * @return array The settings of the specified extension
71 * @internal
72 */
73 public function getSettings($extensionName, $controllerName = '', $actionName = '') {
74 if (empty($this->settings[$extensionName])) {
75 $this->loadGlobalSettings($extensionName);
76 }
77 $settings = $this->settings[$extensionName];
78 if (!empty($controllerName) && is_array($settings[$controllerName])) {
79 if (!empty($actionName) && is_array($settings[$controllerName][$actionName])) {
80 $settings = $settings[$controllerName][$actionName];
81 } else {
82 $settings = $settings[$controllerName];
83 }
84 }
85 return $settings;
86 }
87
88 /**
89 * Loads the Extbase core settings.
90 *
91 * The Extbase settings can be retrieved like any other setting through the
92 * getSettings() method but need to be loaded separately because they are
93 * needed way earlier in the bootstrap than the package's settings.
94 *
95 * @param array $configuration The current incoming extbase configuration
96 * @param tslib_cObj $cObj The current Content Object
97 * @return void
98 */
99 public function loadExtbaseSettings($configuration, $cObj) {
100 $settings = array();
101 $settings['storagePid'] = $this->getDefaultStoragePageId($cObj);
102 $settings['contentObjectData'] = $cObj->data;
103 $extbaseConfiguration = $GLOBALS['TSFE']->tmpl->setup['config.']['tx_extbase.'];
104 if (is_array($extbaseConfiguration)) {
105 $extbaseConfiguration = Tx_Extbase_Configuration_Manager::postProcessSettings($extbaseConfiguration);
106 } else {
107 $extbaseConfiguration = array();
108 }
109 $settings = t3lib_div::array_merge_recursive_overrule($settings, $extbaseConfiguration);
110 $settings = t3lib_div::array_merge_recursive_overrule($settings, self::postProcessSettings($configuration));
111
112 $this->settings['Extbase'] = $settings;
113 }
114
115 /**
116 * Extracts the default storage PID from $this->cObj->data['pages']. ONLY ALLOWS ONE STORAGE PID!
117 * If this one is empty, tries to use $this->cObj->data['storage_pid'].
118 * If this one is empty, tries to use $this->cObj->parentRecord->data['storage_pid']. If all tree are empty, uses current page.
119 *
120 * @param tslib_cObj $cObj The current Content Object
121 * @return integer
122 * @throws InvalidArgumentException if more than one storage page ID is given
123 */
124 protected function getDefaultStoragePageId($cObj) {
125 if (is_string($cObj->data['pages'])) {
126 if (count(explode(',', $cObj->data['pages'])) > 1) {
127 throw new InvalidArgumentException('More than one storage page ID given. This is currently not supported.', 1247597243);
128 }
129 return (int)$cObj->data['pages'];
130 }
131
132 if ($cObj->data['storage_pid'] > 0) {
133 return (int)$cObj->data['storage_pid'];
134 }
135
136 if ($cObj->parentRecord->data['storage_pid'] > 0) {
137 return (int)$cObj->parentRecord->data['storage_pid'];
138 }
139 return self::DEFAULT_STORAGE_PID;
140 }
141
142 /**
143 * Loads the settings defined in the specified extensions and merges them with
144 * those potentially existing in the global configuration folders.
145 *
146 * The result is stored in the configuration manager's settings registry
147 * and can be retrieved with the getSettings() method.
148 *
149 * @param string $extensionName
150 * @return void
151 * @see getSettings()
152 * @internal
153 */
154 protected function loadGlobalSettings($extensionName) {
155 $settings = array();
156 foreach ($this->configurationSources as $configurationSource) {
157 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load($extensionName));
158 }
159 $this->settings[$extensionName] = $settings;
160 }
161
162 /**
163 * Removes all trailing dots recursively from TS settings array
164 *
165 * @param array $setup The settings array
166 * @return void
167 */
168 public static function postProcessSettings(array $settings) {
169 $processedSettings = array();
170 foreach ($settings as $key => $value) {
171 if (substr($key, -1) === '.') {
172 $keyWithoutDot = substr($key, 0, -1);
173 $processedSettings[$keyWithoutDot] = self::postProcessSettings($value);
174 if (array_key_exists($keyWithoutDot, $settings)) {
175 $processedSettings[$keyWithoutDot]['_typoScriptNodeValue'] = $settings[$keyWithoutDot];
176 unset($settings[$keyWithoutDot]);
177 }
178 } else {
179 $keyWithDot = $key . '.';
180 if (array_key_exists($keyWithDot, $settings)) {
181 $processedSettings[$key] = self::postProcessSettings($settings[$keyWithDot]);
182 $processedSettings[$key]['_typoScriptNodeValue'] = $value;
183 unset($settings[$keyWithDot]);
184 } else {
185 $processedSettings[$key] = $value;
186 }
187 }
188 }
189 return $processedSettings;
190 }
191
192 }
193 ?>