[+FEATURE] Extbase (Configuration): Allow StoragePid to be manually defined by TS
authorSebastian Kurfürst <sebastian@typo3.org>
Thu, 13 Jan 2011 19:25:21 +0000 (19:25 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Thu, 13 Jan 2011 19:25:21 +0000 (19:25 +0000)
In certain cases, it is helpful to allow the storage PID
to be defined by TypoScript, so one could use stdWrap for example.
With this patch, this is possible.

Even better would be a more general solution, allowing processing
of arbitrary TS configuration with typoScript; but that's at the
moment not possible.

Credits go to Rens Admiraal for providing the patch!

Resolves: #8397

typo3/sysext/extbase/Classes/Configuration/AbstractConfigurationManager.php
typo3/sysext/extbase/Classes/Utility/FrontendSimulator.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Configuration/AbstractConfigurationManagerTest.php

index 98b20b7..fcf257d 100644 (file)
@@ -158,6 +158,19 @@ abstract class Tx_Extbase_Configuration_AbstractConfigurationManager implements
                        $frameworkConfiguration = $this->getContextSpecificFrameworkConfiguration($frameworkConfiguration);
                }
 
+               if (!empty($frameworkConfiguration['persistence']['storagePid']) &&
+                       is_array($frameworkConfiguration['persistence']['storagePid'])) {
+                               /**
+                                * We simulate the frontend to enable the use of cObjects in
+                                * stdWrap. Than we convert the configuration to normal TypoScript
+                                * and apply the stdWrap to the storagePid
+                                */
+                       Tx_Extbase_Utility_FrontendSimulator::simulateFrontendEnvironment($this->getContentObject());
+                       $conf = Tx_Extbase_Utility_TypoScript::convertPlainArrayToTypoScriptArray($frameworkConfiguration['persistence']);
+                       $frameworkConfiguration['persistence']['storagePid'] = $GLOBALS['TSFE']->cObj->stdWrap($conf['storagePid'], $conf['storagePid.']);
+                       Tx_Extbase_Utility_FrontendSimulator::resetFrontendEnvironment();
+               }
+
                // 1st level cache
                $this->configurationCache[$configurationCacheKey] = $frameworkConfiguration;
                return $frameworkConfiguration;
diff --git a/typo3/sysext/extbase/Classes/Utility/FrontendSimulator.php b/typo3/sysext/extbase/Classes/Utility/FrontendSimulator.php
new file mode 100644 (file)
index 0000000..77b1f1e
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2010 Rens Admiraal <rens@rensnel.nl>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Utilities to simulate a frontend in backend context.
+ *
+ * ONLY USED INTERNALLY, MIGHT CHANGE WITHOUT NOTICE!
+ *
+ * @package Extbase
+ * @subpackage Utility
+ * @version $ID:$
+ */
+class Tx_Extbase_Utility_FrontendSimulator {
+
+       /**
+        * @var mixed
+        */
+       protected static $tsfeBackup;
+
+       /**
+        * Sets the $TSFE->cObjectDepthCounter in Backend mode
+        * This somewhat hacky work around is currently needed because the cObjGetSingle() function of tslib_cObj relies on this setting
+        *
+        * @param tslib_cObj $addCObj
+        * @return void
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public static function simulateFrontendEnvironment(tslib_cObj $cObj = NULL) {
+               self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
+               $GLOBALS['TSFE'] = new stdClass();
+               $GLOBALS['TSFE']->cObjectDepthCounter = 100;
+
+               $GLOBALS['TSFE']->cObj = $cObj !== NULL ? $cObj : t3lib_div::makeInstance('tslib_cObj');
+       }
+
+       /**
+        * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
+        *
+        * @return void
+        * @author Bastian Waidelich <bastian@typo3.org>(
+        * @see simulateFrontendEnvironment()
+        */
+       public static function resetFrontendEnvironment() {
+               if (!empty(self::$tsfeBackup)) {
+                       $GLOBALS['TSFE'] = self::$tsfeBackup;
+               }
+       }
+}
+?>
\ No newline at end of file
index 46e2226..3a06f95 100644 (file)
@@ -416,5 +416,48 @@ class Tx_Extbase_Tests_Unit_Configuration_AbstractConfigurationManagerTest exten
                $actualResult = $mergedConfiguration['controllerConfiguration'];
                $this->assertEquals($expectedResult, $actualResult);
        }
+
+       /**
+        * @test
+        */
+       public function setStoragePidWithStdWrap() {
+
+               unset($this->abstractConfigurationManager);
+               $this->abstractConfigurationManager = $this->getMock('Tx_Extbase_Configuration_AbstractConfigurationManager', array('getSwitchableControllerActions', 'getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getPluginConfiguration'));
+
+               $pluginConfiguration = $this->testPluginConfiguration;
+               $pluginConfiguration['persistence']['storagePid'] = array(
+                       'cObject' => array(
+                               'value' => '8,3',
+                               '_typoScriptNodeValue' => 'TEXT'
+                       )
+               );
+
+               $this->abstractConfigurationManager->expects($this->once())->method('getTypoScriptSetup')->will($this->returnValue($this->testTypoScriptSetup));
+               $this->abstractConfigurationManager->expects($this->once())->method('getPluginConfiguration')->with('CurrentExtensionName', 'CurrentPluginName')->will($this->returnValue($pluginConfiguration));
+               $this->abstractConfigurationManager->expects($this->once())->method('getSwitchableControllerActions')->with('CurrentExtensionName', 'CurrentPluginName')->will($this->returnValue(NULL));
+
+               $expectedResult = array(
+                       'settings' => array(
+                               'setting1' => 'overriddenValue1',
+                               'setting2' => 'value2',
+                               'setting3' => 'additionalValue',
+                       ),
+                       'view' => array(
+                               'viewSub' => array(
+                                       'key1' => 'overridden',
+                                       'key2' => 'value2',
+                                       'key3' => 'new key',
+                               )
+                       ),
+                       'persistence' => array(
+                               'storagePid' => '8,3',
+                       ),
+                       'controllerConfiguration' => NULL
+               );
+
+               $actualResult = $this->abstractConfigurationManager->getConfiguration('CurrentExtensionName', 'CurrentPluginName');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
 }
 ?>
\ No newline at end of file