‪TYPO3CMS  11.5
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\Extbase\Mvc;
17 
18 use Psr\Container\ContainerInterface;
19 use Psr\EventDispatcher\EventDispatcherInterface;
20 use Psr\Http\Message\ResponseInterface;
31 
38 {
43  protected ‪$objectManager;
44 
48  private ‪$container;
49 
53  protected ‪$eventDispatcher;
54 
58  protected ‪$settings = [];
59 
67  public function ‪__construct(
69  ContainerInterface ‪$container,
70  EventDispatcherInterface ‪$eventDispatcher
71  ) {
72  // @deprecated since v11, will be removed in v12
73  $this->objectManager = ‪$objectManager;
74  $this->container = ‪$container;
75  $this->eventDispatcher = ‪$eventDispatcher;
76  }
77 
85  public function ‪dispatch(‪RequestInterface $request): ResponseInterface
86  {
87  $dispatchLoopCount = 0;
88  $isDispatched = false;
89  // @deprecated since v11, will be changed in v12 to: while (!$isDispatched) {
90  while (!$isDispatched || !$request->isDispatched()) {
91  if ($dispatchLoopCount++ > 99) {
92  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);
93  }
94  $controller = $this->‪resolveController($request);
95  try {
96  $response = $controller->processRequest($request);
97  if ($response instanceof ForwardResponse) {
98  // The controller action returned an extbase internal Forward response:
99  // Another action should be dispatched.
100  $request = static::buildRequestFromCurrentRequestAndForwardResponse($request, $response);
101  } elseif ($response instanceof RedirectResponse) {
102  // The controller action returned a core HTTP redirect response.
103  // Dispatching ends here and response is sent to client.
104  $isDispatched = true;
105  } else {
106  // Some casual response returned by action.
107  // Dispatching ends here and response is sent to client.
108  $isDispatched = true;
109  }
110  } catch (StopActionException $ignoredException) {
111  // @deprecated since v11, will be removed in v12
112  $isDispatched = true;
113  $response = $ignoredException->getResponse();
114  }
115  }
116 
117  $this->eventDispatcher->dispatch(new AfterRequestDispatchedEvent($request, $response));
118  return $response;
119  }
120 
129  protected function ‪resolveController(RequestInterface $request)
130  {
131  $controllerObjectName = $request->getControllerObjectName();
132  if ($this->container->has($controllerObjectName)) {
133  $controller = $this->container->get($controllerObjectName);
134  } else {
135  // @deprecated since v11, will be removed in v12.
136  $controller = $this->objectManager->get($controllerObjectName);
137  }
138  if (!$controller instanceof ControllerInterface) {
139  throw new InvalidControllerException(
140  'Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.',
141  1476109646
142  );
143  }
144  return $controller;
145  }
146 
151  public static function ‪buildRequestFromCurrentRequestAndForwardResponse(Request $currentRequest, ForwardResponse $forwardResponse): Request
152  {
153  $extbaseAttribute = clone $currentRequest->getAttribute('extbase');
154  $request = $currentRequest->withAttribute('extbase', $extbaseAttribute);
155 
156  // @deprecated since v11, will be removed in v12.
157  $request->setDispatched(false);
158  $request->setControllerActionName($forwardResponse->getActionName());
159 
160  if ($forwardResponse->getControllerName() !== null) {
161  $request->setControllerName($forwardResponse->getControllerName());
162  }
163 
164  if ($forwardResponse->getExtensionName() !== null) {
165  $request->setControllerExtensionName($forwardResponse->getExtensionName());
166  }
167 
168  if ($forwardResponse->getArguments() !== null) {
169  $request->setArguments($forwardResponse->getArguments());
170  }
171 
172  $request->setOriginalRequest($currentRequest);
173  $request->setOriginalRequestMappingResults($forwardResponse->getArgumentsValidationResult());
174 
175  return $request;
176  }
177 }
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:37
‪TYPO3\CMS\Extbase\Annotation\IgnoreValidation
Definition: IgnoreValidation.php:25
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\dispatch
‪ResponseInterface dispatch(RequestInterface $request)
Definition: Dispatcher.php:81
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getControllerName
‪getControllerName()
Definition: ForwardResponse.php:92
‪TYPO3\CMS\Extbase\Mvc\Dispatcher
Definition: Dispatcher.php:38
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getActionName
‪getActionName()
Definition: ForwardResponse.php:87
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getArgumentsValidationResult
‪getArgumentsValidationResult()
Definition: ForwardResponse.php:107
‪TYPO3\CMS\Extbase\Mvc
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:29
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\resolveController
‪Controller ControllerInterface resolveController(RequestInterface $request)
Definition: Dispatcher.php:125
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\getControllerObjectName
‪string getControllerObjectName()
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$objectManager
‪ObjectManagerInterface $objectManager
Definition: Dispatcher.php:42
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getExtensionName
‪getExtensionName()
Definition: ForwardResponse.php:97
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\buildRequestFromCurrentRequestAndForwardResponse
‪static buildRequestFromCurrentRequestAndForwardResponse(Request $currentRequest, ForwardResponse $forwardResponse)
Definition: Dispatcher.php:147
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$container
‪ContainerInterface $container
Definition: Dispatcher.php:46
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException
Definition: InvalidControllerException.php:25
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:27
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\__construct
‪__construct(ObjectManagerInterface $objectManager, ContainerInterface $container, EventDispatcherInterface $eventDispatcher)
Definition: Dispatcher.php:63
‪TYPO3\CMS\Extbase\Mvc\Request\withAttribute
‪withAttribute($name, $value)
Definition: Request.php:567
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
Definition: ControllerInterface.php:25
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$settings
‪array $settings
Definition: Dispatcher.php:54
‪TYPO3\CMS\Extbase\Mvc\Request\getAttribute
‪getAttribute($name, $default=null)
Definition: Request.php:559
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getArguments
‪getArguments()
Definition: ForwardResponse.php:102
‪TYPO3\CMS\Extbase\Event\Mvc\AfterRequestDispatchedEvent
Definition: AfterRequestDispatchedEvent.php:27
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:39
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException\getResponse
‪getResponse()
Definition: StopActionException.php:51
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: Dispatcher.php:50
‪TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException
Definition: InfiniteLoopException.php:25