ExtBase:
authorJochen Rau <j.rau@web.de>
Fri, 3 Apr 2009 15:00:28 +0000 (15:00 +0000)
committerJochen Rau <j.rau@web.de>
Fri, 3 Apr 2009 15:00:28 +0000 (15:00 +0000)
* Revised RequestBuilder and Request
* Added Unit Tests for the Request Builder

typo3/sysext/extbase/Classes/MVC/Request.php
typo3/sysext/extbase/Classes/MVC/Web/Request.php
typo3/sysext/extbase/Classes/MVC/Web/RequestBuilder.php
typo3/sysext/extbase/Tests/MVC/Web/RequestBuilder_testcase.php [new file with mode: 0644]
typo3/sysext/extbase/class.tx_extbase_dispatcher.php

index c1b3b37..a2d893c 100644 (file)
@@ -72,11 +72,6 @@ class Tx_ExtBase_MVC_Request {
        protected $arguments = array();
 
        /**
-        * @var string The requested representation format
-        */
-       protected $format = 'txt';
-
-       /**
         * @var boolean If this request has been changed and needs to be dispatched again
         */
        protected $dispatched = FALSE;
@@ -179,16 +174,10 @@ class Tx_ExtBase_MVC_Request {
         * @return mixed Either the view object name or FALSE
         */
        public function getViewObjectName() {
-               $possibleViewName = $this->viewObjectNamePattern;
-               $possibleViewName = str_replace('@extension', $this->extensionName, $possibleViewName);
-               $possibleViewName = str_replace('@controller', $this->controllerName, $possibleViewName);
-               $possibleViewName = str_replace('@action', ucfirst($this->controllerActionName), $possibleViewName);
-
-               $viewObjectName = $possibleViewName;
-               // $viewObjectName = str_replace('@format', $this->format, $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', $this->format, $possibleViewName)); // TODO
-               // if ($viewObjectName === FALSE) {
-               //      $viewObjectName = str_replace('@format', '', $possibleViewName); //$this->objectManager->getCaseSensitiveObjectName(str_replace('@format', '', $possibleViewName));
-               // }
+               $viewObjectName = $this->viewObjectNamePattern;
+               $viewObjectName = str_replace('@extension', $this->extensionName, $viewObjectName);
+               $viewObjectName = str_replace('@controller', $this->controllerName, $viewObjectName);
+               $viewObjectName = str_replace('@action', ucfirst($this->controllerActionName), $viewObjectName);
                return $viewObjectName;
        }
 
index 4a6dc3f..19a5bd6 100755 (executable)
  * @scope prototype
  */
 class Tx_ExtBase_MVC_Web_Request extends Tx_ExtBase_MVC_Request {
-// TODO Add a method to get a plugin id; useful to distinguish between several plugins on one page using the same controller
-       /**
-        * @var string The requested representation format
-        */
-       protected $format = 'html';
 
        /**
         * @var string Contains the request method
index 5df143e..0150fd4 100755 (executable)
  * @scope prototype
  */
 class Tx_ExtBase_MVC_Web_RequestBuilder {
+       
+       /**
+        * This is a unique key for a plugin (not the extension key!)
+        *
+        * @var string
+        **/
+       protected $pluginKey = 'plugin';
+       
+       /**
+        * The name of the extension (in UpperCamelCase)
+        *
+        * @var string
+        **/
+       protected $extensionName = 'ExtBase';
+       
+       /**
+        * The default controller name
+        *
+        * @var string
+        **/
+       protected $defaultControllerName = 'Default';
+       
+       /**
+        * The default action of the default controller
+        *
+        * @var string
+        **/
+       protected $defaultActionName = 'index';
 
        /**
-        * Builds a web request object from the raw HTTP information
+        * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
         *
-        * @return \F3\FLOW3\MVC\Web\Request The web request as an object
-        * @author Robert Lemke <robert@typo3.org>
-        */
-       public function build($configuration) {
-               $pluginKey = $configuration['pluginKey'];
-               $extensionName = ($configuration['extensionName'] !== NULL) ? $configuration['extensionName'] : 'ExtBase';
-               $controllerConfigurations = is_array($configuration['controllers.']) ? $configuration['controllers.'] : array();
-               $defaultControllerConfiguration = current($controllerConfigurations);
-               $defaultControllerName = ($defaultControllerConfiguration['controllerName'] !== NULL) ? $defaultControllerConfiguration['controllerName'] : 'Default';
-               $defaultControllerActions = t3lib_div::trimExplode(',', $defaultControllerConfiguration['actions']);
-               $defaultActionName = (!empty($defaultControllerActions[0])) ? $defaultControllerActions[0] : 'index';
-               $allowedControllerActions = array();
-               foreach ($controllerConfigurations as $controllerConfiguration) {
-                       $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions']);
-                       foreach ($controllerActions as $actionName) {
-                               $allowedControllerActions[$controllerConfiguration['controllerName']][] = $actionName;
-                       }
+        * @var array
+        **/
+       protected $allowedControllerActions = array('index', 'error');
+       
+       public function initialize($configuration) {
+               $this->pluginKey = $configuration['pluginKey'];
+               if (!empty($configuration['extensionName']) && is_string($configuration['extensionName'])) {
+                       $this->extensionName = $configuration['extensionName'];
                }
-               $parameters = t3lib_div::_GET('tx_' . strtolower($extensionName) . '_' . strtolower($pluginKey));
-               if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $allowedControllerActions)) {
-                       $controllerName = stripslashes($parameters['controller']);
-               } elseif ($defaultControllerConfiguration['controllerName'] !== NULL) {
-                       $controllerName = $defaultControllerName;
+               if (!empty($configuration['controllers.']) && is_array($configuration['controllers.'])) {
+                       $defaultControllerConfiguration = current($configuration['controllers.']);
+                       if (!empty($defaultControllerConfiguration['controllerName']) && is_string($defaultControllerConfiguration['controllerName'])) {
+                               $this->defaultControllerName = $defaultControllerConfiguration['controllerName'];
+                               $defaultControllerActions = t3lib_div::trimExplode(',', $defaultControllerConfiguration['actions'], TRUE);
+                               if (!empty($defaultControllerActions[0]) && is_string($defaultControllerActions[0])) {
+                                       $this->defaultActionName = $defaultControllerActions[0];
+                               }
+                       }
+                       $allowedControllerActions = array();
+                       foreach ($configuration['controllers.'] as $controllerConfiguration) {
+                               $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions']);
+                               foreach ($controllerActions as $actionName) {
+                                       $allowedControllerActions[$controllerConfiguration['controllerName']][] = $actionName;
+                               }
+                       }
+                       $this->allowedControllerActions = $allowedControllerActions;
                }
+       }
 
-               $allowedActions = $allowedControllerActions[$controllerName];
-
-               if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
-                       $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
-               } elseif (is_string($defaultControllerConfiguration['actions'])) {;
-                       $actions = t3lib_div::trimExplode(',', $defaultControllerConfiguration['actions']);
-                       $actionName = $actions[0];
+       /**
+        * Builds a web request object from the raw HTTP information and the configuration
+        *
+        * @return Tx_ExtBase_MVC_Web_Request The web request as an object
+        */
+       public function build() {
+               $parameters = t3lib_div::_GET('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginKey));
+               if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
+                       $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
+                       $allowedActions = $this->allowedControllerActions[$controllerName];
+                       if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
+                               $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
+                       } else {;
+                               $actionName = $this->defaultActionName;
+                       }
+               } else {
+                       $controllerName = $this->defaultControllerName;
+                       $actionName = $this->defaultActionName;
                }
 
+
                $request = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_Request');
-               $request->setPluginKey($pluginKey);
-               $request->setExtensionName($extensionName);
+               $request->setPluginKey($this->pluginKey);
+               $request->setExtensionName($this->extensionName);
                $request->setControllerName($controllerName);
                $request->setControllerActionName($actionName);
                $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
                $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
-               foreach (t3lib_div::GParrayMerged('tx_' . strtolower($extensionName) . '_' . strtolower($pluginKey)) as $key => $value) {
+               // TODO Revise the GParrayMerged method
+               foreach (t3lib_div::GParrayMerged('tx_' . strtolower($this->extensionName) . '_' . strtolower($this->pluginKey)) as $key => $value) {
                        $request->setArgument($key, $value);
                }
                return $request;
diff --git a/typo3/sysext/extbase/Tests/MVC/Web/RequestBuilder_testcase.php b/typo3/sysext/extbase/Tests/MVC/Web/RequestBuilder_testcase.php
new file mode 100644 (file)
index 0000000..d92a3cf
--- /dev/null
@@ -0,0 +1,107 @@
+!Stack Underflow Error!
+
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
+*  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!
+***************************************************************/
+
+class Tx_ExtBase_MVC_Web_RequestBuilder_testcase extends Tx_ExtBase_Base_testcase {
+       
+       public function __construct() {
+               require_once(t3lib_extMgm::extPath('extbase', 'Classes/MVC/Web/RequestBuilder.php'));
+       }
+       
+       public function setUp() {
+               $this->configuration = array(
+                       'userFunc' => 'tx_extbase_dispatcher->dispatch',
+                       'pluginKey' => 'myplugin',
+                       'extensionName' => 'MyExtension',
+                       'controllers.' => array(
+                               '10.' => array(
+                                       'controllerName' => 'TheFirstController',
+                                       'actions' => ',show,index, ,new,create,delete,edit,update,setup,test'
+                                       ),
+                               '20.' => array(
+                                       'controllerName' => 'TheSecondController',
+                                       'actions' => 'show, index'
+                                       ),
+                               '30.' => array(
+                                       'controllerName' => 'TheThirdController',
+                                       'actions' => 'delete,create'
+                                       )
+                               )
+                       );
+               $this->builder = new Tx_ExtBase_MVC_Web_RequestBuilder;
+       }
+       
+       public function test_BuildReturnsAWebRequestObject() {
+               $this->builder->initialize($this->configuration);
+               $request = $this->builder->build();
+               $this->assertEquals('Tx_ExtBase_MVC_Web_Request', get_class($request));
+               $this->assertEquals('myplugin', $request->getPluginKey());
+               $this->assertEquals('MyExtension', $request->getExtensionName());
+               $this->assertEquals('TheFirstController', $request->getControllerName());
+               $this->assertEquals('show', $request->getControllerActionName());
+       }
+       
+       public function test_BuildWithoutConfigurationReturnsAWebRequestObjectWithDefaultSettings() {
+               $request = $this->builder->build();
+               $this->assertEquals('plugin', $request->getPluginKey());
+               $this->assertEquals('ExtBase', $request->getExtensionName());
+               $this->assertEquals('Default', $request->getControllerName());
+               $this->assertEquals('index', $request->getControllerActionName());
+       }
+
+       public function test_BuildWithMissingControllerConfigurationsReturnsAWebRequestObjectWithDefaultControllerSettings() {
+               $configuration = $this->configuration;
+               unset($configuration['controllers.']);
+               $this->builder->initialize($configuration);
+               $request = $this->builder->build();
+               $this->assertEquals('myplugin', $request->getPluginKey());
+               $this->assertEquals('MyExtension', $request->getExtensionName());
+               $this->assertEquals('Default', $request->getControllerName());
+               $this->assertEquals('index', $request->getControllerActionName());
+       }
+       
+       public function test_BuildWithMissingActionsReturnsAWebRequestObjectWithDefaultControllerSettings() {
+               $configuration = $this->configuration;
+               $configuration['controllers.'] = array(
+                       '10.' => array(
+                               'actions' => ',show,index, ,new,create,delete,edit,update,setup,test'
+                               )
+                       );
+               $this->builder->initialize($configuration);
+               $request = $this->builder->build();
+               $this->assertEquals('myplugin', $request->getPluginKey());
+               $this->assertEquals('MyExtension', $request->getExtensionName());
+               $this->assertEquals('Default', $request->getControllerName());
+               $this->assertEquals('index', $request->getControllerActionName());
+       }
+
+       public function test_BuildSetsTheRequestURIInTheRequestObject() {
+               $this->builder->initialize($this->configuration);
+               $request = $this->builder->build();
+               $this->assertEquals(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'), $request->getRequestURI());
+       }
+
+}
+?>
index a449f8b..f359d53 100644 (file)
@@ -57,7 +57,8 @@ class Tx_ExtBase_Dispatcher {
                        throw new Exception('Could not dispatch the request. Please configure your plugin in the TS Setup.', 1237879677);
                }
                $requestBuilder = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_RequestBuilder');
-               $request = $requestBuilder->build($configuration);
+               $request = $requestBuilder->initialize($configuration);
+               $request = $requestBuilder->build();
                $response = t3lib_div::makeInstance('Tx_ExtBase_MVC_Web_Response');
                $controller = $this->getPreparedController($request);
                $persistenceSession = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Session');