[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Request.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
18
19 /**
20 * Represents a generic request.
21 *
22 * @api
23 */
24 class Request implements RequestInterface
25 {
26 const PATTERN_MATCH_FORMAT = '/^[a-z0-9]{1,5}$/';
27
28 /**
29 * Pattern after which the controller object name is built
30 *
31 * @var string
32 */
33 protected $controllerObjectNamePattern = 'Tx_@extension_@subpackage_Controller_@controllerController';
34
35 /**
36 * Pattern after which the namespaced controller object name is built
37 *
38 * @var string
39 */
40 protected $namespacedControllerObjectNamePattern = '@vendor\@extension\@subpackage\Controller\@controllerController';
41
42 /**
43 * @var string Key of the plugin which identifies the plugin. It must be a string containing [a-z0-9]
44 */
45 protected $pluginName = '';
46
47 /**
48 * @var string Name of the extension which is supposed to handle this request. This is the extension name converted to UpperCamelCase
49 */
50 protected $controllerExtensionName = null;
51
52 /**
53 * @var string vendor prefix
54 */
55 protected $controllerVendorName = null;
56
57 /**
58 * Subpackage key of the controller which is supposed to handle this request.
59 *
60 * @var string
61 */
62 protected $controllerSubpackageKey = null;
63
64 /**
65 * @var string Object name of the controller which is supposed to handle this request.
66 */
67 protected $controllerName = 'Standard';
68
69 /**
70 * @var string Name of the action the controller is supposed to take.
71 */
72 protected $controllerActionName = 'index';
73
74 /**
75 * @var array The arguments for this request
76 */
77 protected $arguments = [];
78
79 /**
80 * Framework-internal arguments for this request, such as __referrer.
81 * All framework-internal arguments start with double underscore (__),
82 * and are only used from within the framework. Not for user consumption.
83 * Internal Arguments can be objects, in contrast to public arguments
84 *
85 * @var array
86 */
87 protected $internalArguments = [];
88
89 /**
90 * @var string The requested representation format
91 */
92 protected $format = 'txt';
93
94 /**
95 * @var bool If this request has been changed and needs to be dispatched again
96 */
97 protected $dispatched = false;
98
99 /**
100 * If this request is a forward because of an error, the original request gets filled.
101 *
102 * @var \TYPO3\CMS\Extbase\Mvc\Request
103 */
104 protected $originalRequest = null;
105
106 /**
107 * If the request is a forward because of an error, these mapping results get filled here.
108 *
109 * @var \TYPO3\CMS\Extbase\Error\Result
110 */
111 protected $originalRequestMappingResults = null;
112
113 /**
114 * Sets the dispatched flag
115 *
116 * @param bool $flag If this request has been dispatched
117 * @api
118 */
119 public function setDispatched($flag)
120 {
121 $this->dispatched = (bool)$flag;
122 }
123
124 /**
125 * If this request has been dispatched and addressed by the responsible
126 * controller and the response is ready to be sent.
127 *
128 * The dispatcher will try to dispatch the request again if it has not been
129 * addressed yet.
130 *
131 * @return bool TRUE if this request has been disptached successfully
132 * @api
133 */
134 public function isDispatched()
135 {
136 return $this->dispatched;
137 }
138
139 /**
140 * Returns the object name of the controller defined by the extension name and
141 * controller name
142 *
143 * @return string The controller's Object Name
144 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchControllerException if the controller does not exist
145 * @api
146 */
147 public function getControllerObjectName()
148 {
149 if (null !== $this->controllerVendorName) {
150 // It's safe to assume a namespaced name as namespaced names have to follow PSR-0
151 $objectName = str_replace(
152 [
153 '@extension',
154 '@subpackage',
155 '@controller',
156 '@vendor',
157 '\\\\'
158 ],
159 [
160 $this->controllerExtensionName,
161 $this->controllerSubpackageKey,
162 $this->controllerName,
163 $this->controllerVendorName,
164 '\\'
165 ],
166 $this->namespacedControllerObjectNamePattern
167 );
168 } else {
169 $objectName = str_replace(
170 [
171 '@extension',
172 '@subpackage',
173 '@controller',
174 '__'
175 ],
176 [
177 $this->controllerExtensionName,
178 $this->controllerSubpackageKey,
179 $this->controllerName,
180 '_'
181 ],
182 $this->controllerObjectNamePattern
183 );
184 }
185 // @todo implement getCaseSensitiveObjectName()
186 if ($objectName === false) {
187 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchControllerException('The controller object "' . $objectName . '" does not exist.', 1220884009);
188 }
189 return $objectName;
190 }
191
192 /**
193 * Explicitly sets the object name of the controller
194 *
195 * @param string $controllerObjectName The fully qualified controller object name
196 */
197 public function setControllerObjectName($controllerObjectName)
198 {
199 $nameParts = ClassNamingUtility::explodeObjectControllerName($controllerObjectName);
200 $this->controllerVendorName = isset($nameParts['vendorName']) ? $nameParts['vendorName'] : null;
201 $this->controllerExtensionName = $nameParts['extensionName'];
202 $this->controllerSubpackageKey = isset($nameParts['subpackageKey']) ? $nameParts['subpackageKey'] : null;
203 $this->controllerName = $nameParts['controllerName'];
204 }
205
206 /**
207 * Sets the plugin name.
208 *
209 * @param string|null $pluginName
210 */
211 public function setPluginName($pluginName = null)
212 {
213 if ($pluginName !== null) {
214 $this->pluginName = $pluginName;
215 }
216 }
217
218 /**
219 * Returns the plugin key.
220 *
221 * @return string The plugin key
222 * @api
223 */
224 public function getPluginName()
225 {
226 return $this->pluginName;
227 }
228
229 /**
230 * Sets the extension name of the controller.
231 *
232 * @param string $controllerExtensionName The extension name.
233 *
234 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException if the extension name is not valid
235 */
236 public function setControllerExtensionName($controllerExtensionName)
237 {
238 if ($controllerExtensionName !== null) {
239 $this->controllerExtensionName = $controllerExtensionName;
240 }
241 }
242
243 /**
244 * Returns the extension name of the specified controller.
245 *
246 * @return string The extension name
247 * @api
248 */
249 public function getControllerExtensionName()
250 {
251 return $this->controllerExtensionName;
252 }
253
254 /**
255 * Returns the extension name of the specified controller.
256 *
257 * @return string The extension key
258 * @api
259 */
260 public function getControllerExtensionKey()
261 {
262 return \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->controllerExtensionName);
263 }
264
265 /**
266 * Sets the subpackage key of the controller.
267 *
268 * @param string $subpackageKey The subpackage key.
269 */
270 public function setControllerSubpackageKey($subpackageKey)
271 {
272 $this->controllerSubpackageKey = $subpackageKey;
273 }
274
275 /**
276 * Returns the subpackage key of the specified controller.
277 * If there is no subpackage key set, the method returns NULL
278 *
279 * @return string The subpackage key
280 */
281 public function getControllerSubpackageKey()
282 {
283 return $this->controllerSubpackageKey;
284 }
285
286 /**
287 * Sets the name of the controller which is supposed to handle the request.
288 * Note: This is not the object name of the controller!
289 *
290 * @param string $controllerName Name of the controller
291 *
292 * @throws Exception\InvalidControllerNameException
293 */
294 public function setControllerName($controllerName)
295 {
296 if (!is_string($controllerName) && $controllerName !== null) {
297 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException('The controller name must be a valid string, ' . gettype($controllerName) . ' given.', 1187176358);
298 }
299 if (strpos($controllerName, '_') !== false) {
300 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException('The controller name must not contain underscores.', 1217846412);
301 }
302 if ($controllerName !== null) {
303 $this->controllerName = $controllerName;
304 }
305 }
306
307 /**
308 * Returns the object name of the controller supposed to handle this request, if one
309 * was set already (if not, the name of the default controller is returned)
310 *
311 * @return string Object name of the controller
312 * @api
313 */
314 public function getControllerName()
315 {
316 return $this->controllerName;
317 }
318
319 /**
320 * Sets the name of the action contained in this request.
321 *
322 * Note that the action name must start with a lower case letter and is case sensitive.
323 *
324 * @param string $actionName Name of the action to execute by the controller
325 *
326 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException if the action name is not valid
327 */
328 public function setControllerActionName($actionName)
329 {
330 if (!is_string($actionName) && $actionName !== null) {
331 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException('The action name must be a valid string, ' . gettype($actionName) . ' given (' . $actionName . ').', 1187176359);
332 }
333 if ($actionName[0] !== strtolower($actionName[0]) && $actionName !== null) {
334 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);
335 }
336 if ($actionName !== null) {
337 $this->controllerActionName = $actionName;
338 }
339 }
340
341 /**
342 * Returns the name of the action the controller is supposed to execute.
343 *
344 * @return string Action name
345 * @api
346 */
347 public function getControllerActionName()
348 {
349 $controllerObjectName = $this->getControllerObjectName();
350 if ($controllerObjectName !== '' && $this->controllerActionName === strtolower($this->controllerActionName)) {
351 $actionMethodName = $this->controllerActionName . 'Action';
352 $classMethods = get_class_methods($controllerObjectName);
353 if (is_array($classMethods)) {
354 foreach ($classMethods as $existingMethodName) {
355 if (strtolower($existingMethodName) === strtolower($actionMethodName)) {
356 $this->controllerActionName = substr($existingMethodName, 0, -6);
357 break;
358 }
359 }
360 }
361 }
362 return $this->controllerActionName;
363 }
364
365 /**
366 * Sets the value of the specified argument
367 *
368 * @param string $argumentName Name of the argument to set
369 * @param mixed $value The new value
370 *
371 * @throws Exception\InvalidArgumentNameException
372 */
373 public function setArgument($argumentName, $value)
374 {
375 if (!is_string($argumentName) || $argumentName === '') {
376 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException('Invalid argument name.', 1210858767);
377 }
378 if ($argumentName[0] === '_' && $argumentName[1] === '_') {
379 $this->internalArguments[$argumentName] = $value;
380 return;
381 }
382 if (!in_array($argumentName, ['@extension', '@subpackage', '@controller', '@action', '@format', '@vendor'], true)) {
383 $this->arguments[$argumentName] = $value;
384 }
385 }
386
387 /**
388 * sets the VendorName
389 *
390 * @param string $vendorName
391 */
392 public function setControllerVendorName($vendorName)
393 {
394 $this->controllerVendorName = $vendorName;
395 }
396
397 /**
398 * get the VendorName
399 *
400 * @return string
401 */
402 public function getControllerVendorName()
403 {
404 return $this->controllerVendorName;
405 }
406
407 /**
408 * Sets the whole arguments array and therefore replaces any arguments
409 * which existed before.
410 *
411 * @param array $arguments An array of argument names and their values
412 */
413 public function setArguments(array $arguments)
414 {
415 $this->arguments = [];
416 foreach ($arguments as $argumentName => $argumentValue) {
417 $this->setArgument($argumentName, $argumentValue);
418 }
419 }
420
421 /**
422 * Returns an array of arguments and their values
423 *
424 * @return array Associative array of arguments and their values (which may be arguments and values as well)
425 * @api
426 */
427 public function getArguments()
428 {
429 return $this->arguments;
430 }
431
432 /**
433 * Returns the value of the specified argument
434 *
435 * @param string $argumentName Name of the argument
436 *
437 * @return string Value of the argument
438 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if such an argument does not exist
439 * @api
440 */
441 public function getArgument($argumentName)
442 {
443 if (!isset($this->arguments[$argumentName])) {
444 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException('An argument "' . $argumentName . '" does not exist for this request.', 1176558158);
445 }
446 return $this->arguments[$argumentName];
447 }
448
449 /**
450 * Checks if an argument of the given name exists (is set)
451 *
452 * @param string $argumentName Name of the argument to check
453 *
454 * @return bool TRUE if the argument is set, otherwise FALSE
455 * @api
456 */
457 public function hasArgument($argumentName)
458 {
459 return isset($this->arguments[$argumentName]);
460 }
461
462 /**
463 * Sets the requested representation format
464 *
465 * @param string $format The desired format, something like "html", "xml", "png", "json" or the like. Can even be something like "rss.xml".
466 */
467 public function setFormat($format)
468 {
469 $this->format = $format;
470 }
471
472 /**
473 * Returns the requested representation format
474 *
475 * @return string The desired format, something like "html", "xml", "png", "json" or the like.
476 * @api
477 */
478 public function getFormat()
479 {
480 return $this->format;
481 }
482
483 /**
484 * Returns the original request. Filled only if a property mapping error occurred.
485 *
486 * @return \TYPO3\CMS\Extbase\Mvc\Request the original request.
487 */
488 public function getOriginalRequest()
489 {
490 return $this->originalRequest;
491 }
492
493 /**
494 * @param \TYPO3\CMS\Extbase\Mvc\Request $originalRequest
495 */
496 public function setOriginalRequest(\TYPO3\CMS\Extbase\Mvc\Request $originalRequest)
497 {
498 $this->originalRequest = $originalRequest;
499 }
500
501 /**
502 * Get the request mapping results for the original request.
503 *
504 * @return \TYPO3\CMS\Extbase\Error\Result
505 */
506 public function getOriginalRequestMappingResults()
507 {
508 if ($this->originalRequestMappingResults === null) {
509 return new \TYPO3\CMS\Extbase\Error\Result();
510 }
511 return $this->originalRequestMappingResults;
512 }
513
514 /**
515 * @param \TYPO3\CMS\Extbase\Error\Result $originalRequestMappingResults
516 */
517 public function setOriginalRequestMappingResults(\TYPO3\CMS\Extbase\Error\Result $originalRequestMappingResults)
518 {
519 $this->originalRequestMappingResults = $originalRequestMappingResults;
520 }
521
522 /**
523 * Get the internal arguments of the request, i.e. every argument starting
524 * with two underscores.
525 *
526 * @return array
527 */
528 public function getInternalArguments()
529 {
530 return $this->internalArguments;
531 }
532
533 /**
534 * Returns the value of the specified argument
535 *
536 * @param string $argumentName Name of the argument
537 *
538 * @return string Value of the argument, or NULL if not set.
539 */
540 public function getInternalArgument($argumentName)
541 {
542 if (!isset($this->internalArguments[$argumentName])) {
543 return null;
544 }
545 return $this->internalArguments[$argumentName];
546 }
547 }