c5e5d718594539d52f48a772d112f4d805eb0633
[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 $configurationSource = t3lib_div::makeInstance('Tx_Extbase_Configuration_Source_TypoScriptSource');
102 $settings['storagePid'] = $this->getDefaultStoragePageId($cObj);
103 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load('Extbase'));
104 $settings = t3lib_div::array_merge_recursive_overrule($settings, self::postProcessSettings($configuration));
105 $settings['contentObjectData'] = $cObj->data;
106
107 $this->settings['Extbase'] = $settings;
108 }
109
110 /**
111 * Extracts the default storage PID from $this->cObj->data['pages']. ONLY ALLOWS ONE STORAGE PID!
112 * If this one is empty, tries to use $this->cObj->data['storage_pid'].
113 * If this one is empty, tries to use $this->cObj->parentRecord->data['storage_pid']. If all tree are empty, uses current page.
114 *
115 * @param tslib_cObj $cObj The current Content Object
116 * @return integer
117 * @throws InvalidArgumentException if more than one storage page ID is given
118 */
119 protected function getDefaultStoragePageId($cObj) {
120 if (is_string($cObj->data['pages'])) {
121 if (count(explode(',', $cObj->data['pages'])) > 1) {
122 throw new InvalidArgumentException('More than one storage page ID given. This is currently not supported.', 1247597243);
123 }
124 return (int)$cObj->data['pages'];
125 }
126
127 if ($cObj->data['storage_pid'] > 0) {
128 return (int)$cObj->data['storage_pid'];
129 }
130
131 if ($cObj->parentRecord->data['storage_pid'] > 0) {
132 return (int)$cObj->parentRecord->data['storage_pid'];
133 }
134 return self::DEFAULT_STORAGE_PID;
135 }
136
137 /**
138 * Loads the settings defined in the specified extensions and merges them with
139 * those potentially existing in the global configuration folders.
140 *
141 * The result is stored in the configuration manager's settings registry
142 * and can be retrieved with the getSettings() method.
143 *
144 * @param string $extensionName
145 * @return void
146 * @see getSettings()
147 * @internal
148 */
149 protected function loadGlobalSettings($extensionName) {
150 $settings = array();
151 foreach ($this->configurationSources as $configurationSource) {
152 $settings = t3lib_div::array_merge_recursive_overrule($settings, $configurationSource->load($extensionName));
153 }
154 $this->settings[$extensionName] = $settings;
155 }
156
157 /**
158 * Removes all trailing dots recursively from TS settings array
159 *
160 * @param array $setup The settings array
161 * @return void
162 */
163 public static function postProcessSettings(array $settings) {
164 $processedSettings = array();
165 foreach ($settings as $key => $value) {
166 if (substr($key, -1) === '.') {
167 $keyWithoutDot = substr($key, 0, -1);
168 $processedSettings[$keyWithoutDot] = self::postProcessSettings($value);
169 if (array_key_exists($keyWithoutDot, $settings)) {
170 $processedSettings[$keyWithoutDot]['_typoScriptNodeValue'] = $settings[$keyWithoutDot];
171 unset($settings[$keyWithoutDot]);
172 }
173 } else {
174 $keyWithDot = $key . '.';
175 if (array_key_exists($keyWithDot, $settings)) {
176 $processedSettings[$key] = self::postProcessSettings($settings[$keyWithDot]);
177 $processedSettings[$key]['_typoScriptNodeValue'] = $value;
178 unset($settings[$keyWithDot]);
179 } else {
180 $processedSettings[$key] = $value;
181 }
182 }
183 }
184 return $processedSettings;
185 }
186
187 }
188 ?>