‪TYPO3CMS  10.4
Dispatcher.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Http;
17 
18 use Psr\Container\ContainerInterface;
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
22 
30 {
34  protected ‪$container;
35 
36  public function ‪__construct(ContainerInterface ‪$container)
37  {
38  $this->container = ‪$container;
39  }
40 
48  public function ‪dispatch(ServerRequestInterface $request): ResponseInterface
49  {
50  $targetIdentifier = $request->getAttribute('target');
51  $target = $this->‪getCallableFromTarget($targetIdentifier);
52  $arguments = [$request];
53  return call_user_func_array($target, $arguments);
54  }
55 
64  protected function ‪getCallableFromTarget($target)
65  {
66  if (is_array($target)) {
67  return $target;
68  }
69 
70  if (is_object($target) && $target instanceof \Closure) {
71  return $target;
72  }
73 
74  // Only a class name is given
75  if (is_string($target) && strpos($target, ':') === false) {
76  $targetObject = $this->container->has($target) ? $this->container->get($target) : GeneralUtility::makeInstance($target);
77  if (!method_exists($targetObject, '__invoke')) {
78  throw new \InvalidArgumentException('Object "' . $target . '" doesn\'t implement an __invoke() method and cannot be used as target.', 1442431631);
79  }
80  return $targetObject;
81  }
82 
83  // Check if the target is a concatenated string of "className::actionMethod"
84  if (is_string($target) && strpos($target, '::') !== false) {
85  [$className, $methodName] = explode('::', $target, 2);
86  $targetObject = $this->container->has($className) ? $this->container->get($className) : GeneralUtility::makeInstance($className);
87  return [$targetObject, $methodName];
88  }
89 
90  // Closures needs to be checked at last as a string with object::method is recognized as callable
91  if (is_callable($target)) {
92  return $target;
93  }
94 
95  throw new \InvalidArgumentException('Invalid target for "' . $target . '", as it is not callable.', 1425381442);
96  }
97 }
‪TYPO3\CMS\Core\Http\Dispatcher\__construct
‪__construct(ContainerInterface $container)
Definition: Dispatcher.php:35
‪TYPO3\CMS\Core\Http\Dispatcher
Definition: Dispatcher.php:30
‪TYPO3\CMS\Core\Http\Dispatcher\$container
‪ContainerInterface $container
Definition: Dispatcher.php:33
‪TYPO3\CMS\Core\Http\Dispatcher\getCallableFromTarget
‪callable getCallableFromTarget($target)
Definition: Dispatcher.php:63
‪TYPO3\CMS\Core\Http\DispatcherInterface
Definition: DispatcherInterface.php:30
‪TYPO3\CMS\Core\Http\Dispatcher\dispatch
‪ResponseInterface dispatch(ServerRequestInterface $request)
Definition: Dispatcher.php:47
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18