[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / AbstractConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Abstract base class for a general purpose configuration manager
32 */
33 abstract class AbstractConfigurationManager implements \TYPO3\CMS\Core\SingletonInterface {
34
35 /**
36 * Default backend storage PID
37 */
38 const DEFAULT_BACKEND_STORAGE_PID = 0;
39
40 /**
41 * Storage of the raw TypoScript configuration
42 *
43 * @var array
44 */
45 protected $configuration = array();
46
47 /**
48 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
49 */
50 protected $contentObject;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
54 * @inject
55 */
56 protected $objectManager;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Service\TypoScriptService
60 * @inject
61 */
62 protected $typoScriptService;
63
64 /**
65 * name of the extension this Configuration Manager instance belongs to
66 *
67 * @var string
68 */
69 protected $extensionName;
70
71 /**
72 * name of the plugin this Configuration Manager instance belongs to
73 *
74 * @var string
75 */
76 protected $pluginName;
77
78 /**
79 * 1st level configuration cache
80 *
81 * @var array
82 */
83 protected $configurationCache = array();
84
85 /**
86 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
87 * @inject
88 */
89 protected $environmentService;
90
91 /**
92 * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject
93 * @return void
94 */
95 public function setContentObject(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject = NULL) {
96 $this->contentObject = $contentObject;
97 }
98
99 /**
100 * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer|NULL
101 */
102 public function getContentObject() {
103 if ($this->contentObject !== NULL) {
104 return $this->contentObject;
105 }
106 return NULL;
107 }
108
109 /**
110 * Sets the specified raw configuration coming from the outside.
111 * Note that this is a low level method and only makes sense to be used by Extbase internally.
112 *
113 * @param array $configuration The new configuration
114 * @return void
115 */
116 public function setConfiguration(array $configuration = array()) {
117 // reset 1st level cache
118 $this->configurationCache = array();
119 $this->extensionName = isset($configuration['extensionName']) ? $configuration['extensionName'] : NULL;
120 $this->pluginName = isset($configuration['pluginName']) ? $configuration['pluginName'] : NULL;
121 $this->configuration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($configuration);
122 }
123
124 /**
125 * Loads the Extbase Framework configuration.
126 *
127 * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
128 * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
129 *
130 * @param string $extensionName if specified, the configuration for the given extension will be returned (plugin.tx_extensionname)
131 * @param string $pluginName if specified, the configuration for the given plugin will be returned (plugin.tx_extensionname_pluginname)
132 * @return array the Extbase framework configuration
133 */
134 public function getConfiguration($extensionName = NULL, $pluginName = NULL) {
135 // 1st level cache
136 $configurationCacheKey = strtolower(($extensionName ?: $this->extensionName) . '_' . ($pluginName ?: $this->pluginName));
137 if (isset($this->configurationCache[$configurationCacheKey])) {
138 return $this->configurationCache[$configurationCacheKey];
139 }
140 $frameworkConfiguration = $this->getExtbaseConfiguration();
141 if (!isset($frameworkConfiguration['persistence']['storagePid'])) {
142 $frameworkConfiguration['persistence']['storagePid'] = $this->getDefaultBackendStoragePid();
143 }
144 // only merge $this->configuration and override switchableControllerActions when retrieving configuration of the current plugin
145 if ($extensionName === NULL || $extensionName === $this->extensionName && $pluginName === $this->pluginName) {
146 $pluginConfiguration = $this->getPluginConfiguration($this->extensionName, $this->pluginName);
147 $pluginConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($pluginConfiguration, $this->configuration);
148 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($this->extensionName, $this->pluginName);
149 if (isset($this->configuration['switchableControllerActions'])) {
150 $this->overrideSwitchableControllerActions($pluginConfiguration, $this->configuration['switchableControllerActions']);
151 }
152 } else {
153 $pluginConfiguration = $this->getPluginConfiguration($extensionName, $pluginName);
154 $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($extensionName, $pluginName);
155 }
156 $frameworkConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($frameworkConfiguration, $pluginConfiguration);
157 // only load context specific configuration when retrieving configuration of the current plugin
158 if ($extensionName === NULL || $extensionName === $this->extensionName && $pluginName === $this->pluginName) {
159 $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
160 }
161
162 if (!empty($frameworkConfiguration['persistence']['storagePid'])) {
163 if (is_array($frameworkConfiguration['persistence']['storagePid'])) {
164 /**
165 * We simulate the frontend to enable the use of cObjects in
166 * stdWrap. Than we convert the configuration to normal TypoScript
167 * and apply the stdWrap to the storagePid
168 */
169 if (!$this->environmentService->isEnvironmentInFrontendMode()) {
170 \TYPO3\CMS\Extbase\Utility\FrontendSimulatorUtility::simulateFrontendEnvironment($this->getContentObject());
171 }
172 $conf = $this->typoScriptService->convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']);
173 $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrap($conf['storagePid'], $conf['storagePid.']);
174 if (!$this->environmentService->isEnvironmentInFrontendMode()) {
175 \TYPO3\CMS\Extbase\Utility\FrontendSimulatorUtility::resetFrontendEnvironment();
176 }
177 }
178
179 if (!empty($frameworkConfiguration['persistence']['recursive'])) {
180 $frameworkConfiguration['persistence']['storagePid'] = $this->getRecursiveStoragePids($frameworkConfiguration['persistence']['storagePid'], (int) $frameworkConfiguration['persistence']['recursive']);
181 }
182 }
183 // 1st level cache
184 $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
185 return $frameworkConfiguration;
186 }
187
188 /**
189 * Returns the TypoScript configuration found in config.tx_extbase
190 *
191 * @return array
192 */
193 protected function getExtbaseConfiguration() {
194 $setup = $this->getTypoScriptSetup();
195 $extbaseConfiguration = array();
196 if (isset($setup['config.']['tx_extbase.'])) {
197 $extbaseConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['config.']['tx_extbase.']);
198 }
199 return $extbaseConfiguration;
200 }
201
202 /**
203 * Returns the default backend storage pid
204 *
205 * @return string
206 */
207 public function getDefaultBackendStoragePid() {
208 return self::DEFAULT_BACKEND_STORAGE_PID;
209 }
210
211 /**
212 * @param array &$frameworkConfiguration
213 * @param array $switchableControllerActions
214 * @return void
215 */
216 protected function overrideSwitchableControllerActions(array &$frameworkConfiguration, array $switchableControllerActions) {
217 $overriddenSwitchableControllerActions = array();
218 foreach ($switchableControllerActions as $controllerName => $actions) {
219 if (!isset($frameworkConfiguration['controllerConfiguration'][$controllerName])) {
220 continue;
221 }
222 $overriddenSwitchableControllerActions[$controllerName] = array('actions' => $actions);
223 $nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'];
224 if (!is_array($nonCacheableActions)) {
225 // There are no non-cacheable actions, thus we can directly continue
226 // with the next controller name.
227 continue;
228 }
229 $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
230 if (!empty($overriddenNonCacheableActions)) {
231 $overriddenSwitchableControllerActions[$controllerName]['nonCacheableActions'] = $overriddenNonCacheableActions;
232 }
233 }
234 $frameworkConfiguration['controllerConfiguration'] = $overriddenSwitchableControllerActions;
235 }
236
237 /**
238 * The context specific configuration returned by this method
239 * will override the framework configuration which was
240 * obtained from TypoScript. This can be used f.e. to override the storagePid
241 * with the value set inside the Plugin Instance.
242 *
243 * WARNING: Make sure this method ALWAYS returns an array!
244 *
245 * @param array $frameworkConfiguration The framework configuration until now
246 * @return array context specific configuration which will override the configuration obtained by TypoScript
247 */
248 abstract protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration);
249
250 /**
251 * Returns TypoScript Setup array from current Environment.
252 *
253 * @return array the TypoScript setup
254 */
255 abstract public function getTypoScriptSetup();
256
257 /**
258 * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin / module.tx_yourextension_yourmodule
259 * merged with the global configuration of your extension from plugin.tx_yourextension / module.tx_yourextension
260 *
261 * @param string $extensionName
262 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
263 * @return array
264 */
265 abstract protected function getPluginConfiguration($extensionName, $pluginName = NULL);
266
267 /**
268 * Returns the configured controller/action pairs of the specified plugin/module in the format
269 * array(
270 * 'Controller1' => array('action1', 'action2'),
271 * 'Controller2' => array('action3', 'action4')
272 * )
273 *
274 * @param string $extensionName
275 * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
276 * @return array
277 */
278 abstract protected function getSwitchableControllerActions($extensionName, $pluginName);
279
280 /**
281 * The implementation of the methods to return a list of storagePid that are below a certain
282 * storage pid.
283 *
284 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
285 * @param integer $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
286 * @return string storage PIDs
287 */
288 abstract protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0);
289
290 }
291
292 ?>