[TASK][WIP] Move non class code back to source files
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / RequestHandlerResolver.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 * Analyzes the raw request and delivers a request handler which can handle it.
29 */
30 class Tx_Extbase_MVC_RequestHandlerResolver {
31
32 /**
33 * @var Tx_Extbase_Object_ObjectManagerInterface
34 */
35 protected $objectManager;
36
37 /**
38 * @var Tx_Extbase_Reflection_ReflectionService
39 */
40 protected $reflectionService;
41
42 /**
43 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
44 */
45 protected $configurationManager;
46
47 /**
48 * Injects the object manager
49 *
50 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager A reference to the object manager
51 * @return void
52 */
53 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
54 $this->objectManager = $objectManager;
55 }
56
57 /**
58 * Injects the reflection service
59 *
60 * @param Tx_Extbase_Reflection_Service $reflectionService
61 * @return void
62 */
63 public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
64 $this->reflectionService = $reflectionService;
65 }
66
67 /**
68 * Injects the configuration manager
69 *
70 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
71 * @return void
72 */
73 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
74 $this->configurationManager = $configurationManager;
75 }
76
77 /**
78 * Analyzes the raw request and tries to find a request handler which can handle
79 * it. If none is found, an exception is thrown.
80 *
81 * @return Tx_Extbase_MVC_RequestHandler A request handler
82 * @throws Tx_Extbase_MVC_Exception
83 */
84 public function resolveRequestHandler() {
85 $availableRequestHandlerClassNames = $this->getRegisteredRequestHandlerClassNames();
86 $suitableRequestHandlers = array();
87 foreach ($availableRequestHandlerClassNames as $requestHandlerClassName) {
88 $requestHandler = $this->objectManager->get($requestHandlerClassName);
89 if ($requestHandler->canHandleRequest()) {
90 $priority = $requestHandler->getPriority();
91 if (isset($suitableRequestHandlers[$priority])) {
92 throw new Tx_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);
93 }
94 $suitableRequestHandlers[$priority] = $requestHandler;
95 }
96 }
97 if (count($suitableRequestHandlers) === 0) {
98 throw new Tx_Extbase_MVC_Exception('No suitable request handler found.', 1205414233);
99 }
100 ksort($suitableRequestHandlers);
101 return array_pop($suitableRequestHandlers);
102 }
103
104 /**
105 * Returns a list of all registered request handlers.
106 *
107 * @return array
108 */
109 public function getRegisteredRequestHandlerClassNames() {
110 $settings = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
111 return is_array($settings['mvc']['requestHandlers']) ? $settings['mvc']['requestHandlers'] : array();
112 }
113
114 }
115
116 ?>