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.
37 class Tx_Extbase_MVC_Request
implements Tx_Extbase_MVC_RequestInterface
{
39 const PATTERN_MATCH_FORMAT
= '/^[a-z0-9]{1,5}$/';
42 * Pattern after which the controller object name is built
46 protected $controllerObjectNamePattern = 'Tx_@extension_@subpackage_Controller_@controllerController';
49 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
51 protected $pluginName = '';
54 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
56 protected $controllerExtensionName = NULL;
59 * Subpackage key of the controller which is supposed to handle this request.
63 protected $controllerSubpackageKey = NULL;
66 * @var string Object name of the controller which is supposed to handle this request.
68 protected $controllerName = 'Standard';
71 * @var string Name of the action the controller is supposed to take.
73 protected $controllerActionName = 'index';
76 * @var array The arguments for this request
78 protected $arguments = array();
81 * @var string The requested representation format
83 protected $format = 'txt';
86 * @var boolean If this request has been changed and needs to be dispatched again
88 protected $dispatched = FALSE;
91 * @var array Errors that occured during this request
93 protected $errors = array();
96 * Sets the dispatched flag
98 * @param boolean $flag If this request has been dispatched
102 public function setDispatched($flag) {
103 $this->dispatched
= $flag ?
TRUE : FALSE;
107 * If this request has been dispatched and addressed by the responsible
108 * controller and the response is ready to be sent.
110 * The dispatcher will try to dispatch the request again if it has not been
113 * @return boolean TRUE if this request has been disptached sucessfully
116 public function isDispatched() {
117 return $this->dispatched
;
121 * Returns the object name of the controller defined by the extension name and
124 * @return string The controller's Object Name
125 * @throws Tx_Extbase_MVC_Exception_NoSuchController if the controller does not exist
128 public function getControllerObjectName() {
129 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName
, $this->controllerObjectNamePattern
);
130 $lowercaseObjectName = str_replace('@subpackage', $this->controllerSubpackageKey
, $lowercaseObjectName);
131 $lowercaseObjectName = str_replace('@controller', $this->controllerName
, $lowercaseObjectName);
132 $lowercaseObjectName = str_replace('__', '_', $lowercaseObjectName);
133 // TODO implement getCaseSensitiveObjectName()
134 $objectName = $lowercaseObjectName;
135 if ($objectName === FALSE) throw new Tx_Extbase_MVC_Exception_NoSuchController('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
141 * Explicitly sets the object name of the controller
143 * @param string $controllerObjectName The fully qualified controller object name
146 public function setControllerObjectName($controllerObjectName) {
150 _(?P<extensionName>[^_]+)
155 (?P<subpackageKey>.+)_Controller
157 _(?P<controllerName>[a-z_]+)Controller
158 $/ix', $controllerObjectName, $matches
161 $this->controllerExtensionName
= $matches['extensionName'];
162 $this->controllerSubpackageKey
= (isset($matches['subpackageKey'])) ?
$matches['subpackageKey'] : NULL;
163 $this->controllerName
= $matches['controllerName'];
167 * Sets the plugin name.
169 * @param string $extensionName The plugin name.
172 public function setPluginName($pluginName = NULL) {
173 if ($pluginName !== NULL) {
174 $this->pluginName
= $pluginName;
179 * Returns the plugin key.
181 * @return string The plugin key
184 public function getPluginName() {
185 return $this->pluginName
;
189 * Sets the extension name of the controller.
191 * @param string $controllerExtensionName The extension name.
193 * @throws Tx_Extbase_MVC_Exception_InvalidExtensionName if the extension name is not valid
195 public function setControllerExtensionName($controllerExtensionName) {
196 if ($controllerExtensionName !== NULL) {
197 $this->controllerExtensionName
= $controllerExtensionName;
202 * Returns the extension name of the specified controller.
204 * @return string The extension name
207 public function getControllerExtensionName() {
208 return $this->controllerExtensionName
;
212 * Returns the extension name of the specified controller.
214 * @return string The extension key
217 public function getControllerExtensionKey() {
218 return Tx_Extbase_Utility_Extension
::convertCamelCaseToLowerCaseUnderscored($this->controllerExtensionName
);
222 * Sets the subpackage key of the controller.
224 * @param string $subpackageKey The subpackage key.
227 public function setControllerSubpackageKey($subpackageKey) {
228 $this->controllerSubpackageKey
= $subpackageKey;
232 * Returns the subpackage key of the specified controller.
233 * If there is no subpackage key set, the method returns NULL
235 * @return string The subpackage key
237 public function getControllerSubpackageKey() {
238 return $this->controllerSubpackageKey
;
242 * Sets the name of the controller which is supposed to handle the request.
243 * Note: This is not the object name of the controller!
245 * @param string $controllerName Name of the controller
248 public function setControllerName($controllerName) {
249 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);
250 if (strpos($controllerName, '_') !== FALSE) throw new Tx_Extbase_MVC_Exception_InvalidControllerName('The controller name must not contain underscores.', 1217846412);
251 if ($controllerName !== NULL) {
252 $this->controllerName
= $controllerName;
257 * Returns the object name of the controller supposed to handle this request, if one
258 * was set already (if not, the name of the default controller is returned)
260 * @return string Object name of the controller
263 public function getControllerName() {
264 return $this->controllerName
;
268 * Sets the name of the action contained in this request.
270 * Note that the action name must start with a lower case letter and is case sensitive.
272 * @param string $actionName: Name of the action to execute by the controller
274 * @throws Tx_Extbase_MVC_Exception_InvalidActionName if the action name is not valid
276 public function setControllerActionName($actionName) {
277 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);
278 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);
279 if ($actionName !== NULL) {
280 $this->controllerActionName
= $actionName;
285 * Returns the name of the action the controller is supposed to execute.
287 * @return string Action name
290 public function getControllerActionName() {
291 $controllerObjectName = $this->getControllerObjectName();
292 if ($controllerObjectName !== '' && ($this->controllerActionName
=== strtolower($this->controllerActionName
))) {
293 $actionMethodName = $this->controllerActionName
. 'Action';
294 foreach (get_class_methods($controllerObjectName) as $existingMethodName) {
295 if (strtolower($existingMethodName) === strtolower($actionMethodName)) {
296 $this->controllerActionName
= substr($existingMethodName, 0, -6);
301 return $this->controllerActionName
;
305 * Sets the value of the specified argument
307 * @param string $argumentName Name of the argument to set
308 * @param mixed $value The new value
311 public function setArgument($argumentName, $value) {
312 if (!is_string($argumentName) ||
strlen($argumentName) == 0) throw new Tx_Extbase_MVC_Exception_InvalidArgumentName('Invalid argument name.', 1210858767);
313 $this->arguments
[$argumentName] = $value;
317 * Sets the whole arguments array and therefore replaces any arguments
318 * which existed before.
320 * @param array $arguments An array of argument names and their values
323 public function setArguments(array $arguments) {
324 $this->arguments
= $arguments;
328 * Returns an array of arguments and their values
330 * @return array Associative array of arguments and their values (which may be arguments and values as well)
333 public function getArguments() {
334 return $this->arguments
;
338 * Returns the value of the specified argument
340 * @param string $argumentName Name of the argument
341 * @return string Value of the argument
342 * @throws Tx_Extbase_MVC_Exception_NoSuchArgument if such an argument does not exist
345 public function getArgument($argumentName) {
346 if (!isset($this->arguments
[$argumentName])) throw new Tx_Extbase_MVC_Exception_NoSuchArgument('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
347 return $this->arguments
[$argumentName];
351 * Checks if an argument of the given name exists (is set)
353 * @param string $argumentName Name of the argument to check
354 * @return boolean TRUE if the argument is set, otherwise FALSE
357 public function hasArgument($argumentName) {
358 return isset($this->arguments
[$argumentName]);
362 * Sets the requested representation format
364 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
366 * @author Robert Lemke <robert@typo3.org>
368 public function setFormat($format) {
369 $this->format
= $format;
373 * Returns the requested representation format
375 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
376 * @author Robert Lemke <robert@typo3.org>
379 public function getFormat() {
380 return $this->format
;
384 * Set errors that occured during the request (e.g. argument mapping errors)
386 * @param array $errors An array of Tx_Extbase_Error_Error objects
389 public function setErrors(array $errors) {
390 $this->errors
= $errors;
394 * Get errors that occured during the request (e.g. argument mapping errors)
396 * @return array The errors that occured during the request
398 public function getErrors() {
399 return $this->errors
;