[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / RequestHandlerResolver.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Analyzes the raw request and delivers a request handler which can handle it.
19 * @internal only to be used within Extbase, not part of TYPO3 Core API.
20 */
21 class RequestHandlerResolver
22 {
23 /**
24 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
25 */
26 protected $objectManager;
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
30 */
31 protected $configurationManager;
32
33 /**
34 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
35 */
36 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
37 {
38 $this->objectManager = $objectManager;
39 }
40
41 /**
42 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
43 */
44 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
45 {
46 $this->configurationManager = $configurationManager;
47 }
48
49 /**
50 * Analyzes the raw request and tries to find a request handler which can handle
51 * it. If none is found, an exception is thrown.
52 *
53 * @return \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface A request handler
54 * @throws \TYPO3\CMS\Extbase\Mvc\Exception
55 */
56 public function resolveRequestHandler()
57 {
58 $availableRequestHandlerClassNames = $this->getRegisteredRequestHandlerClassNames();
59 $suitableRequestHandlers = [];
60 foreach ($availableRequestHandlerClassNames as $requestHandlerClassName) {
61 $requestHandler = $this->objectManager->get($requestHandlerClassName);
62 if ($requestHandler->canHandleRequest()) {
63 $priority = $requestHandler->getPriority();
64 if (isset($suitableRequestHandlers[$priority])) {
65 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);
66 }
67 $suitableRequestHandlers[$priority] = $requestHandler;
68 }
69 }
70 if (empty($suitableRequestHandlers)) {
71 throw new \TYPO3\CMS\Extbase\Mvc\Exception('No suitable request handler found.', 1205414233);
72 }
73 ksort($suitableRequestHandlers);
74 return array_pop($suitableRequestHandlers);
75 }
76
77 /**
78 * Returns a list of all registered request handlers.
79 *
80 * @return array
81 */
82 public function getRegisteredRequestHandlerClassNames()
83 {
84 $settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
85 return is_array($settings['mvc']['requestHandlers']) ? $settings['mvc']['requestHandlers'] : [];
86 }
87 }