[FEATURE] Introduce Request/Response based on PSR-7
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Console / CliRequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend\Console;
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 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19 use TYPO3\CMS\Core\Core\RequestHandlerInterface;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Command Line Interface Request Handler dealing with "cliKey"-based Commands from the cli_dispatch.phpsh script.
24 * Picks up requests only when coming from the CLI mode.
25 * Resolves the "cliKey" which is registered inside $TYPO3_CONF_VARS[SC_OPTIONS][GLOBAL][cliKeys]
26 * and includes the CLI-based script or exits if no valid "cliKey" is found.
27 */
28 class CliRequestHandler implements RequestHandlerInterface {
29
30 /**
31 * Instance of the current TYPO3 bootstrap
32 * @var Bootstrap
33 */
34 protected $bootstrap;
35
36 /**
37 * Constructor handing over the bootstrap
38 *
39 * @param Bootstrap $bootstrap
40 */
41 public function __construct(Bootstrap $bootstrap) {
42 $this->bootstrap = $bootstrap;
43 }
44
45 /**
46 * Handles any commandline request
47 *
48 * @param ServerRequestInterface $request
49 * @return void
50 */
51 public function handleRequest(ServerRequestInterface $request) {
52 $commandLineKey = $this->getCommandLineKeyOrDie();
53 $commandLineScript = $this->getIncludeScriptByCommandLineKey($commandLineKey);
54
55 $this->boot();
56
57 try {
58 include($commandLineScript);
59 } catch (\Exception $e) {
60 fwrite(STDERR, $e->getMessage() . LF);
61 exit(99);
62 }
63 }
64
65 /**
66 * Execute TYPO3 bootstrap
67 */
68 protected function boot() {
69 // Evaluate the constant for skipping the BE user check for the bootstrap
70 if (defined('TYPO3_PROCEED_IF_NO_USER') && TYPO3_PROCEED_IF_NO_USER) {
71 $proceedIfNoUserIsLoggedIn = TRUE;
72 } else {
73 $proceedIfNoUserIsLoggedIn = FALSE;
74 }
75
76 $this->bootstrap
77 ->loadExtensionTables(TRUE)
78 ->initializeBackendUser()
79 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
80 ->initializeLanguageObject();
81
82 // Make sure output is not buffered, so command-line output and interaction can take place
83 GeneralUtility::flushOutputBuffers();
84 }
85
86 /**
87 * Check CLI parameters.
88 * First argument is a key that points to the script configuration.
89 * If it is not set or not valid, the script exits with an error message.
90 *
91 * @return string the CLI key in use
92 */
93 protected function getCommandLineKeyOrDie() {
94 $cliKey = $_SERVER['argv'][1];
95 $errorMessage = '';
96 if (empty($cliKey)) {
97 $errorMessage = 'This script must have a \'cliKey\' as first argument.';
98 } elseif (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey])) {
99 $errorMessage = 'The supplied \'cliKey\' is not valid.';
100 }
101
102 // exit with an error message
103 if (!empty($errorMessage)) {
104 $errorMessage .= ' Valid keys are:
105
106 ';
107 $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
108 asort($cliKeys);
109 foreach ($cliKeys as $key => $value) {
110 $errorMessage .= ' ' . $value . LF;
111 }
112 fwrite(STDERR, $errorMessage . LF);
113 die(1);
114 }
115
116 return $cliKey;
117 }
118
119 /**
120 * Define cli-related parameters and return the include script.
121 *
122 * @param string $cliKey the CLI key
123 * @return string the absolute path to the include script
124 */
125 protected function getIncludeScriptByCommandLineKey($cliKey) {
126 list($commandLineScript, $commandLineName) = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey];
127 $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
128 // Note: These constants are not in use anymore
129 define('TYPO3_cliKey', $cliKey);
130 define('TYPO3_cliInclude', $commandLineScript);
131 $GLOBALS['MCONF']['name'] = $commandLineName;
132 // This is a compatibility layer: Some cli scripts rely on this, like ext:phpunit cli
133 $GLOBALS['temp_cliScriptPath'] = array_shift($_SERVER['argv']);
134 $GLOBALS['temp_cliKey'] = array_shift($_SERVER['argv']);
135 array_unshift($_SERVER['argv'], $GLOBALS['temp_cliScriptPath']);
136 return $commandLineScript;
137 }
138
139 /**
140 * This request handler can handle any CLI request .
141 *
142 * @param ServerRequestInterface $request
143 * @return bool If the request is a CLI request, TRUE otherwise FALSE
144 */
145 public function canHandleRequest(ServerRequestInterface $request) {
146 return defined('TYPO3_cliMode') && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE) && (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
147 }
148
149 /**
150 * Returns the priority - how eager the handler is to actually handle the request.
151 *
152 * @return int The priority of the request handler.
153 */
154 public function getPriority() {
155 return 50;
156 }
157 }