TYPO3 API  SVNRelease
AbstractConfigurationManager.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * Abstract base class for a general purpose configuration manager
00027  *
00028  * @package Extbase
00029  * @subpackage Configuration
00030  * @version $ID:$
00031  */
00032 abstract class Tx_Extbase_Configuration_AbstractConfigurationManager implements t3lib_Singleton {
00033 
00034     /**
00035      * Default backend storage PID
00036      */
00037     const DEFAULT_BACKEND_STORAGE_PID = 0;
00038 
00039     /**
00040      * Storage of the raw TypoScript configuration
00041      * @var array
00042      */
00043     protected $configuration = array();
00044 
00045     /**
00046      * @var tslib_cObj
00047      */
00048     protected $contentObject;
00049 
00050     /**
00051      * @var Tx_Extbase_Object_ObjectManagerInterface
00052      */
00053     protected $objectManager;
00054 
00055     /**
00056      * name of the extension this Configuration Manager instance belongs to
00057      * @var string
00058      */
00059     protected $extensionName;
00060 
00061     /**
00062      * name of the plugin this Configuration Manager instance belongs to
00063      * @var string
00064      */
00065     protected $pluginName;
00066 
00067     /**
00068      * 1st level configuration cache
00069      *
00070      * @var array
00071      */
00072     protected $configurationCache = array();
00073 
00074     /**
00075      * @param Tx_Extbase_Object_ManagerInterface $objectManager
00076      * @return void
00077      */
00078     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00079         $this->objectManager = $objectManager;
00080     }
00081 
00082     /**
00083      * @param tslib_cObj $contentObject
00084      * @return void
00085      */
00086     public function setContentObject(tslib_cObj $contentObject = NULL) {
00087         $this->contentObject = $contentObject;
00088     }
00089 
00090     /**
00091      * @return tslib_cObj
00092      */
00093     public function getContentObject() {
00094         return $this->contentObject;
00095     }
00096 
00097     /**
00098      * Sets the specified raw configuration coming from the outside.
00099      * Note that this is a low level method and only makes sense to be used by Extbase internally.
00100      *
00101      * @param array $configuration The new configuration
00102      * @return void
00103      */
00104     public function setConfiguration(array $configuration = array()) {
00105         // reset 1st level cache
00106         $this->configurationCache = array();
00107 
00108         $this->extensionName = $configuration['extensionName'];
00109         $this->pluginName = $configuration['pluginName'];
00110         $this->configuration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($configuration);
00111     }
00112 
00113     /**
00114      * Loads the Extbase Framework configuration.
00115      *
00116      * The Extbase framework configuration HAS TO be retrieved using this method, as they are come from different places than the normal settings.
00117      * Framework configuration is, in contrast to normal settings, needed for the Extbase framework to operate correctly.
00118      *
00119      * @param string $extensionName if specified, the configuration for the given extension will be returned (plugin.tx_extensionname)
00120      * @param string $pluginName if specified, the configuration for the given plugin will be returned (plugin.tx_extensionname_pluginname)
00121      * @return array the Extbase framework configuration
00122      */
00123     public function getConfiguration($extensionName = NULL, $pluginName = NULL) {
00124         // 1st level cache
00125         if ($extensionName !== NULL) {
00126             if ($pluginName === NULL) {
00127                 throw new Tx_Extbase_Configuration_Exception('You\'ll have to specify either both, extensionName and pluginName, or neither.', 1289852422);
00128             }
00129             $configurationCacheKey = strtolower($extensionName . '_' . $pluginName);
00130         } else {
00131             $configurationCacheKey = strtolower($this->extensionName . '_' . $this->pluginName);
00132         }
00133         if (isset($this->configurationCache[$configurationCacheKey])) {
00134             return $this->configurationCache[$configurationCacheKey];
00135         }
00136 
00137         $frameworkConfiguration = $this->getExtbaseConfiguration();
00138         if (!isset($frameworkConfiguration['persistence']['storagePid'])) {
00139             $frameworkConfiguration['persistence']['storagePid'] = self::DEFAULT_BACKEND_STORAGE_PID;
00140         }
00141 
00142         // only merge $this->configuration and override switchableControllerActions when retrieving configuration of the current plugin
00143         if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
00144             $pluginConfiguration = $this->getPluginConfiguration($this->extensionName, $this->pluginName);
00145             $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, $this->configuration);
00146             $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($this->extensionName, $this->pluginName);
00147             if (isset($this->configuration['switchableControllerActions'])) {
00148                 $this->overrideSwitchableControllerActions($pluginConfiguration, $this->configuration['switchableControllerActions']);
00149             }
00150         } else {
00151             $pluginConfiguration = $this->getPluginConfiguration($extensionName, $pluginName);
00152             $pluginConfiguration['controllerConfiguration'] = $this->getSwitchableControllerActions($extensionName, $pluginName);
00153         }
00154         $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, $pluginConfiguration);
00155 
00156         // only load context specific configuration when retrieving configuration of the current plugin
00157         if ($extensionName === NULL || ($extensionName === $this->extensionName && $pluginName === $this->pluginName)) {
00158             $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
00159         }
00160 
00161         if (!empty($frameworkConfiguration['persistence']['storagePid']) &&
00162             is_array($frameworkConfiguration['persistence']['storagePid'])) {
00163                 /**
00164                  * We simulate the frontend to enable the use of cObjects in
00165                  * stdWrap. Than we convert the configuration to normal TypoScript
00166                  * and apply the stdWrap to the storagePid
00167                  */
00168             Tx_Extbase_Utility_FrontendSimulator::simulateFrontendEnvironment($this->getContentObject());
00169             $conf = Tx_Extbase_Utility_TypoScript::convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']);
00170             $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrap($conf['storagePid'], $conf['storagePid.']);
00171             Tx_Extbase_Utility_FrontendSimulator::resetFrontendEnvironment();
00172         }
00173 
00174         // 1st level cache
00175         $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
00176         return $frameworkConfiguration;
00177     }
00178 
00179     /**
00180      * Returns the TypoScript configuration found in config.tx_extbase
00181      *
00182      * @return array
00183      */
00184     protected function getExtbaseConfiguration() {
00185         $setup = $this->getTypoScriptSetup();
00186         $extbaseConfiguration = array();
00187         if (isset($setup['config.']['tx_extbase.'])) {
00188             $extbaseConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['config.']['tx_extbase.']);
00189         }
00190         return $extbaseConfiguration;
00191     }
00192 
00193     /**
00194      * @param array $frameworkConfiguration
00195      * @param array $overriddenSwitchableControllerActions in the format array('Controller1' => array('action1', 'action2'), 'Controller2' => ...)
00196      * @return void
00197      */
00198     protected function overrideSwitchableControllerActions(array &$frameworkConfiguration, array $switchableControllerActions) {
00199         $overriddenSwitchableControllerActions = array();
00200         foreach ($switchableControllerActions as $controllerName => $actions) {
00201             if (!isset($frameworkConfiguration['controllerConfiguration'][$controllerName])) {
00202                 continue;
00203             }
00204             $overriddenSwitchableControllerActions[$controllerName] = array('actions' => $actions);
00205             $nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'];
00206 
00207             if (!is_array($nonCacheableActions)) {
00208                 // There are no non-cacheable actions, thus we can directly continue
00209                 // with the next controller name.
00210                 continue;
00211             }
00212 
00213             $overriddenNonCacheableActions = array_intersect($nonCacheableActions, $actions);
00214             if (!empty($overriddenNonCacheableActions)) {
00215                 $overriddenSwitchableControllerActions[$controllerName]['nonCacheableActions'] = $overriddenNonCacheableActions;
00216             }
00217         }
00218         $frameworkConfiguration['controllerConfiguration'] = $overriddenSwitchableControllerActions;
00219     }
00220 
00221     /**
00222      * The context specific configuration returned by this method
00223      * will override the framework configuration which was
00224      * obtained from TypoScript. This can be used f.e. to override the storagePid
00225      * with the value set inside the Plugin Instance.
00226      *
00227      * WARNING: Make sure this method ALWAYS returns an array!
00228      *
00229      * @param array $frameworkConfiguration The framework configuration until now
00230      * @return array context specific configuration which will override the configuration obtained by TypoScript
00231      */
00232     abstract protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration);
00233 
00234     /**
00235      * Returns TypoScript Setup array from current Environment.
00236      *
00237      * @return array the TypoScript setup
00238      */
00239     abstract protected function getTypoScriptSetup();
00240 
00241     /**
00242      * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin / module.tx_yourextension_yourmodule
00243      * merged with the global configuration of your extension from plugin.tx_yourextension / module.tx_yourextension
00244      *
00245      * @param string $extensionName
00246      * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
00247      * @return array
00248      */
00249     abstract protected function getPluginConfiguration($extensionName, $pluginName);
00250 
00251     /**
00252      * Returns the configured controller/action pairs of the specified plugin/module in the format
00253      * array(
00254      *  'Controller1' => array('action1', 'action2'),
00255      *  'Controller2' => array('action3', 'action4')
00256      * )
00257      *
00258      * @param string $extensionName
00259      * @param string $pluginName in FE mode this is the specified plugin name, in BE mode this is the full module signature
00260      * @return array
00261      */
00262     abstract protected function getSwitchableControllerActions($extensionName, $pluginName);
00263 }
00264 ?>