[+FEATURE] Extbase (MVC): Configurable plugin namespace
authorBastian Waidelich <bastian@typo3.org>
Thu, 23 Sep 2010 16:41:04 +0000 (16:41 +0000)
committerBastian Waidelich <bastian@typo3.org>
Thu, 23 Sep 2010 16:41:04 +0000 (16:41 +0000)
until now the namespace (aka prefix) of Extbase plugins was
fixed (tx_extensionname_pluginname). This is now configurable
via TypoScript. Just write:
plugin.tx_extensionname_pluginname.view.pluginNamespace = my_custom_namespace
to change the prefix for a specific plugin or
plugin.tx_extensionname.view.pluginNamespace = my_custom_namespace
to change if for the whole extension.

Note: This feature still has to be documented!

Resolves: #8365

typo3/sysext/extbase/Classes/MVC/Web/RequestBuilder.php
typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php
typo3/sysext/extbase/Classes/Utility/Extension.php
typo3/sysext/extbase/Tests/Fixtures/Dispatcher.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/Utility/Extension_testcase.php

index e0152af..a7cb33a 100644 (file)
@@ -108,7 +108,9 @@ class Tx_Extbase_MVC_Web_RequestBuilder {
         * @return Tx_Extbase_MVC_Web_Request The web request as an object
         */
        public function build() {
-               $parameters = t3lib_div::_GPmerged('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginName));
+               $pluginSignature = strtolower($this->extensionName . '_' . $this->pluginName);
+               $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
+               $parameters = t3lib_div::_GPmerged($pluginNamespace);
 
                if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
                        $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
index 0349c60..5402173 100644 (file)
@@ -416,13 +416,14 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
                if ($this->format !== '') {
                        $controllerArguments['format'] = $this->format;
                }
-               $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
-               $prefixedControllerArguments = array($argumentPrefix => $controllerArguments);
+               $pluginSignature = strtolower($extensionName . '_' . $pluginName);
+               $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
+               $prefixedControllerArguments = array($pluginNamespace => $controllerArguments);
                $this->arguments = t3lib_div::array_merge_recursive_overrule($this->arguments, $prefixedControllerArguments);
 
                return $this->build();
        }
-       
+
        /**
         * Builds the URI
         * Depending on the current context this calls buildBackendUri() or buildFrontendUri()
@@ -561,11 +562,11 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
                }
                return $arguments;
        }
-       
+
        /**
         * Converts a given object recursively into an array.
         *
-        * @param Tx_Extbase_DomainObject_AbstractDomainObject $object 
+        * @param Tx_Extbase_DomainObject_AbstractDomainObject $object
         * @return void
         */
        // TODO Refactore this into convertDomainObjectsToIdentityArrays()
index c4090b5..461cc2e 100644 (file)
@@ -458,5 +458,27 @@ tt_content.list.20.' . $pluginSignature . ' {
                return $returnValue;
        }
 
+       /**
+        * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionName]_[pluginName]")
+        * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
+        * If pluginNamespace is not specified "tx_[extensionName]_[pluginName]" is returned.
+        *
+        * @param string $pluginSignature Plugin signature: strtolower($extensionName) . '_' . strtolower($pluginName)
+        * @return string plugin namespace
+        */
+       public static function getPluginNamespaceByPluginSignature($pluginSignature) {
+               $defaultPluginNamespace = 'tx_' . $pluginSignature;
+               $configurationManager = Tx_Extbase_Dispatcher::getConfigurationManager();
+               if (!isset($configurationManager) || !isset($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']) || !is_array($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'])) {
+                       return $defaultPluginNamespace;
+               }
+               $pluginConfiguration = $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'][$pluginSignature . '.'];
+               $frameworkConfiguration = $configurationManager->getFrameworkConfiguration($pluginConfiguration);
+               if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
+                       return $defaultPluginNamespace;
+               }
+               return $frameworkConfiguration['view']['pluginNamespace'];
+       }
+
 }
 ?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Tests/Fixtures/Dispatcher.php b/typo3/sysext/extbase/Tests/Fixtures/Dispatcher.php
new file mode 100644 (file)
index 0000000..59145ff
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
+*  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!
+***************************************************************/
+
+/**
+ * Creates a request an dispatches it to the controller which was specified
+ * by TS Setup, Flexform and returns the content to the v4 framework.
+ *
+ * This class is the main entry point for extbase extensions.
+ *
+ * @package Extbase
+ */
+class Tx_Extbase_Tests_Fixtures_Dispatcher extends Tx_Extbase_Dispatcher {
+
+       /**
+        * sets the configuration manager
+        */
+       public function setConfigurationManager(Tx_Extbase_Configuration_AbstractConfigurationManager $configurationManager) {
+               parent::$configurationManager = $configurationManager;
+       }
+
+}
+?>
\ No newline at end of file
index 0ac3ead..9b31971 100644 (file)
@@ -22,6 +22,7 @@
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
 
