TYPO3 CMS  TYPO3_8-7
CommandController.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 
31 
38 {
42  protected $request;
43 
47  protected $response;
48 
52  protected $arguments;
53 
59  protected $commandMethodName = '';
60 
67  protected $requestAdminPermissions = false;
68 
72  protected $reflectionService;
73 
77  protected $objectManager;
78 
82  protected $output;
83 
88  {
89  $this->objectManager = $objectManager;
90  }
91 
96  {
97  $this->reflectionService = $reflectionService;
98  }
99 
106  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
107  {
108  return $request instanceof Request;
109  }
110 
120  {
121  if (!$this->canProcessRequest($request)) {
122  throw new UnsupportedRequestTypeException(sprintf('%s only supports command line requests – requests of type "%s" given.', get_class($this), get_class($request)), 1300787096);
123  }
124 
125  $this->request = $request;
126  $this->request->setDispatched(true);
127  $this->response = $response;
128 
129  $this->commandMethodName = $this->resolveCommandMethodName();
130  $this->output = $this->objectManager->get(ConsoleOutput::class);
131  $this->arguments = $this->objectManager->get(Arguments::class);
133  $this->mapRequestArgumentsToControllerArguments();
135  $this->callCommandMethod();
136  }
137 
148  protected function resolveCommandMethodName()
149  {
150  $commandMethodName = $this->request->getControllerCommandName() . 'Command';
151  if (!is_callable([$this, $commandMethodName])) {
152  throw new NoSuchCommandException(sprintf('A command method "%s()" does not exist in controller "%s".', $commandMethodName, get_class($this)), 1300902143);
153  }
154  return $commandMethodName;
155  }
156 
164  protected function initializeCommandMethodArguments()
165  {
166  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
167 
168  foreach ($methodParameters as $parameterName => $parameterInfo) {
169  $dataType = null;
170  if (isset($parameterInfo['type'])) {
171  $dataType = $parameterInfo['type'];
172  } elseif ($parameterInfo['array']) {
173  $dataType = 'array';
174  }
175  if ($dataType === null) {
176  throw new InvalidArgumentTypeException(sprintf('The argument type for parameter $%s of method %s->%s() could not be detected.', $parameterName, get_class($this), $this->commandMethodName), 1306755296);
177  }
178  $defaultValue = (isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : null);
179  $this->arguments->addNewArgument($parameterName, $dataType, ($parameterInfo['optional'] === false), $defaultValue);
180  }
181  }
182 
186  protected function mapRequestArgumentsToControllerArguments()
187  {
189  foreach ($this->arguments as $argument) {
190  $argumentName = $argument->getName();
191  if ($this->request->hasArgument($argumentName)) {
192  $argument->setValue($this->request->getArgument($argumentName));
193  continue;
194  }
195  if (!$argument->isRequired()) {
196  continue;
197  }
198  $argumentValue = null;
199  $commandArgumentDefinition = $this->objectManager->get(CommandArgumentDefinition::class, $argumentName, true, null);
200  while ($argumentValue === null) {
201  $argumentValue = $this->output->ask(sprintf('<comment>Please specify the required argument "%s":</comment> ', $commandArgumentDefinition->getDashedName()));
202  }
203  $argument->setValue($argumentValue);
204  }
205  }
206 
210  protected function initializeBackendAuthentication()
211  {
212  $backendUserAuthentication = $this->getBackendUserAuthentication();
213  if ($backendUserAuthentication !== null) {
214  $backendUserAuthentication->backendCheckLogin();
215  }
216  }
217 
229  protected function forward($commandName, $controllerObjectName = null, array $arguments = [])
230  {
231  $this->request->setDispatched(false);
232  $this->request->setControllerCommandName($commandName);
233  if ($controllerObjectName !== null) {
234  $this->request->setControllerObjectName($controllerObjectName);
235  }
236  $this->request->setArguments($arguments);
237 
238  $this->arguments->removeAll();
239  throw new StopActionException('forward', 1476107661);
240  }
241 
249  protected function callCommandMethod()
250  {
251  $preparedArguments = [];
253  foreach ($this->arguments as $argument) {
254  $preparedArguments[] = $argument->getValue();
255  }
256  $commandResult = call_user_func_array([$this, $this->commandMethodName], $preparedArguments);
257  if (is_string($commandResult) && $commandResult !== '') {
258  $this->response->appendContent($commandResult);
259  } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
260  $this->response->appendContent((string)$commandResult);
261  }
262  }
263 
271  protected function ensureAdminRoleIfRequested()
272  {
274  $userAuthentication = $this->getBackendUserAuthentication();
275 
276  if (!$this->requestAdminPermissions || $userAuthentication === null || !isset($userAuthentication->user['admin'])) {
277  return null;
278  }
279 
280  $originalRole = $userAuthentication->user['admin'];
281  $userAuthentication->user['admin'] = 1;
282  return $originalRole;
283  }
284 
291  protected function restoreUserRole($originalRole)
292  {
294  $userAuthentication = $this->getBackendUserAuthentication();
295 
296  if ($originalRole !== null && $userAuthentication !== null) {
297  $userAuthentication->user['admin'] = $originalRole;
298  }
299  }
300 
309  protected function output($text, array $arguments = [])
310  {
311  $this->output->output($text, $arguments);
312  }
313 
321  protected function outputLine($text = '', array $arguments = [])
322  {
323  $this->output->outputLine($text, $arguments);
324  }
325 
335  protected function outputFormatted($text = '', array $arguments = [], $leftPadding = 0)
336  {
337  $this->output->outputFormatted($text, $arguments, $leftPadding);
338  }
339 
347  protected function quit($exitCode = 0)
348  {
349  $this->response->setExitCode($exitCode);
350  throw new StopActionException('quit', 1476107681);
351  }
352 
359  protected function sendAndExit($exitCode = 0)
360  {
361  $this->response->send();
362  exit($exitCode);
363  }
364 
370  protected function getBackendUserAuthentication()
371  {
372  return isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
373  }
374 }
processRequest(RequestInterface $request, ResponseInterface $response)
forward($commandName, $controllerObjectName=null, array $arguments=[])
injectObjectManager(ObjectManagerInterface $objectManager)
outputFormatted($text='', array $arguments=[], $leftPadding=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
injectReflectionService(ReflectionService $reflectionService)
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)