[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / RequestHandlerResolver.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Jochen Rau <jochen.rau@typoplanet.de>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Analyzes the raw request and delivers a request handler which can handle it.
31 */
32 class RequestHandlerResolver {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * @var Tx_Extbase_Reflection_ReflectionService
41 */
42 protected $reflectionService;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
46 */
47 protected $configurationManager;
48
49 /**
50 * Injects the object manager
51 *
52 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
53 * @return void
54 */
55 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
56 $this->objectManager = $objectManager;
57 }
58
59 /**
60 * Injects the reflection service
61 *
62 * @param \TYPO3\CMS\Extbase\Reflection\Service $reflectionService
63 * @return void
64 */
65 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\Service $reflectionService) {
66 $this->reflectionService = $reflectionService;
67 }
68
69 /**
70 * Injects the configuration manager
71 *
72 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
73 * @return void
74 */
75 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
76 $this->configurationManager = $configurationManager;
77 }
78
79 /**
80 * Analyzes the raw request and tries to find a request handler which can handle
81 * it. If none is found, an exception is thrown.
82 *
83 * @return Tx_Extbase_MVC_RequestHandler A request handler
84 * @throws \TYPO3\CMS\Extbase\Mvc\Exception
85 */
86 public function resolveRequestHandler() {
87 $availableRequestHandlerClassNames = $this->getRegisteredRequestHandlerClassNames();
88 $suitableRequestHandlers = array();
89 foreach ($availableRequestHandlerClassNames as $requestHandlerClassName) {
90 $requestHandler = $this->objectManager->get($requestHandlerClassName);
91 if ($requestHandler->canHandleRequest()) {
92 $priority = $requestHandler->getPriority();
93 if (isset($suitableRequestHandlers[$priority])) {
94 throw new \TYPO3\CMS\Extbase\Mvc\Exception('More than one request handler with the same priority can handle the request, but only one handler may be active at a time!', 1176475350);
95 }
96 $suitableRequestHandlers[$priority] = $requestHandler;
97 }
98 }
99 if (count($suitableRequestHandlers) === 0) {
100 throw new \TYPO3\CMS\Extbase\Mvc\Exception('No suitable request handler found.', 1205414233);
101 }
102 ksort($suitableRequestHandlers);
103 return array_pop($suitableRequestHandlers);
104 }
105
106 /**
107 * Returns a list of all registered request handlers.
108 *
109 * @return array
110 */
111 public function getRegisteredRequestHandlerClassNames() {
112 $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
113 return is_array($settings['mvc']['requestHandlers']) ? $settings['mvc']['requestHandlers'] : array();
114 }
115
116 }
117
118
119 ?>