[TASK] Have eIDs with PSR-7 without ControllerInterface
[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 EidRequestHandler
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 * @param ResponseInterface $response the prepared response
34 * @return ResponseInterface the filled response by the callable / controller/action
35 * @throws \InvalidArgumentException if the defined target is invalid
36 */
37 public function dispatch(ServerRequestInterface $request, ResponseInterface $response) {
38 $targetIdentifier = $request->getAttribute('target');
39 $target = $this->getCallableFromTarget($targetIdentifier);
40 return call_user_func_array($target, array($request, $response));
41 }
42
43 /**
44 * Creates a callable out of the given parameter, which can be a string, a callable / closure or an array
45 * which can be handed to call_user_func_array()
46 *
47 * @param array|string|callable $target the target which is being resolved.
48 * @return callable
49 * @throws \InvalidArgumentException
50 */
51 protected function getCallableFromTarget($target) {
52 if (is_array($target)) {
53 return $target;
54 }
55
56 if (is_object($target) && $target instanceof \Closure) {
57 return $target;
58 }
59
60 // Only a class name is given
61 if (is_string($target) && strpos($target, ':') === FALSE) {
62 $target = GeneralUtility::makeInstance($target);
63 if (method_exists($target, '__invoke')) {
64 return $target;
65 }
66 }
67
68 // Check if the target is a concatenated string of "className::actionMethod"
69 if (is_string($target) && strpos($target, '::') !== FALSE) {
70 list($className, $methodName) = explode('::', $target, 2);
71 $targetObject = GeneralUtility::makeInstance($className);
72 return [$targetObject, $methodName];
73 }
74
75 // This needs to be checked at last as a string with object::method is recognize as callable
76 if (is_callable($target)) {
77 return $target;
78 }
79
80 throw new \InvalidArgumentException('Invalid target for "' . $target. '", as it is not callable.', 1425381442);
81 }
82 }