[TASK] Replace inject methods with @inject; allow @Flow\Inject
[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 * @inject
44 */
45 protected $reflectionService;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
49 * @inject
50 */
51 protected $signalSlotDispatcher;
52
53 /**
54 * @var array
55 */
56 protected $settings = array();
57
58 /**
59 * Constructs the global dispatcher
60 *
61 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
62 */
63 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
64 $this->objectManager = $objectManager;
65 }
66
67 /**
68 * Dispatches a request to a controller and initializes the security framework.
69 *
70 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
71 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, to be modified by the controller
72 * @throws Exception\InfiniteLoopException
73 * @return void
74 */
75 public function dispatch(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
76 $dispatchLoopCount = 0;
77 while (!$request->isDispatched()) {
78 if ($dispatchLoopCount++ > 99) {
79 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);
80 }
81 $controller = $this->resolveController($request);
82 try {
83 $controller->processRequest($request, $response);
84 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $ignoredException) {
85 }
86 }
87 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterRequestDispatch', array('request' => $request, 'response' => $response));
88 }
89
90 /**
91 * Finds and instanciates a controller that matches the current request.
92 * If no controller can be found, an instance of NotFoundControllerInterface is returned.
93 *
94 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request to dispatch
95 * @throws Exception\InvalidControllerException
96 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
97 */
98 protected function resolveController(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request) {
99 $controllerObjectName = $request->getControllerObjectName();
100 $controller = $this->objectManager->get($controllerObjectName);
101 if (!$controller instanceof \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface) {
102 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerException('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerInterface.', 1202921619);
103 }
104 return $controller;
105 }
106 }
107
108 ?>