01f4f7f00be93581eb00f49dbf1ad301b6985cdc
[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 = isset($_SERVER['argv']) ? $_SERVER['argv'] : [];
84 $callingScript = array_shift($commandLine);
85 if ($callingScript !== $_SERVER['_']) {
86 $callingScript = $_SERVER['_'] . ' ' . $callingScript;
87 }
88
89 // Add the "extbase" prefix for the cli_dispatch command line tool
90 if (strpos($callingScript, 'cli_dispatch') !== false) {
91 $callingScript .= ' extbase';
92 }
93 $request = $this->requestBuilder->build($commandLine, $callingScript);
94 /** @var $response \TYPO3\CMS\Extbase\Mvc\Cli\Response */
95 $response = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Response::class);
96 $this->dispatcher->dispatch($request, $response);
97 $response->send();
98 return $response;
99 }
100
101 /**
102 * This request handler can handle any command line request.
103 *
104 * @return bool If the request is a command line request, TRUE otherwise FALSE
105 */
106 public function canHandleRequest()
107 {
108 return $this->environmentService->isEnvironmentInCliMode();
109 }
110
111 /**
112 * Returns the priority - how eager the handler is to actually handle the
113 * request.
114 *
115 * @return int The priority of the request handler.
116 */
117 public function getPriority()
118 {
119 return 100;
120 }
121 }