[+BUGFIX] Extbase (Persistence): Fixed a problem where the cache was cleared at every...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / FrontendConfigurationManager.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 used in frontend mode.
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_FrontendConfigurationManager extends Tx_Extbase_Configuration_AbstractConfigurationManager {
35
36 /**
37 * @var tslib_cObj
38 */
39 protected $contentObject;
40
41 /**
42 * @param tslib_cObj $contentObject
43 * @return void
44 */
45 public function setContentObject(tslib_cObj $contentObject) {
46 $this->contentObject = $contentObject;
47 }
48
49 /**
50 * Returns TypoScript Setup array from current Environment.
51 *
52 * @return array the TypoScript setup
53 */
54 public function loadTypoScriptSetup() {
55 return $GLOBALS['TSFE']->tmpl->setup;
56 }
57
58 /**
59 * Get context specific framework configuration.
60 * - Overrides storage PID with setting "Startingpoint"
61 * - merge flexform configuration, if needed
62 *
63 * @param array $frameworkConfiguration The framework configuration to modify
64 * @return array the modified framework configuration
65 */
66 protected function getContextSpecificFrameworkConfiguration($frameworkConfiguration) {
67 $frameworkConfiguration = $this->overrideStoragePidIfStartingPointIsSet($frameworkConfiguration);
68 $frameworkConfiguration = $this->overrideConfigurationFromFlexform($frameworkConfiguration);
69
70 return $frameworkConfiguration;
71 }
72
73 /**
74 * Overrides the storage PID settings, in case the "Startingpoint" settings
75 * is set in the plugin configuration.
76 *
77 * @param array $frameworkConfiguration the framework configurations
78 * @return array the framework configuration with overriden storagePid
79 */
80 protected function overrideStoragePidIfStartingPointIsSet($frameworkConfiguration) {
81 if (is_string($this->contentObject->data['pages'])
82 && strlen($this->contentObject->data['pages']) > 0) {
83 $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, array(
84 'persistence' => array(
85 'storagePid' => $this->contentObject->data['pages']
86 )
87 ));
88 }
89
90 return $frameworkConfiguration;
91 }
92
93 /**
94 * Overrides configuration settings from flexforms.
95 * This merges the whole flexform data, and overrides switchable controller actions.
96 *
97 * @param array the framework configuration
98 * @return array the framework configuration with overridden data from flexform
99 */
100 protected function overrideConfigurationFromFlexform(array $frameworkConfiguration) {
101 if (strlen($this->contentObject->data['pi_flexform']) > 0) {
102 $flexformConfiguration = $this->convertFlexformContentToArray($this->contentObject->data['pi_flexform']);
103
104 $frameworkConfiguration = $this->mergeConfigurationPartFromFlexformIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'settings');
105 $frameworkConfiguration = $this->mergeConfigurationPartFromFlexformIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'persistence');
106 $frameworkConfiguration = $this->mergeConfigurationPartFromFlexformIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'view');
107
108 $frameworkConfiguration = $this->overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration);
109 }
110 return $frameworkConfiguration;
111 }
112
113 /**
114 * Parses the FlexForm content recursivly and converts it to an array
115 * The resulting array will be multi-dimensional, as a value "bla.blubb"
116 * results in two levels, and a value "bla.blubb.bla" results in three levels.
117 *
118 * Note: multi-language FlexForms are not supported yet
119 *
120 * @param string $flexFormContent FlexForm xml string
121 * @return array the processed array
122 */
123 protected function convertFlexformContentToArray($flexFormContent) {
124 $settings = array();
125 $languagePointer = 'lDEF';
126 $valuePointer = 'vDEF';
127
128 $flexFormArray = t3lib_div::xml2array($flexFormContent);
129 $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
130 foreach(array_values($flexFormArray) as $languages) {
131 if (!is_array($languages[$languagePointer])) {
132 continue;
133 }
134 foreach($languages[$languagePointer] as $valueKey => $valueDefinition) {
135 if (strpos($valueKey, '.') === false) {
136 $settings[$valueKey] = $valueDefinition[$valuePointer];
137 } else {
138 $valueKeyParts = explode('.', $valueKey);
139 $currentNode =& $settings;
140 foreach ($valueKeyParts as $valueKeyPart) {
141 $currentNode =& $currentNode[$valueKeyPart];
142 }
143 $currentNode = $valueDefinition[$valuePointer];
144 }
145 }
146 }
147 return $settings;
148 }
149
150 /**
151 * Merge a configuration part from the flexform configuration to the framework configuration.
152 *
153 * @param array $frameworkConfiguration the framework configuration to merge the data on
154 * @param array $flexformConfiguration The full flexform configuration
155 * @param string $configurationPartName The name of the configuration part which should be merged.
156 * @return array the processed framework configuration
157 */
158 protected function mergeConfigurationPartFromFlexformIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, $configurationPartName) {
159 if (is_array($flexformConfiguration[$configurationPartName])) {
160 $frameworkConfiguration[$configurationPartName] = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration[$configurationPartName], $flexformConfiguration[$configurationPartName]);
161 }
162 return $frameworkConfiguration;
163 }
164
165
166 /**
167 * Overrides the switchable controller actions from the flexform.
168 *
169 * @param $frameworkConfiguration The original framework configuration
170 * @param $flexformConfiguration The full flexform configuration
171 * @return array the modified framework configuration, if needed
172 * @todo: Check that the controller has been before inside the switchableControllerActions.
173 */
174 protected function overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration) {
175 if (isset($flexformConfiguration['switchableControllerActions']) && !is_array($flexformConfiguration['switchableControllerActions'])) {
176
177 // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by , first.
178 $switchableControllerActionPartsFromFlexform = t3lib_div::trimExplode(',', str_replace(';', ',', $flexformConfiguration['switchableControllerActions']));
179
180 $newSwitchableControllerActionsFromFlexform = array();
181 foreach ($switchableControllerActionPartsFromFlexform as $switchableControllerActionPartFromFlexform) {
182 list($controller, $action) = explode('->', $switchableControllerActionPartFromFlexform);
183 if (empty($controller) || empty($action)) {
184 throw new Tx_Extbase_Configuration_Exception_ParseError('Controller or action were empty when overriding switchableControllerActions from flexform.', 1257146403);
185 }
186
187 $newSwitchableControllerActionsFromFlexform[$controller][] = $action;
188 }
189
190 if (count($newSwitchableControllerActionsFromFlexform)) {
191 $frameworkConfiguration['switchableControllerActions'] = array();
192 foreach ($newSwitchableControllerActionsFromFlexform as $controller => $actions) {
193 $frameworkConfiguration['switchableControllerActions'][] = array(
194 'controller' => $controller,
195 'actions' => implode(',', $actions)
196 );
197 }
198
199 // We want the first controller/action be the default.
200 unset($frameworkConfiguration['controller']);
201 unset($frameworkConfiguration['action']);
202 }
203 }
204 return $frameworkConfiguration;
205 }
206 }
207 ?>