2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Represents a generic request.
36 class Tx_Extbase_MVC_Request
{
38 const PATTERN_MATCH_FORMAT
= '/^[a-z0-9]{1,5}$/';
41 * Pattern after which the controller object name is built
45 protected $controllerObjectNamePattern = 'Tx_@extension_Controller_@controllerController';
48 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
50 protected $pluginName = '';
53 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
55 protected $controllerExtensionName = 'Extbase';
58 * @var string Name of the controller which is supposed to handle this request.
60 protected $controllerName = 'Standard';
63 * @var string Name of the action the controller is supposed to take.
65 protected $controllerActionName = 'index';
68 * @var ArrayObject The arguments for this request
70 protected $arguments = array();
73 * @var boolean If this request has been changed and needs to be dispatched again
75 protected $dispatched = FALSE;
78 * @var array Errors that occured during this request
80 protected $errors = array();
83 * Constructs this request
86 public function __construct() {
90 * Sets the dispatched flag
92 * @param boolean $flag If this request has been dispatched
96 public function setDispatched($flag) {
97 $this->dispatched
= $flag ?
TRUE : FALSE;
101 * If this request has been dispatched and addressed by the responsible
102 * controller and the response is ready to be sent.
104 * The dispatcher will try to dispatch the request again if it has not been
107 * @return boolean TRUE if this request has been disptached sucessfully
110 public function isDispatched() {
111 return $this->dispatched
;
115 * Returns the object name of the controller defined by the extension name and
118 * @return string The controller's Object Name
119 * @throws Tx_Extbase_MVC_Exception_NoSuchController if the controller does not exist
122 public function getControllerObjectName() {
123 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName
, $this->controllerObjectNamePattern
);
124 $lowercaseObjectName = str_replace('@controller', $this->controllerName
, $lowercaseObjectName);
125 // TODO implement getCaseSensitiveObjectName()
126 $objectName = $lowercaseObjectName;
127 if ($objectName === FALSE) throw new Tx_Extbase_MVC_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
133 * Sets the plugin name.
135 * @param string $extensionName The plugin name.
138 public function setPluginName($pluginName = NULL) {
139 if ($pluginName !== NULL) {
140 $this->pluginName
= $pluginName;
145 * Returns the plugin key.
147 * @return string The plugin key
150 public function getPluginName() {
151 return $this->pluginName
;
155 * Sets the extension name of the controller.
157 * @param string $controllerExtensionName The extension name.
159 * @throws Tx_Extbase_MVC_Exception_InvalidExtensionName if the extension name is not valid
161 public function setControllerExtensionName($controllerExtensionName = NULL) {
162 if ($controllerExtensionName !== NULL) {
163 $this->controllerExtensionName
= $controllerExtensionName;
168 * Returns the extension name of the specified controller.
170 * @return string The extension name
173 public function getControllerExtensionName() {
174 return $this->controllerExtensionName
;
178 * Returns the extension name of the specified controller.
180 * @return string The extension name
183 public function getControllerExtensionKey() {
184 return Tx_Extbase_Utility_Extension
::convertCamelCaseToLowerCaseUnderscored($this->controllerExtensionName
);
188 * Sets the name of the controller which is supposed to handle the request.
189 * Note: This is not the object name of the controller!
191 * @param string $controllerName Name of the controller
194 public function setControllerName($controllerName = NULL) {
195 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);
196 if (strpos($controllerName, '_') !== FALSE) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
197 if ($controllerName !== NULL) {
198 $this->controllerName
= $controllerName;
203 * Returns the object name of the controller supposed to handle this request, if one
204 * was set already (if not, the name of the default controller is returned)
206 * @return string Object name of the controller
209 public function getControllerName() {
210 return $this->controllerName
;
214 * Sets the name of the action contained in this request.
216 * Note that the action name must start with a lower case letter.
218 * @param string $actionName: Name of the action to execute by the controller
220 * @throws Tx_Extbase_MVC_Exception_InvalidActionName if the action name is not valid
222 public function setControllerActionName($actionName = NULL) {
223 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);
224 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);
225 if ($actionName !== NULL) {
226 $this->controllerActionName
= $actionName;
231 * Returns the name of the action the controller is supposed to execute.
233 * @return string Action name
236 public function getControllerActionName() {
237 return $this->controllerActionName
;
241 * Sets the value of the specified argument
243 * @param string $argumentName Name of the argument to set
244 * @param mixed $value The new value
247 public function setArgument($argumentName, $value) {
248 if (!is_string($argumentName) ||
strlen($argumentName) == 0) throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
249 $this->arguments
[$argumentName] = $value;
253 * Sets the whole arguments array and therefore replaces any arguments
254 * which existed before.
256 * @param array $arguments An array of argument names and their values
259 public function setArguments(array $arguments) {
260 $this->arguments
= $arguments;
264 * Returns an array of arguments and their values
266 * @return array Associative array of arguments and their values (which may be arguments and values as well)
269 public function getArguments() {
270 return $this->arguments
;
274 * Returns the value of the specified argument
276 * @param string $argumentName Name of the argument
277 * @return string Value of the argument
278 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
281 public function getArgument($argumentName) {
282 if (!isset($this->arguments
[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
283 return $this->arguments
[$argumentName];
287 * Checks if an argument of the given name exists (is set)
289 * @param string $argumentName Name of the argument to check
290 * @return boolean TRUE if the argument is set, otherwise FALSE
293 public function hasArgument($argumentName) {
294 return isset($this->arguments
[$argumentName]);
298 * Set errors that occured during the request (e.g. argument mapping errors)
300 * @param array $errors An array of Tx_Extbase_Error_Error objects
303 public function setErrors(array $errors) {
304 $this->errors
= $errors;
308 * Get errors that occured during the request (e.g. argument mapping errors)
310 * @return array The errors that occured during the request
312 public function getErrors() {
313 return $this->errors
;