[TASK] Streamline return tags in phpdocs
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Dispatcher.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Dispatches requests to the controller which was specified by the request and
19 * returns the response the controller generated.
20 */
21 class Dispatcher implements \TYPO3\CMS\Core\SingletonInterface
22 {
23 /**
24 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface A reference to the object manager
25 */
26 protected $objectManager;
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
30 */
31 protected $reflectionService;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
35 */
36 protected $signalSlotDispatcher;
37
38 /**
39 * @var array
40 */
41 protected $settings = [];
42
43 /**
44 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
45 */
46 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
47 {
48 $this->reflectionService = $reflectionService;
49 }
50
51 /**
52 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
53 */
54 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
55 {
56 $this->signalSlotDispatcher = $signalSlotDispatcher;
57 }
58
59 /**
60 * Constructs the global dispatcher
61 *
62 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
63 */
64 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
65 {
66 $this->objectManager = $objectManager;
67 }
68
69 /**
70 * Dispatches a request to a controller and initializes the security framework.
71 *
72 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
73 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, to be modified by the controller
74 * @throws Exception\InfiniteLoopException
75 */
76 public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
77 {
78 $dispatchLoopCount = 0;
79 while (!$request->isDispatched()) {
80 if ($dispatchLoopCount++ > 99) {
81 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException('Could not ultimately dispatch the request after ' . $dispatchLoopCount . ' iterations. Most probably, a @ignorevalidation annotation is missing on re-displaying a form with validation errors.', 1217839467);
82 }
83 $controller = $this->resolveController($request);
84 try {
85 $controller->processRequest($request, $response);
86 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $ignoredException) {
87 }
88 }
89 $this->emitAfterRequestDispatchSignal($request, $response);
90 }
91
92 /**
93 * Emits a signal after a request was dispatched
94 *
95 * @param RequestInterface $request
96 * @param ResponseInterface $response
97 */
98 protected function emitAfterRequestDispatchSignal(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
99 {
100 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRequestDispatch', [$request, $response]);
101 }
102
103 /**
104 * Finds and instanciates a controller that matches the current request.
105 * If no controller can be found, an instance of NotFoundControllerInterface is returned.
106 *
107 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
108 * @throws Exception\InvalidControllerException
109 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
110 */
111 protected function resolveController(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
112 {
113 $controllerObjectName = $request->getControllerObjectName();
114 $controller = $this->objectManager->get($controllerObjectName);
115 if (!$controller instanceof \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface) {
116 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException(
117 'Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.',
118 1476109646
119 );
120 }
121 return $controller;
122 }
123 }