[BUGFIX] Fix PHP 8.0 issues in EXT:setup
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Dispatcher.php
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\Extbase\Mvc;
17
18 use Psr\Container\ContainerInterface;
19 use Psr\EventDispatcher\EventDispatcherInterface;
20 use Psr\Http\Message\ResponseInterface;
21 use TYPO3\CMS\Core\SingletonInterface;
22 use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
23 use TYPO3\CMS\Extbase\Event\Mvc\AfterRequestDispatchedEvent;
24 use TYPO3\CMS\Extbase\Http\ForwardResponse;
25 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface;
26 use TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException;
27 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException;
28 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
29 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
30
31 /**
32 * Dispatches requests to the controller which was specified by the request and
33 * returns the response the controller generated.
34 * @internal only to be used within Extbase, not part of TYPO3 Core API.
35 */
36 class Dispatcher implements SingletonInterface
37 {
38 /**
39 * @var ObjectManagerInterface A reference to the object manager
40 */
41 protected $objectManager;
42
43 /**
44 * @var ContainerInterface
45 */
46 private $container;
47
48 /**
49 * @var EventDispatcherInterface
50 */
51 protected $eventDispatcher;
52
53 /**
54 * @var array
55 */
56 protected $settings = [];
57
58 /**
59 * Constructs the global dispatcher
60 *
61 * @param ObjectManagerInterface $objectManager A reference to the object manager
62 * @param ContainerInterface $container
63 * @param EventDispatcherInterface $eventDispatcher
64 */
65 public function __construct(
66 ObjectManagerInterface $objectManager,
67 ContainerInterface $container,
68 EventDispatcherInterface $eventDispatcher
69 ) {
70 $this->objectManager = $objectManager;
71 $this->container = $container;
72 $this->eventDispatcher = $eventDispatcher;
73 }
74
75 /**
76 * Dispatches a request to a controller and initializes the security framework.
77 *
78 * @param RequestInterface $request The request to dispatch
79 * @return ResponseInterface
80 * @throws Exception\InfiniteLoopException
81 */
82 public function dispatch(RequestInterface $request): ResponseInterface
83 {
84 $dispatchLoopCount = 0;
85 while (!$request->isDispatched()) {
86 if ($dispatchLoopCount++ > 99) {
87 throw new InfiniteLoopException('Could not ultimately dispatch the request after ' . $dispatchLoopCount . ' iterations. Most probably, a @' . IgnoreValidation::class . ' annotation is missing on re-displaying a form with validation errors.', 1217839467);
88 }
89 $controller = $this->resolveController($request);
90 try {
91 $response = $controller->processRequest($request);
92 if ($response instanceof ForwardResponse) {
93 $request = static::buildRequestFromCurrentRequestAndForwardResponse($request, $response);
94 }
95 } catch (StopActionException $ignoredException) {
96 $response = $ignoredException->getResponse();
97 }
98 }
99
100 if (!isset($response)) {
101 // This fallback is no longer needed once the StopActionException is removed and replaced with proper
102 // redirect and forward response objects and we always get a response back from the controller.
103 $response = new \TYPO3\CMS\Core\Http\Response();
104 }
105
106 $this->eventDispatcher->dispatch(new AfterRequestDispatchedEvent($request, $response));
107 return $response;
108 }
109
110 /**
111 * Finds and instantiates a controller that matches the current request.
112 * If no controller can be found, an instance of NotFoundControllerInterface is returned.
113 *
114 * @param RequestInterface $request The request to dispatch
115 * @return Controller\ControllerInterface
116 * @throws Exception\InvalidControllerException
117 */
118 protected function resolveController(RequestInterface $request)
119 {
120 $controllerObjectName = $request->getControllerObjectName();
121 if ($this->container->has($controllerObjectName)) {
122 $controller = $this->container->get($controllerObjectName);
123 } else {
124 $controller = $this->objectManager->get($controllerObjectName);
125 }
126 if (!$controller instanceof ControllerInterface) {
127 throw new InvalidControllerException(
128 'Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.',
129 1476109646
130 );
131 }
132 return $controller;
133 }
134
135 /**
136 * @internal only to be used within Extbase, not part of TYPO3 Core API.
137 * @todo: make this a private method again as soon as the tests, that fake the dispatching of requests, are refactored.
138 */
139 public static function buildRequestFromCurrentRequestAndForwardResponse(Request $currentRequest, ForwardResponse $forwardResponse): Request
140 {
141 $request = clone $currentRequest;
142 $request->setDispatched(false);
143 $request->setControllerActionName($forwardResponse->getActionName());
144
145 if ($forwardResponse->getControllerName() !== null) {
146 $request->setControllerName($forwardResponse->getControllerName());
147 }
148
149 if ($forwardResponse->getExtensionName() !== null) {
150 $request->setControllerExtensionName($forwardResponse->getExtensionName());
151 }
152
153 if ($forwardResponse->getArguments() !== []) {
154 $request->setArguments($forwardResponse->getArguments());
155 }
156
157 $request->setOriginalRequest($currentRequest);
158 $request->setOriginalRequestMappingResults($forwardResponse->getArgumentsValidationResult());
159
160 return $request;
161 }
162 }