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