Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[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 if (!is_array($pluginConfiguration['settings.'])) $pluginConfiguration['settings.'] = array(); // We expect that the settings are arrays on various places
84 }
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
95 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration));
96
97 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
98 return $frameworkConfiguration;
99 }
100
101 /**
102 * The context specific configuration returned by this method
103 * will override the framework configuration which was
104 * obtained from TypoScript. This can be used f.e. to override the storagePid
105 * with the value set inside the Plugin Instance.
106 *
107 * WARNING: Make sure this method ALWAYS returns an array!
108 *
109 * @param array $frameworkConfiguration The framework configuration until now
110 * @return array context specific configuration which will override the configuration obtained by TypoScript
111 */
112 abstract protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration);
113
114 /**
115 * Returns TypoScript Setup array from current Environment.
116 *
117 * @return array the TypoScript setup
118 */
119 abstract public function loadTypoScriptSetup();
120
121 /**
122 * Resolves the TypoScript reference for $pluginConfiguration[$setting].
123 * In case the setting is a string and starts with "<", we know that this is a TypoScript reference which
124 * needs to be resolved separately.
125 *
126 * @param array $pluginConfiguration The whole plugin configuration
127 * @param string $setting The key inside the $pluginConfiguration to check
128 * @return array The modified plugin configuration
129 */
130 protected function resolveTyposcriptReference($pluginConfiguration, $setting) {
131 if (is_string($pluginConfiguration[$setting]) && substr($pluginConfiguration[$setting], 0, 1) === '<') {
132 $key = trim(substr($pluginConfiguration[$setting], 1));
133 $setup = $this->loadTypoScriptSetup();
134 list(, $newValue) = $this->typoScriptParser->getVal($key, $setup);
135
136 unset($pluginConfiguration[$setting]);
137 $pluginConfiguration[$setting . '.'] = $newValue;
138 }
139 return $pluginConfiguration;
140 }
141 }
142 ?>