[TASK][WIP] Move non class code back to source files
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Dispatcher.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Dispatches requests to the controller which was specified by the request and
29 * returns the response the controller generated.
30 */
31 class Tx_Extbase_MVC_Dispatcher implements t3lib_Singleton {
32
33 /**
34 * @var Tx_Extbase_Object_ObjectManagerInterface A reference to the object manager
35 */
36 protected $objectManager;
37
38 /**
39 * @var Tx_Extbase_Reflection_Service
40 */
41 protected $reflectionService;
42
43 /**
44 * @var Tx_Extbase_SignalSlot_Dispatcher
45 */
46 protected $signalSlotDispatcher;
47
48 /**
49 * @var array
50 */
51 protected $settings = array();
52
53 /**
54 * Constructs the global dispatcher
55 *
56 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager A reference to the object manager
57 */
58 public function __construct(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
59 $this->objectManager = $objectManager;
60 }
61
62 /**
63 * Injects the Reflection Service
64 *
65 * @param Tx_Extbase_Reflection_Service $reflectionService
66 * @return void
67 */
68 public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
69 $this->reflectionService = $reflectionService;
70 }
71
72 /**
73 * Injects the SignalSlotDispatcher Service
74 *
75 * @param Tx_Extbase_SignalSlot_Dispatcher $signalSlotDispatcher
76 */
77 public function injectSignalSlotDispatcher(Tx_Extbase_SignalSlot_Dispatcher $signalSlotDispatcher) {
78 $this->signalSlotDispatcher = $signalSlotDispatcher;
79 }
80
81 /**
82 * Dispatches a request to a controller and initializes the security framework.
83 *
84 * @param Tx_Extbase_MVC_RequestInterface $request The request to dispatch
85 * @param Tx_Extbase_MVC_ResponseInterface $response The response, to be modified by the controller
86 * @return void
87 */
88 public function dispatch(Tx_Extbase_MVC_RequestInterface $request, Tx_Extbase_MVC_ResponseInterface $response) {
89 $dispatchLoopCount = 0;
90 while (!$request->isDispatched()) {
91 if ($dispatchLoopCount++ > 99) {
92 throw new Tx_Extbase_MVC_Exception_InfiniteLoop(('Could not ultimately dispatch the request after ' . $dispatchLoopCount) . ' iterations. Most probably, a @dontvalidate annotation is missing on re-displaying a form with validation errors.', 1217839467);
93 }
94 $controller = $this->resolveController($request);
95 try {
96 $controller->processRequest($request, $response);
97 } catch (Tx_Extbase_MVC_Exception_StopAction $ignoredException) {
98
99 }
100 }
101 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRequestDispatch', array('request' => $request, 'response' => $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 Tx_Extbase_MVC_RequestInterface $request The request to dispatch
109 * @return Tx_Extbase_MVC_Controller_ControllerInterface
110 * @author Bastian Waidelich <bastian@typo3.org>
111 * @author Robert Lemke <robert@typo3.org>
112 */
113 protected function resolveController(Tx_Extbase_MVC_RequestInterface $request) {
114 $controllerObjectName = $request->getControllerObjectName();
115 $controller = $this->objectManager->get($controllerObjectName);
116 if (!$controller instanceof Tx_Extbase_MVC_Controller_ControllerInterface) {
117 throw new Tx_Extbase_MVC_Exception_InvalidController(('Invalid controller "' . $request->getControllerObjectName()) . '". The controller must implement the Tx_Extbase_MVC_Controller_ControllerInterface.', 1202921619);
118 }
119 return $controller;
120 }
121
122 }
123
124 ?>