2ebf2847e7226bd8b8795d904a7bffefbbe5088a
[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 * Constructs the configuration manager
55 *
56 */
57 public function __construct() {
58 $this->typoScriptParser = t3lib_div::makeInstance('t3lib_TSparser');
59 }
60
61 /**
62 * Loads the Extbase Framework configuration.
63 *
64 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
65 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
66 *
67 * @param array $pluginConfiguration The current incoming extbase configuration
68 * @return array the Extbase framework configuration
69 */
70 public function getFrameworkConfiguration($pluginConfiguration) {
71 $frameworkConfiguration = array();
72 $frameworkConfiguration['persistence']['storagePid'] = self::DEFAULT_BACKEND_STORAGE_PID;
73
74 $setup = $this->loadTypoScriptSetup();
75 $extbaseConfiguration = $setup['config.']['tx_extbase.'];
76 if (is_array($extbaseConfiguration)) {
77 $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($extbaseConfiguration);
78 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $extbaseConfiguration);
79 }
80
81 if (isset($pluginConfiguration['settings'])) {
82 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'settings');
83 }
84 if (!is_array($pluginConfiguration['settings.'])) $pluginConfiguration['settings.'] = array(); // We expect that the settings are arrays on various places
85 if (isset($pluginConfiguration['persistence'])) {
86 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'persistence');
87 }
88 if (isset($pluginConfiguration['view'])) {
89 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, 'view');
90 }
91 if (isset($pluginConfiguration['_LOCAL_LANG'])) {
92 $pluginConfiguration = $this->resolveTyposcriptReference($pluginConfiguration, '_LOCAL_LANG');
93 }
94 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
95
96 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
97 return $frameworkConfiguration;
98 }
99
100 /**
101 * The context specific configuration returned by this method
102 * will override the framework configuration which was
103 * obtained from TypoScript. This can be used f.e. to override the storagePid
104 * with the value set inside the Plugin Instance.
105 *
106 * WARNING: Make sure this method ALWAYS returns an array!
107 *
108 * @param array $frameworkConfiguration The framework configuration until now
109 * @return array context specific configuration which will override the configuration obtained by TypoScript
110 */
111 abstract protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration);
112
113 /**
114 * Returns TypoScript Setup array from current Environment.
115 *
116 * @return array the TypoScript setup
117 */
118 abstract public function loadTypoScriptSetup();
119
120 /**
121 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
122 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
123 * needs to be resolved separately.
124 *
125 * @param array $pluginConfiguration The whole plugin configuration
126 * @param string $setting The key inside the $pluginConfiguration to check
127 * @return array The modified plugin configuration
128 */
129 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
130 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
131 $key = trim(substr($pluginConfiguration[$setting], 1));
132 $setup = $this->loadTypoScriptSetup();
133 list(, $newValue) = $this->typoScriptParser->getVal($key, $setup);
134
135 unset($pluginConfiguration[$setting]);
136 $pluginConfiguration[$setting . '.'] = $newValue;
137 }
138 return $pluginConfiguration;
139 }
140 }
141 ?>