[TASK] Make TYPO3 Core PSR-2 standard compliant
[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 = array();
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 * @return void
76 */
77 public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
78 {
79 $dispatchLoopCount = 0;
80 while (!$request->isDispatched()) {
81 if ($dispatchLoopCount++ > 99) {
82 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException('Could not ultimately dispatch the request after ' . $dispatchLoopCount . ' iterations. Most probably, a @ignorevalidation or @dontvalidate (old propertymapper) annotation is missing on re-displaying a form with validation errors.', 1217839467);
83 }
84 $controller = $this->resolveController($request);
85 try {
86 $controller->processRequest($request, $response);
87 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $ignoredException) {
88 }
89 }
90 $this->emitAfterRequestDispatchSignal($request, $response);
91 }
92
93 /**
94 * Emits a signal after a request was dispatched
95 *
96 * @param RequestInterface $request
97 * @param ResponseInterface $response
98 */
99 protected function emitAfterRequestDispatchSignal(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
100 {
101 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRequestDispatch', array($request, $response));
102 }
103
104 /**
105 * Finds and instanciates a controller that matches the current request.
106 * If no controller can be found, an instance of NotFoundControllerInterface is returned.
107 *
108 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
109 * @throws Exception\InvalidControllerException
110 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
111 */
112 protected function resolveController(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
113 {
114 $controllerObjectName = $request->getControllerObjectName();
115 $controller = $this->objectManager->get($controllerObjectName);
116 if (!$controller instanceof \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface) {
117 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.', 1202921619);
118 }
119 return $controller;
120 }
121 }