2 namespace TYPO3\CMS\Extbase\Mvc
;
3 /***************************************************************
6 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
9 * This class is a backport of the corresponding class of FLOW3.
10 * All credits go to the v5 team.
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
29 * Represents a generic request.
37 class Request
implements \TYPO3\CMS\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 * Pattern after which the namespaced controller object name is built
53 protected $namespacedControllerObjectNamePattern = '@vendor\@extension\@subpackage\Controller\@controllerController';
56 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
58 protected $pluginName = '';
61 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
63 protected $controllerExtensionName = NULL;
66 * @var string vendor prefix
68 protected $controllerVendorName = NULL;
71 * Subpackage key of the controller which is supposed to handle this request.
75 protected $controllerSubpackageKey = NULL;
78 * @var string Object name of the controller which is supposed to handle this request.
80 protected $controllerName = 'Standard';
83 * @var string Name of the action the controller is supposed to take.
85 protected $controllerActionName = 'index';
88 * @var array The arguments for this request
90 protected $arguments = array();
93 * Framework-internal arguments for this request, such as __referrer.
94 * All framework-internal arguments start with double underscore (__),
95 * and are only used from within the framework. Not for user consumption.
96 * Internal Arguments can be objects, in contrast to public arguments
100 protected $internalArguments = array();
103 * @var string The requested representation format
105 protected $format = 'txt';
108 * @var boolean If this request has been changed and needs to be dispatched again
110 protected $dispatched = FALSE;
113 * If this request is a forward because of an error, the original request gets filled.
115 * @var \TYPO3\CMS\Extbase\Mvc\Request
117 protected $originalRequest = NULL;
120 * If the request is a forward because of an error, these mapping results get filled here.
122 * @var \TYPO3\CMS\Extbase\Error\Result
124 protected $originalRequestMappingResults = NULL;
127 * @var array Errors that occured during this request
128 * @deprecated since Extbase 1.4.0, will be removed with Extbase 6.0
130 protected $errors = array();
133 * Sets the dispatched flag
135 * @param boolean $flag If this request has been dispatched
140 public function setDispatched($flag) {
141 $this->dispatched
= $flag ?
TRUE : FALSE;
145 * If this request has been dispatched and addressed by the responsible
146 * controller and the response is ready to be sent.
148 * The dispatcher will try to dispatch the request again if it has not been
151 * @return boolean TRUE if this request has been disptached sucessfully
154 public function isDispatched() {
155 return $this->dispatched
;
159 * Returns the object name of the controller defined by the extension name and
162 * @return string The controller's Object Name
163 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchControllerException if the controller does not exist
166 public function getControllerObjectName() {
167 if (NULL !== $this->controllerVendorName
) {
168 // It's safe to assume a namespaced name as namespaced names have to follow PSR-0
169 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName
, $this->namespacedControllerObjectNamePattern
);
170 $lowercaseObjectName = str_replace('@subpackage', $this->controllerSubpackageKey
, $lowercaseObjectName);
171 $lowercaseObjectName = str_replace('@controller', $this->controllerName
, $lowercaseObjectName);
172 $lowercaseObjectName = str_replace('@vendor', $this->controllerVendorName
, $lowercaseObjectName);
173 $lowercaseObjectName = str_replace('\\\\', '\\', $lowercaseObjectName);
175 $lowercaseObjectName = str_replace('@extension', $this->controllerExtensionName
, $this->controllerObjectNamePattern
);
176 $lowercaseObjectName = str_replace('@subpackage', $this->controllerSubpackageKey
, $lowercaseObjectName);
177 $lowercaseObjectName = str_replace('@controller', $this->controllerName
, $lowercaseObjectName);
178 $lowercaseObjectName = str_replace('__', '_', $lowercaseObjectName);
180 // TODO implement getCaseSensitiveObjectName()
181 $objectName = $lowercaseObjectName;
182 if ($objectName === FALSE) {
183 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
NoSuchControllerException('The controller object "' . $lowercaseObjectName . '" does not exist.', 1220884009);
189 * Explicitly sets the object name of the controller
191 * @param string $controllerObjectName The fully qualified controller object name
195 public function setControllerObjectName($controllerObjectName) {
197 if (strpos($controllerObjectName, '\\') !== FALSE) {
198 if (substr($controllerObjectName, 0, 9) === 'TYPO3\CMS') {
199 $extensionName = '^(?P<vendorName>[^\\\]+\\\[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
201 $extensionName = '^(?P<vendorName>[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
204 $extensionName . '\\\(Controller|(?P<subpackageKey>.+)\\\Controller)\\\(?P<controllerName>[a-z\\\]+)Controller
205 $/ix', $controllerObjectName, $matches);
208 ^Tx_(?P<extensionName>[^_]+)_(Controller|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller
209 $/ix', $controllerObjectName, $matches);
211 $this->controllerVendorName
= isset($matches['vendorName']) ?
$matches['vendorName'] : NULL;
212 $this->controllerExtensionName
= $matches['extensionName'];
213 $this->controllerSubpackageKey
= isset($matches['subpackageKey']) ?
$matches['subpackageKey'] : NULL;
214 $this->controllerName
= $matches['controllerName'];
218 * Sets the plugin name.
220 * @param string|NULL $pluginName
224 public function setPluginName($pluginName = NULL) {
225 if ($pluginName !== NULL) {
226 $this->pluginName
= $pluginName;
231 * Returns the plugin key.
233 * @return string The plugin key
236 public function getPluginName() {
237 return $this->pluginName
;
241 * Sets the extension name of the controller.
243 * @param string $controllerExtensionName The extension name.
246 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException if the extension name is not valid
248 public function setControllerExtensionName($controllerExtensionName) {
249 if ($controllerExtensionName !== NULL) {
250 $this->controllerExtensionName
= $controllerExtensionName;
255 * Returns the extension name of the specified controller.
257 * @return string The extension name
260 public function getControllerExtensionName() {
261 return $this->controllerExtensionName
;
265 * Returns the extension name of the specified controller.
267 * @return string The extension key
270 public function getControllerExtensionKey() {
271 return \TYPO3\CMS\Core\Utility\GeneralUtility
::camelCaseToLowerCaseUnderscored($this->controllerExtensionName
);
275 * Sets the subpackage key of the controller.
277 * @param string $subpackageKey The subpackage key.
281 public function setControllerSubpackageKey($subpackageKey) {
282 $this->controllerSubpackageKey
= $subpackageKey;
286 * Returns the subpackage key of the specified controller.
287 * If there is no subpackage key set, the method returns NULL
289 * @return string The subpackage key
291 public function getControllerSubpackageKey() {
292 return $this->controllerSubpackageKey
;
296 * Sets the name of the controller which is supposed to handle the request.
297 * Note: This is not the object name of the controller!
299 * @param string $controllerName Name of the controller
301 * @throws Exception\InvalidControllerNameException
304 public function setControllerName($controllerName) {
305 if (!is_string($controllerName) && $controllerName !== NULL) {
306 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
InvalidControllerNameException('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
308 if (strpos($controllerName, '_') !== FALSE) {
309 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
InvalidControllerNameException('The controller name must not contain underscores.', 1217846412);
311 if ($controllerName !== NULL) {
312 $this->controllerName
= $controllerName;
317 * Returns the object name of the controller supposed to handle this request, if one
318 * was set already (if not, the name of the default controller is returned)
320 * @return string Object name of the controller
323 public function getControllerName() {
324 return $this->controllerName
;
328 * Sets the name of the action contained in this request.
330 * Note that the action name must start with a lower case letter and is case sensitive.
332 * @param string $actionName: Name of the action to execute by the controller
335 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException if the action name is not valid
337 public function setControllerActionName($actionName) {
338 if (!is_string($actionName) && $actionName !== NULL) {
339 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
InvalidActionNameException('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176358);
341 if ($actionName[0] !== strtolower($actionName[0]) && $actionName !== NULL) {
342 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
InvalidActionNameException('The action name must start with a lower case letter, "' . $actionName . '" does not match this criteria.', 1218473352);
344 if ($actionName !== NULL) {
345 $this->controllerActionName
= $actionName;
350 * Returns the name of the action the controller is supposed to execute.
352 * @return string Action name
355 public function getControllerActionName() {
356 $controllerObjectName = $this->getControllerObjectName();
357 if ($controllerObjectName !== '' && $this->controllerActionName
=== strtolower($this->controllerActionName
)) {
358 $actionMethodName = $this->controllerActionName
. 'Action';
359 $classMethods = get_class_methods($controllerObjectName);
360 if (is_array($classMethods)) {
361 foreach ($classMethods as $existingMethodName) {
362 if (strtolower($existingMethodName) === strtolower($actionMethodName)) {
363 $this->controllerActionName
= substr($existingMethodName, 0, -6);
369 return $this->controllerActionName
;
373 * Sets the value of the specified argument
375 * @param string $argumentName Name of the argument to set
376 * @param mixed $value The new value
378 * @throws Exception\InvalidArgumentNameException
381 public function setArgument($argumentName, $value) {
382 if (!is_string($argumentName) ||
strlen($argumentName) == 0) {
383 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
InvalidArgumentNameException('Invalid argument name.', 1210858767);
385 if ($argumentName[0] === '_' && $argumentName[1] === '_') {
386 $this->internalArguments
[$argumentName] = $value;
389 switch ($argumentName) {
391 $this->setControllerExtensionName($value);
394 $this->setControllerSubpackageKey($value);
397 $this->setControllerName($value);
400 $this->setControllerActionName($value);
403 $this->setFormat($value);
406 $this->setControllerVendorName($value);
409 $this->arguments
[$argumentName] = $value;
414 * sets the VendorName
416 * @param string $vendorName
420 public function setControllerVendorName($vendorName) {
421 $this->controllerVendorName
= $vendorName;
429 public function getControllerVendorName() {
430 return $this->controllerVendorName
;
434 * Sets the whole arguments array and therefore replaces any arguments
435 * which existed before.
437 * @param array $arguments An array of argument names and their values
441 public function setArguments(array $arguments) {
442 $this->arguments
= array();
443 foreach ($arguments as $argumentName => $argumentValue) {
444 $this->setArgument($argumentName, $argumentValue);
449 * Returns an array of arguments and their values
451 * @return array Associative array of arguments and their values (which may be arguments and values as well)
454 public function getArguments() {
455 return $this->arguments
;
459 * Returns the value of the specified argument
461 * @param string $argumentName Name of the argument
463 * @return string Value of the argument
464 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if such an argument does not exist
467 public function getArgument($argumentName) {
468 if (!isset($this->arguments
[$argumentName])) {
469 throw new \TYPO3\CMS\Extbase\Mvc\Exception\
NoSuchArgumentException('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
471 return $this->arguments
[$argumentName];
475 * Checks if an argument of the given name exists (is set)
477 * @param string $argumentName Name of the argument to check
479 * @return boolean TRUE if the argument is set, otherwise FALSE
482 public function hasArgument($argumentName) {
483 return isset($this->arguments
[$argumentName]);
487 * Sets the requested representation format
489 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
492 * @author Robert Lemke <robert@typo3.org>
494 public function setFormat($format) {
495 $this->format
= $format;
499 * Returns the requested representation format
501 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
502 * @author Robert Lemke <robert@typo3.org>
505 public function getFormat() {
506 return $this->format
;
510 * Set errors that occured during the request (e.g. argument mapping errors)
512 * @param array $errors An array of Tx_Extbase_Error_Error objects
515 * @deprecated since Extbase 1.4.0, will be removed with Extbase 6.0
517 public function setErrors(array $errors) {
518 $this->errors
= $errors;
522 * Get errors that occured during the request (e.g. argument mapping errors)
524 * @return array The errors that occured during the request
525 * @deprecated since Extbase 1.4.0, will be removed with Extbase 6.0
527 public function getErrors() {
528 return $this->errors
;
532 * Returns the original request. Filled only if a property mapping error occured.
534 * @return \TYPO3\CMS\Extbase\Mvc\Request the original request.
536 public function getOriginalRequest() {
537 return $this->originalRequest
;
541 * @param \TYPO3\CMS\Extbase\Mvc\Request $originalRequest
545 public function setOriginalRequest(\TYPO3\CMS\Extbase\Mvc\Request
$originalRequest) {
546 $this->originalRequest
= $originalRequest;
550 * Get the request mapping results for the original request.
552 * @return \TYPO3\CMS\Extbase\Error\Result
554 public function getOriginalRequestMappingResults() {
555 if ($this->originalRequestMappingResults
=== NULL) {
556 return new \TYPO3\CMS\Extbase\Error\
Result();
558 return $this->originalRequestMappingResults
;
562 * @param \TYPO3\CMS\Extbase\Error\Result $originalRequestMappingResults
564 public function setOriginalRequestMappingResults(\TYPO3\CMS\Extbase\Error\Result
$originalRequestMappingResults) {
565 $this->originalRequestMappingResults
= $originalRequestMappingResults;
569 * Get the internal arguments of the request, i.e. every argument starting
570 * with two underscores.
574 public function getInternalArguments() {
575 return $this->internalArguments
;
579 * Returns the value of the specified argument
581 * @param string $argumentName Name of the argument
583 * @return string Value of the argument, or NULL if not set.
585 public function getInternalArgument($argumentName) {
586 if (!isset($this->internalArguments
[$argumentName])) {
589 return $this->internalArguments
[$argumentName];