TYPO3 CMS  TYPO3_7-6
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 
28 
35 {
39  protected $request;
40 
44  protected $response;
45 
49  protected $arguments;
50 
56  protected $commandMethodName = '';
57 
64  protected $requestAdminPermissions = false;
65 
70 
74  protected $reflectionService;
75 
79  protected $objectManager;
80 
84  protected $output;
85 
90  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
91  {
92  $this->objectManager = $objectManager;
93  $this->arguments = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class);
94  $this->userAuthentication = isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
95  $this->output = $this->objectManager->get(ConsoleOutput::class);
96  }
97 
101  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
102  {
103  $this->reflectionService = $reflectionService;
104  }
105 
112  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
113  {
114  return $request instanceof Request;
115  }
116 
127  {
128  if (!$this->canProcessRequest($request)) {
129  throw new UnsupportedRequestTypeException(sprintf('%s only supports command line requests – requests of type "%s" given.', get_class($this), get_class($request)), 1300787096);
130  }
131 
132  $this->request = $request;
133  $this->request->setDispatched(true);
134  $this->response = $response;
135 
136  $this->commandMethodName = $this->resolveCommandMethodName();
138  $this->mapRequestArgumentsToControllerArguments();
139  $this->callCommandMethod();
140  }
141 
152  protected function resolveCommandMethodName()
153  {
154  $commandMethodName = $this->request->getControllerCommandName() . 'Command';
155  if (!is_callable([$this, $commandMethodName])) {
156  throw new NoSuchCommandException(sprintf('A command method "%s()" does not exist in controller "%s".', $commandMethodName, get_class($this)), 1300902143);
157  }
158  return $commandMethodName;
159  }
160 
169  protected function initializeCommandMethodArguments()
170  {
171  $this->arguments->removeAll();
172  $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
173 
174  foreach ($methodParameters as $parameterName => $parameterInfo) {
175  $dataType = null;
176  if (isset($parameterInfo['type'])) {
177  $dataType = $parameterInfo['type'];
178  } elseif ($parameterInfo['array']) {
179  $dataType = 'array';
180  }
181  if ($dataType === null) {
182  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);
183  }
184  $defaultValue = (isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : null);
185  $this->arguments->addNewArgument($parameterName, $dataType, ($parameterInfo['optional'] === false), $defaultValue);
186  }
187  }
188 
194  protected function mapRequestArgumentsToControllerArguments()
195  {
197  foreach ($this->arguments as $argument) {
198  $argumentName = $argument->getName();
199  if ($this->request->hasArgument($argumentName)) {
200  $argument->setValue($this->request->getArgument($argumentName));
201  continue;
202  }
203  if (!$argument->isRequired()) {
204  continue;
205  }
206  $argumentValue = null;
207  $commandArgumentDefinition = $this->objectManager->get(CommandArgumentDefinition::class, $argumentName, true, null);
208  while ($argumentValue === null) {
209  $argumentValue = $this->output->ask(sprintf('<comment>Please specify the required argument "%s":</comment> ', $commandArgumentDefinition->getDashedName()));
210  }
211  $argument->setValue($argumentValue);
212  }
213  }
214 
227  protected function forward($commandName, $controllerObjectName = null, array $arguments = [])
228  {
229  $this->request->setDispatched(false);
230  $this->request->setControllerCommandName($commandName);
231  if ($controllerObjectName !== null) {
232  $this->request->setControllerObjectName($controllerObjectName);
233  }
234  $this->request->setArguments($arguments);
235 
236  $this->arguments->removeAll();
237  throw new StopActionException();
238  }
239 
249  protected function callCommandMethod()
250  {
251  $preparedArguments = [];
253  foreach ($this->arguments as $argument) {
254  $preparedArguments[] = $argument->getValue();
255  }
256  $originalRole = $this->ensureAdminRoleIfRequested();
257  $commandResult = call_user_func_array([$this, $this->commandMethodName], $preparedArguments);
258  $this->restoreUserRole($originalRole);
259  if (is_string($commandResult) && $commandResult !== '') {
260  $this->response->appendContent($commandResult);
261  } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
262  $this->response->appendContent((string)$commandResult);
263  }
264  }
265 
272  protected function ensureAdminRoleIfRequested()
273  {
274  if (!$this->requestAdminPermissions || !$this->userAuthentication || !isset($this->userAuthentication->user['admin'])) {
275  return null;
276  }
277  $originalRole = $this->userAuthentication->user['admin'];
278  $this->userAuthentication->user['admin'] = 1;
279  return $originalRole;
280  }
281 
287  protected function restoreUserRole($originalRole)
288  {
289  if ($originalRole !== null) {
290  $this->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;
348  }
349 
357  protected function sendAndExit($exitCode = 0)
358  {
359  $this->response->send();
360  exit($exitCode);
361  }
362 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
processRequest(RequestInterface $request, ResponseInterface $response)
forward($commandName, $controllerObjectName=null, array $arguments=[])
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
outputFormatted($text='', array $arguments=[], $leftPadding=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)