+require_once (t3lib_extMgm::extPath('extbase') . 'Tests/Fixtures/Dispatcher.php');
 
 /**
  * Testcase for class Tx_Extbase_Utility_Extension
@@ -59,6 +60,57 @@ class Tx_Extbase_Utility_Extension_testcase extends tx_phpunit_testcase {
                if (!isset($GLOBALS['TSFE']->tmpl->setup)) {
                        $GLOBALS['TSFE']->tmpl->setup = array();
                }
+               $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'] = array(
+                       '9' => 'CASE',
+                       '9.' => array(
+                               'key.' => array(
+                                       'field' => 'layout'),
+                                       0 => '< plugin.tt_news'
+                               ),
+                       'someextension_someplugin' => 'USER',
+                       'someextension_someplugin.' => array(
+                               'userFunc' => 'tx_extbase_dispatcher->dispatch',
+                               'pluginName' => 'SomePlugin',
+                               'extensionName' => 'SomeExtension',
+                               'controller' => 'ControllerName',
+                               'action' => 'index',
+                               'switchableControllerActions.' => array(
+                                       'ControllerName.' => array(
+                                               'actions' => 'index,otherAction',
+                                       ),
+                               ),
+                       ),
+                       'someotherextensionname_secondplugin' => 'USER',
+                       'someotherextensionname_secondplugin.' => array(
+                               'userFunc' => 'tx_extbase_dispatcher->dispatch',
+                               'pluginName' => 'SecondPlugin',
+                               'extensionName' => 'SomeOtherExtensionName',
+                               'controller' => 'ControllerName',
+                               'action' => 'index',
+                               'switchableControllerActions.' => array(
+                                       'ControllerName.' => array(
+                                               'actions' => 'index,otherAction',
+                                       ),
+                                       'SecondControllerName.' => array(
+                                               'actions' => 'someAction,someOtherAction',
+                                               'nonCacheableActions' => 'someOtherAction',
+                                       ),
+                               ),
+                       ),
+                       'extensionname_thirdplugin' => 'USER',
+                       'extensionname_thirdplugin.' => array(
+                               'userFunc' => 'tx_extbase_dispatcher->dispatch',
+                               'pluginName' => 'ThirdPlugin',
+                               'extensionName' => 'ExtensionName',
+                               'controller' => 'ControllerName',
+                               'action' => 'index',
+                               'switchableControllerActions.' => array(
+                                       'FirstController.' => array(
+                                               'actions' => 'otherAction,thirdAction',
+                                       ),
+                               ),
+                       ),
+               );
        }
 
        public function tearDown() {
@@ -304,6 +356,43 @@ tt_content.list.20.myextension_pi1 {', $staticTypoScript);
 ', $staticTypoScript);
        }
 
+       /**
+        * DataProvider for getPluginNamespaceByPluginSignatureTests()
+        *
+        * @return array
+        */
+       public function getPluginNamespaceByPluginSignatureDataProvider() {
+               return array(
+                       array('someextension_someplugin', 'tx_someextension_someplugin'),
+                       array('nonexistingextension_someplugin', 'tx_nonexistingextension_someplugin'),
+                       array('InvalidPluginNamespace', 'tx_InvalidPluginNamespace'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider getPluginNamespaceByPluginSignatureDataProvider
+        */
+       public function getPluginNamespaceByPluginSignatureTests($pluginSignature, $expectedResult) {
+               $dispatcher = new Tx_Extbase_Tests_Fixtures_Dispatcher();
+               $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_AbstractConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'loadTypoScriptSetup'));
+               $dispatcher->setConfigurationManager($mockConfigurationManager);
+               $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
+               $this->assertEquals($expectedResult, $actualResult, 'Failing for $pluginSignature: "' . $pluginSignature . '"');
+       }
+
+       /**
+        * @test
+        */
+       public function pluginNamespaceCanBeOverridden() {
+               $dispatcher = new Tx_Extbase_Tests_Fixtures_Dispatcher();
+               $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_AbstractConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'loadTypoScriptSetup', 'getFrameworkConfiguration'));
+               $mockConfigurationManager->expects($this->once())->method('getFrameworkConfiguration')->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
+               $dispatcher->setConfigurationManager($mockConfigurationManager);
+               $expectedResult = 'overridden_plugin_namespace';
+               $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature('somePluginSignature');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
 }
 
 ?>
\ No newline at end of file