TYPO3 CMS  TYPO3_8-7
CliRequestHandler.php
Go to the documentation of this file.
1 <?php
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 
23 
34 {
39  protected $bootstrap;
40 
46  public function __construct(Bootstrap $bootstrap)
47  {
48  $this->bootstrap = $bootstrap;
49  }
50 
56  public function handleRequest(InputInterface $input)
57  {
58  GeneralUtility::deprecationLog('Using cli_dispatch.phpsh as entry point for CLI commands has been marked '
59  . 'as deprecated and will be removed in TYPO3 v9. Please use the new CLI entrypoint via /typo3/sysext/core/bin/typo3 instead.');
60  $output = GeneralUtility::makeInstance(ConsoleOutput::class);
61  $exitCode = 0;
62 
63  try {
64  $command = $this->validateCommandLineKeyFromInput($input);
65 
66  // try and look up if the CLI command exists
67  $commandLineScript = $this->getIncludeScriptByCommandLineKey($command);
68  $this->boot();
69 
70  if (is_callable($commandLineScript)) {
71  call_user_func($commandLineScript, $input, $output);
72  } else {
73  // include the CLI script
74  include($commandLineScript);
75  }
76  } catch (\InvalidArgumentException $e) {
77  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
78  $output->writeln('');
79  $output->writeln('Valid keys are:');
80  $output->writeln('');
81  $cliKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']);
82  asort($cliKeys);
83  foreach ($cliKeys as $key => $value) {
84  $output->writeln(' ' . $value);
85  }
86  $exitCode = $e->getCode();
87  } catch (\RuntimeException $e) {
88  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
89  $exitCode = $e->getCode();
90  } catch (\Throwable $e) {
91  $output->writeln('<error>Oops, an error occurred: ' . $e->getMessage() . '</error>');
92  $exitCode = $e->getCode();
93  }
94 
95  if ($exitCode > 0 && 0 === $exitCode % 256) {
96  $exitCode = 1;
97  }
98 
99  exit($exitCode);
100  }
101 
107  protected function boot()
108  {
109  $this->bootstrap
110  ->loadBaseTca()
111  ->loadExtTables()
112  ->initializeBackendUser(CommandLineUserAuthentication::class);
113 
114  // Checks for a user _CLI_, if non exists, will create one
116 
117  $this->bootstrap
118  ->initializeLanguageObject()
119  // Make sure output is not buffered, so command-line output and interaction can take place
120  ->endOutputBufferingAndCleanPreviousOutput();
121  }
122 
132  protected function validateCommandLineKeyFromInput(InputInterface $input)
133  {
134  $cliKey = $input->getFirstArgument();
135  if (empty($cliKey)) {
136  throw new \InvalidArgumentException('This script must have a command as first argument.', 1476107418);
137  }
138  if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey])) {
139  throw new \InvalidArgumentException('This supplied command is not valid.', 1476107480);
140  }
141  return $cliKey;
142  }
143 
150  protected function getIncludeScriptByCommandLineKey($cliKey)
151  {
152  $commandLineScript = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys'][$cliKey][0];
153  if (!is_callable($commandLineScript)) {
154  $commandLineScript = GeneralUtility::getFileAbsFileName($commandLineScript);
155  }
156 
157  // the CLI key (e.g. "extbase" or "lowlevel"), is removed from the argv, but the script path is kept
158  $cliScriptPath = array_shift($_SERVER['argv']);
159  array_shift($_SERVER['argv']);
160  array_unshift($_SERVER['argv'], $cliScriptPath);
161  return $commandLineScript;
162  }
163 
169  protected function loadCommandLineBackendUser()
170  {
171  if ($GLOBALS['BE_USER']->user['uid']) {
172  throw new \RuntimeException('Another user was already loaded which is impossible in CLI mode!', 1476107444);
173  }
174  $GLOBALS['BE_USER']->authenticate();
175  }
176 
183  public function canHandleRequest(InputInterface $input)
184  {
185  return true;
186  }
187 
193  public function getPriority()
194  {
195  return 20;
196  }
197 }
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']