TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CommandController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
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 
30 
37 {
41  protected $request;
42 
46  protected $response;
47 
51  protected $arguments;
52 
58  protected $commandMethodName = '';
59 
66  protected $requestAdminPermissions = false;
67 
71  protected $reflectionService;
72 
76  protected $objectManager;
77 
81  protected $output;
82 
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 
121  {
122  if (!$this->canProcessRequest($request)) {
123  throw new UnsupportedRequestTypeException(sprintf('%s only supports command line requests – requests of type "%s" given.', get_class($this), get_class($request)), 1300787096);
124  }
125 
126  $this->request = $request;
127  $this->request->setDispatched(true);
128  $this->response = $response;
129 
130  $this->commandMethodName = $this->resolveCommandMethodName();
131  $this->output = $this->objectManager->get(ConsoleOutput::class);
132  $this->arguments = $this->objectManager->get(Arguments::class);
134  $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 
165  protected function initializeCommandMethodArguments()
166  {
167  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
168 
169  foreach ($methodParameters as $parameterName => $parameterInfo) {
170  $dataType = null;
171  if (isset($parameterInfo['type'])) {
172  $dataType = $parameterInfo['type'];
173  } elseif ($parameterInfo['array']) {
174  $dataType = 'array';
175  }
176  if ($dataType === null) {
177  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);
178  }
179  $defaultValue = (isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : null);
180  $this->arguments->addNewArgument($parameterName, $dataType, ($parameterInfo['optional'] === false), $defaultValue);
181  }
182  }
183 
189  protected function mapRequestArgumentsToControllerArguments()
190  {
192  foreach ($this->arguments as $argument) {
193  $argumentName = $argument->getName();
194  if ($this->request->hasArgument($argumentName)) {
195  $argument->setValue($this->request->getArgument($argumentName));
196  continue;
197  }
198  if (!$argument->isRequired()) {
199  continue;
200  }
201  $argumentValue = null;
202  $commandArgumentDefinition = $this->objectManager->get(CommandArgumentDefinition::class, $argumentName, true, null);
203  while ($argumentValue === null) {
204  $argumentValue = $this->output->ask(sprintf('<comment>Please specify the required argument "%s":</comment> ', $commandArgumentDefinition->getDashedName()));
205  }
206  $argument->setValue($argumentValue);
207  }
208  }
209 
222  protected function forward($commandName, $controllerObjectName = null, array $arguments = [])
223  {
224  $this->request->setDispatched(false);
225  $this->request->setControllerCommandName($commandName);
226  if ($controllerObjectName !== null) {
227  $this->request->setControllerObjectName($controllerObjectName);
228  }
229  $this->request->setArguments($arguments);
230 
231  $this->arguments->removeAll();
232  throw new StopActionException('forward', 1476107661);
233  }
234 
244  protected function callCommandMethod()
245  {
246  $preparedArguments = [];
248  foreach ($this->arguments as $argument) {
249  $preparedArguments[] = $argument->getValue();
250  }
251  $originalRole = $this->ensureAdminRoleIfRequested();
252  $commandResult = call_user_func_array([$this, $this->commandMethodName], $preparedArguments);
253  $this->restoreUserRole($originalRole);
254  if (is_string($commandResult) && $commandResult !== '') {
255  $this->response->appendContent($commandResult);
256  } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
257  $this->response->appendContent((string)$commandResult);
258  }
259  }
260 
267  protected function ensureAdminRoleIfRequested()
268  {
269  $userAuthentication = $this->getBackendUserAuthentication();
270 
271  if (!$this->requestAdminPermissions || $userAuthentication === null || !isset($userAuthentication->user['admin'])) {
272  return null;
273  }
274 
275  $originalRole = $userAuthentication->user['admin'];
276  $userAuthentication->user['admin'] = 1;
277  return $originalRole;
278  }
279 
285  protected function restoreUserRole($originalRole)
286  {
287  $userAuthentication = $this->getBackendUserAuthentication();
288 
289  if ($originalRole !== null && $userAuthentication !== null) {
290  $userAuthentication->user['admin'] = $originalRole;
291  }
292  }
293 
303  protected function output($text, array $arguments = [])
304  {
305  $this->output->output($text, $arguments);
306  }
307 
316  protected function outputLine($text = '', array $arguments = [])
317  {
318  $this->output->outputLine($text, $arguments);
319  }
320 
331  protected function outputFormatted($text = '', array $arguments = [], $leftPadding = 0)
332  {
333  $this->output->outputFormatted($text, $arguments, $leftPadding);
334  }
335 
344  protected function quit($exitCode = 0)
345  {
346  $this->response->setExitCode($exitCode);
347  throw new StopActionException('quit', 1476107681);
348  }
349 
357  protected function sendAndExit($exitCode = 0)
358  {
359  $this->response->send();
360  exit($exitCode);
361  }
362 
368  protected function getBackendUserAuthentication()
369  {
370  return isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
371  }
372 }
outputFormatted($text= '', array $arguments=[], $leftPadding=0)
processRequest(RequestInterface $request, ResponseInterface $response)
injectObjectManager(ObjectManagerInterface $objectManager)
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
forward($commandName, $controllerObjectName=null, array $arguments=[])
injectReflectionService(ReflectionService $reflectionService)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']