[!!!][TASK] Remove $response preparation in route dispatching
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / Dispatcher.php
1 <?php
2 namespace TYPO3\CMS\Core\Http;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Dispatcher which resolves a target, which was given to the request to call a controller and method (but also a callable)
23 * where the request contains a "target" as attribute.
24 *
25 * Used in eID Frontend Requests, see EidHandler
26 */
27 class Dispatcher implements DispatcherInterface
28 {
29 /**
30 * Main method that fetches the target from the request and calls the target directly
31 *
32 * @param ServerRequestInterface $request the current server request
33 * @return ResponseInterface the filled response by the callable/controller/action
34 * @throws \InvalidArgumentException if the defined target is invalid
35 */
36 public function dispatch(ServerRequestInterface $request): ResponseInterface
37 {
38 $targetIdentifier = $request->getAttribute('target');
39 $target = $this->getCallableFromTarget($targetIdentifier);
40 $arguments = [$request];
41 return call_user_func_array($target, $arguments);
42 }
43
44 /**
45 * Creates a callable out of the given parameter, which can be a string, a callable / closure or an array
46 * which can be handed to call_user_func_array()
47 *
48 * @param array|string|callable $target the target which is being resolved.
49 * @return callable
50 * @throws \InvalidArgumentException
51 */
52 protected function getCallableFromTarget($target)
53 {
54 if (is_array($target)) {
55 return $target;
56 }
57
58 if (is_object($target) && $target instanceof \Closure) {
59 return $target;
60 }
61
62 // Only a class name is given
63 if (is_string($target) && strpos($target, ':') === false) {
64 $targetObject = GeneralUtility::makeInstance($target);
65 if (!method_exists($targetObject, '__invoke')) {
66 throw new \InvalidArgumentException('Object "' . $target . '" doesn\'t implement an __invoke() method and cannot be used as target.', 1442431631);
67 }
68 return $targetObject;
69 }
70
71 // Check if the target is a concatenated string of "className::actionMethod"
72 if (is_string($target) && strpos($target, '::') !== false) {
73 list($className, $methodName) = explode('::', $target, 2);
74 $targetObject = GeneralUtility::makeInstance($className);
75 return [$targetObject, $methodName];
76 }
77
78 // Closures needs to be checked at last as a string with object::method is recognized as callable
79 if (is_callable($target)) {
80 return $target;
81 }
82
83 throw new \InvalidArgumentException('Invalid target for "' . $target . '", as it is not callable.', 1425381442);
84 }
85 }