TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
core/Classes/Http/Dispatcher.php
Go to the documentation of this file.
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;
20 
28 {
37  public function dispatch(ServerRequestInterface $request, ResponseInterface $response)
38  {
39  $targetIdentifier = $request->getAttribute('target');
40  $target = $this->getCallableFromTarget($targetIdentifier);
41  return call_user_func_array($target, [$request, $response]);
42  }
43 
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  // This needs to be checked at last as a string with object::method is recognize 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 }
dispatch(ServerRequestInterface $request, ResponseInterface $response)
static makeInstance($className,...$constructorArguments)