[TASK] Use null coalescing operator where possible
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / RequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
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 * The generic command line interface request handler for the MVC framework.
19 *
20 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
21 */
22 class RequestHandler implements \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
26 */
27 protected $objectManager;
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Mvc\Dispatcher
31 */
32 protected $dispatcher;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder
36 */
37 protected $requestBuilder;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
41 */
42 protected $environmentService;
43
44 /**
45 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
46 */
47 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
48 {
49 $this->objectManager = $objectManager;
50 }
51
52 /**
53 * @param \TYPO3\CMS\Extbase\Mvc\Dispatcher $dispatcher
54 */
55 public function injectDispatcher(\TYPO3\CMS\Extbase\Mvc\Dispatcher $dispatcher)
56 {
57 $this->dispatcher = $dispatcher;
58 }
59
60 /**
61 * @param \TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder $requestBuilder
62 */
63 public function injectRequestBuilder(\TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder $requestBuilder)
64 {
65 $this->requestBuilder = $requestBuilder;
66 }
67
68 /**
69 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
70 */
71 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
72 {
73 $this->environmentService = $environmentService;
74 }
75
76 /**
77 * Handles the request
78 *
79 * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface
80 */
81 public function handleRequest()
82 {
83 $commandLine = $_SERVER['argv'] ?? [];
84 $callingScript = array_shift($commandLine);
85 if ($callingScript !== $_SERVER['_']) {
86 $callingScript = $_SERVER['_'] . ' ' . $callingScript;
87 }
88
89 $request = $this->requestBuilder->build($commandLine, $callingScript);
90 /** @var $response \TYPO3\CMS\Extbase\Mvc\Cli\Response */
91 $response = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Response::class);
92 $this->dispatcher->dispatch($request, $response);
93 $response->send();
94 return $response;
95 }
96
97 /**
98 * This request handler can handle any command line request.
99 *
100 * @return bool If the request is a command line request, TRUE otherwise FALSE
101 */
102 public function canHandleRequest()
103 {
104 return $this->environmentService->isEnvironmentInCliMode();
105 }
106
107 /**
108 * Returns the priority - how eager the handler is to actually handle the
109 * request.
110 *
111 * @return int The priority of the request handler.
112 */
113 public function getPriority()
114 {
115 return 100;
116 }
117 }