‪TYPO3CMS  ‪main
Dispatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Extbase\Mvc;
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\EventDispatcher\EventDispatcherInterface;
22 use Psr\Http\Message\ResponseInterface;
29 
36 {
37  private ContainerInterface ‪$container;
38  protected EventDispatcherInterface ‪$eventDispatcher;
39 
40  public function ‪__construct(
41  ContainerInterface ‪$container,
42  EventDispatcherInterface ‪$eventDispatcher
43  ) {
44  $this->container = ‪$container;
45  $this->eventDispatcher = ‪$eventDispatcher;
46  }
47 
54  public function ‪dispatch(‪RequestInterface $request): ResponseInterface
55  {
56  $dispatchLoopCount = 0;
57  $isDispatched = false;
58  while (!$isDispatched) {
59  if ($dispatchLoopCount++ > 99) {
60  throw new ‪InfiniteLoopException(
61  'Could not ultimately dispatch the request after ' . $dispatchLoopCount
62  . ' iterations. Most probably, a @' . IgnoreValidation::class
63  . ' annotation is missing on re-displaying a form with validation errors.',
64  1217839467
65  );
66  }
67  $controller = $this->‪resolveController($request);
68  $response = $controller->processRequest($request);
69  if ($response instanceof ‪ForwardResponse) {
70  // The controller action returned an extbase internal Forward response:
71  // Another action should be dispatched.
72  $request = static::buildRequestFromCurrentRequestAndForwardResponse($request, $response);
73  } else {
74  // The controller action returned a casual or a HTTP redirect response.
75  // Dispatching ends here and response is sent to client.
76  $isDispatched = true;
77  }
78  }
79  $this->eventDispatcher->dispatch(new ‪AfterRequestDispatchedEvent($request, $response));
80  return $response;
81  }
82 
91  protected function ‪resolveController(‪RequestInterface $request)
92  {
93  $controllerObjectName = $request->‪getControllerObjectName();
94  $controller = $this->container->get($controllerObjectName);
95  if (!$controller instanceof ‪ControllerInterface) {
97  'Invalid controller "' . $request->‪getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.',
98  1476109646
99  );
100  }
101  return $controller;
102  }
103 
109  {
110  $request = $currentRequest->‪withControllerActionName($forwardResponse->‪getActionName());
111  if ($forwardResponse->‪getControllerName() !== null) {
112  $request = $request->withControllerName($forwardResponse->‪getControllerName());
113  }
114  if ($forwardResponse->‪getExtensionName() !== null) {
115  $request = $request->withControllerExtensionName($forwardResponse->‪getExtensionName());
116  }
117  if ($forwardResponse->‪getArguments() !== null) {
118  $request = $request->withArguments($forwardResponse->‪getArguments());
119  }
121  $extbaseRequestParameters = clone $request->getAttribute('extbase');
122  $extbaseRequestParameters->setOriginalRequest($currentRequest);
123  $extbaseRequestParameters->setOriginalRequestMappingResults($forwardResponse->‪getArgumentsValidationResult());
124  return $request->withAttribute('extbase', $extbaseRequestParameters);
125  }
126 }
‪TYPO3\CMS\Extbase\Annotation\IgnoreValidation
Definition: IgnoreValidation.php:26
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getControllerName
‪getControllerName()
Definition: ForwardResponse.php:90
‪TYPO3\CMS\Extbase\Mvc\Dispatcher
Definition: Dispatcher.php:36
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\getControllerObjectName
‪getControllerObjectName()
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getActionName
‪getActionName()
Definition: ForwardResponse.php:85
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getArgumentsValidationResult
‪getArgumentsValidationResult()
Definition: ForwardResponse.php:105
‪TYPO3\CMS\Extbase\Mvc
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\dispatch
‪dispatch(RequestInterface $request)
Definition: Dispatcher.php:54
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\buildRequestFromCurrentRequestAndForwardResponse
‪static buildRequestFromCurrentRequestAndForwardResponse(RequestInterface $currentRequest, ForwardResponse $forwardResponse)
Definition: Dispatcher.php:108
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\resolveController
‪Controller ControllerInterface resolveController(RequestInterface $request)
Definition: Dispatcher.php:91
‪TYPO3\CMS\Extbase\Mvc\RequestInterface\withControllerActionName
‪withControllerActionName(string $actionName)
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getExtensionName
‪getExtensionName()
Definition: ForwardResponse.php:95
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$container
‪ContainerInterface $container
Definition: Dispatcher.php:37
‪TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException
Definition: InvalidControllerException.php:25
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
Definition: ControllerInterface.php:25
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\__construct
‪__construct(ContainerInterface $container, EventDispatcherInterface $eventDispatcher)
Definition: Dispatcher.php:40
‪TYPO3\CMS\Extbase\Http\ForwardResponse\getArguments
‪getArguments()
Definition: ForwardResponse.php:100
‪TYPO3\CMS\Extbase\Event\Mvc\AfterRequestDispatchedEvent
Definition: AfterRequestDispatchedEvent.php:27
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: Dispatcher.php:38
‪TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException
Definition: InfiniteLoopException.php:25