TYPO3 API  SVNRelease
Request.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 class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * Represents a generic request.
00030  *
00031  * @package Extbase
00032  * @subpackage MVC
00033  * @version $ID:$
00034  * @scope prototype
00035  * @api
00036  */
00037 class Tx_Extbase_MVC_Request implements Tx_Extbase_MVC_RequestInterface {
00038 
00039     const PATTERN_MATCH_FORMAT = '/^[a-z0-9]{1,5}$/';
00040 
00041     /**
00042      * Pattern after which the controller object name is built
00043      *
00044      * @var string
00045      */
00046     protected $controllerObjectNamePattern = 'Tx_@extension_@subpackage_Controller_@controllerController';
00047 
00048     /**
00049      * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
00050      */
00051     protected $pluginName = '';
00052 
00053     /**
00054      * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
00055      */
00056     protected $controllerExtensionName = NULL;
00057 
00058     /**
00059      * Subpackage key of the controller which is supposed to handle this request.
00060      *
00061      * @var string
00062      */
00063     protected $controllerSubpackageKey = NULL;
00064 
00065     /**
00066      * @var string Object name of the controller which is supposed to handle this request.
00067      */
00068     protected $controllerName = 'Standard';
00069 
00070     /**
00071      * @var string Name of the action the controller is supposed to take.
00072      */
00073     protected $controllerActionName = 'index';
00074 
00075     /**
00076      * @var array The arguments for this request
00077      */
00078     protected $arguments = array();
00079 
00080     /**
00081      * @var string The requested representation format
00082      */
00083     protected $format = 'txt';
00084 
00085     /**
00086      * @var boolean If this request has been changed and needs to be dispatched again
00087      */
00088     protected $dispatched = FALSE;
00089 
00090     /**
00091      * @var array Errors that occured during this request
00092      */
00093     protected $errors = array();
00094 
00095     /**
00096      * Sets the dispatched flag
00097      *
00098      * @param boolean $flag If this request has been dispatched
00099      * @return void
00100      * @api
00101      */
00102     public function setDispatched($flag) {
00103         $this->dispatched = $flag ? TRUE : FALSE;
00104     }
00105 
00106     /**
00107      * If this request has been dispatched and addressed by the responsible
00108      * controller and the response is ready to be sent.
00109      *
00110      * The dispatcher will try to dispatch the request again if it has not been
00111      * addressed yet.
00112      *
00113      * @return boolean TRUE if this request has been disptached sucessfully
00114      * @api
00115      */
00116     public function isDispatched() {
00117         return $this->dispatched;
00118     }
00119 
00120     /**
00121      * Returns the object name of the controller defined by the extension name and
00122      * controller name
00123      *
00124      * @return string The controller's Object Name
00125      * @throws Tx_Extbase_MVC_Exception_NoSuchController if the controller does not exist
00126      * @api
00127      */
00128     public function getControllerObjectName() {
00129         $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName, $this->controllerObjectNamePattern);
00130         $lowercaseObjectName = str_replace('@subpackage', $this->controllerSubpackageKey, $lowercaseObjectName);
00131         $lowercaseObjectName = str_replace('@controller', $this->controllerName, $lowercaseObjectName);
00132         $lowercaseObjectName = str_replace('__', '_', $lowercaseObjectName);
00133         // TODO implement getCaseSensitiveObjectName()
00134         $objectName = $lowercaseObjectName;
00135         if ($objectName === FALSE) throw new Tx_Extbase_MVC_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
00136 
00137         return $objectName;
00138     }
00139 
00140     /**
00141      * Explicitly sets the object name of the controller
00142      *
00143      * @param string $controllerObjectName The fully qualified controller object name
00144      * @return void
00145      */
00146     public function setControllerObjectName($controllerObjectName) {
00147         $matches = array();
00148         preg_match('/
00149             ^Tx
00150             _(?P<extensionName>[^_]+)
00151             _
00152             (
00153                 Controller
00154             |
00155                 (?P<subpackageKey>.+)_Controller
00156             )
00157             _(?P<controllerName>[a-z_]+)Controller
00158             $/ix', $controllerObjectName, $matches
00159         );
00160 
00161         $this->controllerExtensionName = $matches['extensionName'];
00162         $this->controllerSubpackageKey = (isset($matches['subpackageKey'])) ? $matches['subpackageKey'] : NULL;
00163         $this->controllerName = $matches['controllerName'];
00164     }
00165 
00166     /**
00167      * Sets the plugin name.
00168      *
00169      * @param string $extensionName The plugin name.
00170      * @return void
00171      */
00172     public function setPluginName($pluginName = NULL) {
00173         if ($pluginName !== NULL) {
00174             $this->pluginName = $pluginName;
00175         }
00176     }
00177 
00178     /**
00179      * Returns the plugin key.
00180      *
00181      * @return string The plugin key
00182      * @api
00183      */
00184     public function getPluginName() {
00185         return $this->pluginName;
00186     }
00187 
00188     /**
00189      * Sets the extension name of the controller.
00190      *
00191      * @param string $controllerExtensionName The extension name.
00192      * @return void
00193      * @throws Tx_Extbase_MVC_Exception_InvalidExtensionName if the extension name is not valid
00194      */
00195     public function setControllerExtensionName($controllerExtensionName) {
00196         if ($controllerExtensionName !== NULL) {
00197             $this->controllerExtensionName = $controllerExtensionName;
00198         }
00199     }
00200 
00201     /**
00202      * Returns the extension name of the specified controller.
00203      *
00204      * @return string The extension name
00205      * @api
00206      */
00207     public function getControllerExtensionName() {
00208         return $this->controllerExtensionName;
00209     }
00210 
00211     /**
00212      * Returns the extension name of the specified controller.
00213      *
00214      * @return string The extension key
00215      * @api
00216      */
00217     public function getControllerExtensionKey() {
00218         return t3lib_div::camelCaseToLowerCaseUnderscored($this->controllerExtensionName);
00219     }
00220 
00221     /**
00222      * Sets the subpackage key of the controller.
00223      *
00224      * @param string $subpackageKey The subpackage key.
00225      * @return void
00226      */
00227     public function setControllerSubpackageKey($subpackageKey) {
00228         $this->controllerSubpackageKey = $subpackageKey;
00229     }
00230 
00231     /**
00232      * Returns the subpackage key of the specified controller.
00233      * If there is no subpackage key set, the method returns NULL
00234      *
00235      * @return string The subpackage key
00236      */
00237     public function getControllerSubpackageKey() {
00238         return $this->controllerSubpackageKey;
00239     }
00240 
00241     /**
00242      * Sets the name of the controller which is supposed to handle the request.
00243      * Note: This is not the object name of the controller!
00244      *
00245      * @param string $controllerName Name of the controller
00246      * @return void
00247      */
00248     public function setControllerName($controllerName) {
00249         if (!is_string($controllerName) && $controllerName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
00250         if (strpos($controllerName, '_') !== FALSE) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
00251         if ($controllerName !== NULL) {
00252             $this->controllerName = $controllerName;
00253         }
00254     }
00255 
00256     /**
00257      * Returns the object name of the controller supposed to handle this request, if one
00258      * was set already (if not, the name of the default controller is returned)
00259      *
00260      * @return string Object name of the controller
00261      * @api
00262      */
00263     public function getControllerName() {
00264         return $this->controllerName;
00265     }
00266 
00267     /**
00268      * Sets the name of the action contained in this request.
00269      *
00270      * Note that the action name must start with a lower case letter and is case sensitive.
00271      *
00272      * @param string $actionName: Name of the action to execute by the controller
00273      * @return void
00274      * @throws Tx_Extbase_MVC_Exception_InvalidActionName if the action name is not valid
00275      */
00276     public function setControllerActionName($actionName) {
00277         if (!is_string($actionName) && $actionName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidActionName('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
00278         if (($actionName{0} !== strtolower($actionName{0})) && $actionName !== NULL) throw new Tx_Extbase_MVC_Exception_InvalidActionName('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
00279         if ($actionName !== NULL) {
00280             $this->controllerActionName = $actionName;
00281         }
00282     }
00283 
00284     /**
00285      * Returns the name of the action the controller is supposed to execute.
00286      *
00287      * @return string Action name
00288      * @api
00289      */
00290     public function getControllerActionName() {
00291         $controllerObjectName = $this->getControllerObjectName();
00292         if ($controllerObjectName !== '' && ($this->controllerActionName === strtolower($this->controllerActionName))) {
00293             $actionMethodName = $this->controllerActionName . 'Action';
00294             foreach (get_class_methods($controllerObjectName) as $existingMethodName) {
00295                 if (strtolower($existingMethodName) === strtolower($actionMethodName)) {
00296                     $this->controllerActionName = substr($existingMethodName, 0, -6);
00297                     break;
00298                 }
00299             }
00300         }
00301         return $this->controllerActionName;
00302     }
00303 
00304     /**
00305      * Sets the value of the specified argument
00306      *
00307      * @param string $argumentName Name of the argument to set
00308      * @param mixed $value The new value
00309      * @return void
00310      */
00311     public function setArgument($argumentName, $value) {
00312         if (!is_string($argumentName) || strlen($argumentName) == 0) throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
00313         $this->arguments[$argumentName] = $value;
00314     }
00315 
00316     /**
00317      * Sets the whole arguments array and therefore replaces any arguments
00318      * which existed before.
00319      *
00320      * @param array $arguments An array of argument names and their values
00321      * @return void
00322      */
00323     public function setArguments(array $arguments) {
00324         $this->arguments = $arguments;
00325     }
00326 
00327     /**
00328      * Returns an array of arguments and their values
00329      *
00330      * @return array Associative array of arguments and their values (which may be arguments and values as well)
00331      * @api
00332      */
00333     public function getArguments() {
00334         return $this->arguments;
00335     }
00336 
00337     /**
00338      * Returns the value of the specified argument
00339      *
00340      * @param string $argumentName Name of the argument
00341      * @return string Value of the argument
00342      * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
00343      * @api
00344      */
00345     public function getArgument($argumentName) {
00346         if (!isset($this->arguments[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
00347         return $this->arguments[$argumentName];
00348     }
00349 
00350     /**
00351      * Checks if an argument of the given name exists (is set)
00352      *
00353      * @param string $argumentName Name of the argument to check
00354      * @return boolean TRUE if the argument is set, otherwise FALSE
00355      * @api
00356      */
00357     public function hasArgument($argumentName) {
00358         return isset($this->arguments[$argumentName]);
00359     }
00360 
00361     /**
00362      * Sets the requested representation format
00363      *
00364      * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
00365      * @return void
00366      * @author Robert Lemke <robert@typo3.org>
00367      */
00368     public function setFormat($format) {
00369         $this->format = $format;
00370     }
00371 
00372     /**
00373      * Returns the requested representation format
00374      *
00375      * @return string The desired format, something like "html", "xml", "png", "json" or the like.
00376      * @author Robert Lemke <robert@typo3.org>
00377      * @api
00378      */
00379     public function getFormat() {
00380         return $this->format;
00381     }
00382 
00383     /**
00384      * Set errors that occured during the request (e.g. argument mapping errors)
00385      *
00386      * @param array $errors An array of Tx_Extbase_Error_Error objects
00387      * @return void
00388      */
00389     public function setErrors(array $errors) {
00390         $this->errors = $errors;
00391     }
00392 
00393     /**
00394      * Get errors that occured during the request (e.g. argument mapping errors)
00395      *
00396      * @return array The errors that occured during the request
00397      */
00398     public function getErrors() {
00399         return $this->errors;
00400     }
00401 
00402 }