[TASK] Update copyright year to 2013
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Dispatcher.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Dispatches requests to the controller which was specified by the request and
32 * returns the response the controller generated.
33 */
34 class Dispatcher implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface A reference to the object manager
38 */
39 protected $objectManager;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
43 */
44 protected $reflectionService;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
48 */
49 protected $signalSlotDispatcher;
50
51 /**
52 * @var array
53 */
54 protected $settings = array();
55
56 /**
57 * Constructs the global dispatcher
58 *
59 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
60 */
61 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
62 $this->objectManager = $objectManager;
63 }
64
65 /**
66 * Injects the Reflection Service
67 *
68 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
69 * @return void
70 */
71 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
72 $this->reflectionService = $reflectionService;
73 }
74
75 /**
76 * Injects the SignalSlotDispatcher Service
77 *
78 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
79 */
80 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher) {
81 $this->signalSlotDispatcher = $signalSlotDispatcher;
82 }
83
84 /**
85 * Dispatches a request to a controller and initializes the security framework.
86 *
87 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
88 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, to be modified by the controller
89 * @throws Exception\InfiniteLoopException
90 * @return void
91 */
92 public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
93 $dispatchLoopCount = 0;
94 while (!$request->isDispatched()) {
95 if ($dispatchLoopCount++ > 99) {
96 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InfiniteLoopException('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);
97 }
98 $controller = $this->resolveController($request);
99 try {
100 $controller->processRequest($request, $response);
101 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $ignoredException) {
102 }
103 }
104 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRequestDispatch', array('request' => $request, 'response' => $response));
105 }
106
107 /**
108 * Finds and instanciates a controller that matches the current request.
109 * If no controller can be found, an instance of NotFoundControllerInterface is returned.
110 *
111 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
112 * @throws Exception\InvalidControllerException
113 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
114 */
115 protected function resolveController(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request) {
116 $controllerObjectName = $request->getControllerObjectName();
117 $controller = $this->objectManager->get($controllerObjectName);
118 if (!$controller instanceof \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface) {
119 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.', 1202921619);
120 }
121 return $controller;
122 }
123 }
124
125 ?>