[+FEATURE] Extbase (MVC): Tx_Extbase_MVC_Web_Routing_UriBuilder::reset() now returns...
authorBastian Waidelich <bastian@typo3.org>
Fri, 4 Sep 2009 16:05:43 +0000 (16:05 +0000)
committerBastian Waidelich <bastian@typo3.org>
Fri, 4 Sep 2009 16:05:43 +0000 (16:05 +0000)
[+FEATURE] Extbase (MVC): Added Tx_Extbase_MVC_Web_Routing_UriBuilder::setFormat(). This allows you to specify the format of the target (e.g. <f:link.action format="xml" />). Some smaller tweaks in UriBuilder

typo3/sysext/extbase/Classes/MVC/Controller/AbstractController.php
typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php
typo3/sysext/extbase/Tests/MVC/Controller/AbstractController_testcase.php
typo3/sysext/extbase/Tests/MVC/Web/Routing/UriBuilder_testcase.php

index d305999..9295dcb 100755 (executable)
@@ -263,9 +263,10 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
                        $pageUid = $GLOBALS['TSFE']->id;
                }
 
-               $this->uriBuilder->reset();
-               $this->uriBuilder->setTargetPageUid($pageUid);
-               $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName);
+               $uri = $this->uriBuilder
+                       ->reset()
+                       ->setTargetPageUid($pageUid)
+                       ->uriFor($actionName, $arguments, $controllerName, $extensionName);
                $this->redirectToURI($uri, $delay, $statusCode);
        }
 
index 268fb66..630a5d3 100644 (file)
@@ -83,6 +83,10 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
         */
        protected $useCacheHash = TRUE;
 
+       /**
+        * @var string
+        */
+       protected $format = '';
 
        /**
         * Constructs this URI Helper
@@ -127,7 +131,7 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
 
        /**
         * @return array
-        * #@api
+        * @api
         */
        public function getArguments() {
                return $this->arguments;
@@ -154,6 +158,26 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
        }
 
        /**
+        * Specifies the format of the target (e.g. "html" or "xml")
+        *
+        * @param string $section
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setFormat($format) {
+               $this->format = $format;
+               return $this;
+       }
+
+       /**
+        * @return string
+        * @api
+        */
+       public function getFormat() {
+               return $this->format;
+       }
+
+       /**
         * If set, the URI is prepended with the current base URI. Defaults to FALSE.
         *
         * @param boolean $createAbsoluteUri
@@ -322,16 +346,38 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
        }
 
        /**
-        * Creates an URI by making use of the typolink mechanism.
+        * Resets all UriBuilder options to their default value
+        *
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function reset() {
+               $this->arguments = array();
+               $this->section = '';
+               $this->createAbsoluteUri = FALSE;
+               $this->addQueryString = FALSE;
+               $this->argumentsToBeExcludedFromQueryString = array();
+               $this->linkAccessRestrictedPages = FALSE;
+               $this->targetPageUid = NULL;
+               $this->targetPageType = 0;
+               $this->noCache = FALSE;
+               $this->useCacheHash = TRUE;
+
+               return $this;
+       }
+
+       /**
+        * Creates an URI used for linking to an Extbase action.
+        * Works in Frondend and Backend mode of TYPO3.
         *
-        * @param integer $pageUid uid of the target page
         * @param string $actionName Name of the action to be called
-        * @param array $controllerArguments Additional query parameters, will be "namespaced"
-        * @param string $controllerName Name of the target controller
-        * @param string $extensionName Name of the target extension, without underscores. If NULL current ExtensionName is used.
-        * @param string $pluginName Name of the target plugin.  If NULL current PluginName is used.
-        * @return string the typolink URI
+        * @param array $controllerArguments Additional query parameters. Will be "namespaced" and merged with $this->arguments.
+        * @param string $controllerName Name of the target controller. If not set, current ControllerName is used.
+        * @param string $extensionName Name of the target extension, without underscores. If not set, current ExtensionName is used.
+        * @param string $pluginName Name of the target plugin. If not set, current PluginName is used.
+        * @return string the rendered URI
         * @api
+        * @see build()
         */
        public function uriFor($actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL) {
                if ($actionName !== NULL) {
@@ -348,8 +394,11 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
                if ($pluginName === NULL) {
                        $pluginName = $this->request->getPluginName();
                }
+               if ($this->format !== '') {
+                       $controllerArguments['format'] = $this->format;
+               }
                $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
-               $prefixedControllerArguments = (count($controllerArguments) > 0) ? array($argumentPrefix => $controllerArguments) : array();
+               $prefixedControllerArguments = array($argumentPrefix => $controllerArguments);
                $this->arguments = t3lib_div::array_merge_recursive_overrule($this->arguments, $prefixedControllerArguments);
 
                return $this->build();
@@ -417,24 +466,6 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
                return $uri;
        }
 
-       /**
-        * Resets all UriBuilder options to their default value
-        *
-        * @return void
-        * @api
-        */
-       public function reset() {
-               $this->arguments = array();
-               $this->section = '';
-               $this->createAbsoluteUri = FALSE;
-               $this->addQueryString = FALSE;
-               $this->argumentsToBeExcludedFromQueryString = array();
-               $this->linkAccessRestrictedPages = FALSE;
-               $this->targetPageUid = NULL;
-               $this->targetPageType = 0;
-               $this->noCache = FALSE;
-               $this->useCacheHash = TRUE;
-       }
 
        /**
         * Builds a TypoLink configuration array from the current settings
index d2662d1..3671b35 100644 (file)
@@ -104,7 +104,8 @@ class Tx_Extbase_MVC_Controller_AbstractController_testcase extends Tx_Extbase_B
                $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
 
                $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
-               $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123);
+               $mockUriBuilder->expects($this->once())->method('reset')->will($this->returnValue($mockUriBuilder));
+               $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123)->will($this->returnValue($mockUriBuilder));
                $mockUriBuilder->expects($this->once())->method('uriFor')->with('theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
 
                $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
index 49f0a12..88fd8ab 100644 (file)
@@ -75,8 +75,10 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder_testcase extends Tx_Extbase_Base_tes
         */
        public function settersAndGettersWorkAsExpected() {
                $this->uriBuilder
+                       ->reset()
                        ->setArguments(array('test' => 'arguments'))
                        ->setSection('testSection')
+                       ->setFormat('testFormat')
                        ->setCreateAbsoluteUri(TRUE)
                        ->setAddQueryString(TRUE)
                        ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
@@ -88,6 +90,7 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder_testcase extends Tx_Extbase_Base_tes
 
                $this->assertEquals(array('test' => 'arguments'), $this->uriBuilder->getArguments());
                $this->assertEquals('testSection', $this->uriBuilder->getSection());
+               $this->assertEquals('testFormat', $this->uriBuilder->getFormat());
                $this->assertEquals(TRUE, $this->uriBuilder->getCreateAbsoluteUri());
                $this->assertEquals(TRUE, $this->uriBuilder->getAddQueryString());
                $this->assertEquals(array('test' => 'addQueryStringExcludeArguments'), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());