‪TYPO3CMS  9.5
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 
30 
37 {
41  protected ‪$request;
42 
46  protected ‪$response;
47 
51  protected ‪$arguments;
52 
58  protected ‪$commandMethodName = '';
59 
65  protected ‪$requestAdminPermissions = false;
66 
70  protected ‪$reflectionService;
71 
75  protected ‪$objectManager;
76 
80  protected ‪$output;
81 
82  public function ‪__construct()
83  {
84  trigger_error('Extbase Command Controllers will be removed in TYPO3 v10.0. Migrate to symfony/console commands instead.', E_USER_DEPRECATED);
85  }
86 
91  {
92  $this->objectManager = ‪$objectManager;
93  }
94 
99  {
100  $this->reflectionService = ‪$reflectionService;
101  }
102 
110  {
111  return ‪$request instanceof ‪Request;
112  }
113 
122  {
123  if (!$this->‪canProcessRequest($request)) {
124  throw new ‪UnsupportedRequestTypeException(sprintf('%s only supports command line requests – requests of type "%s" given.', static::class, get_class(‪$request)), 1300787096);
125  }
126 
127  $this->request = ‪$request;
128  $this->request->‪setDispatched(true);
129  $this->response = ‪$response;
130 
131  $this->commandMethodName = $this->‪resolveCommandMethodName();
132  $this->‪output = $this->objectManager->get(ConsoleOutput::class);
133  $this->arguments = $this->objectManager->get(Arguments::class);
137  $this->‪callCommandMethod();
138  }
139 
150  protected function ‪resolveCommandMethodName()
151  {
152  ‪$commandMethodName = $this->request->getControllerCommandName() . 'Command';
153  if (!is_callable([$this, ‪$commandMethodName])) {
154  throw new ‪NoSuchCommandException(sprintf('A command method "%s()" does not exist in controller "%s".', ‪$commandMethodName, static::class), 1300902143);
155  }
156  return ‪$commandMethodName;
157  }
158 
166  protected function ‪initializeCommandMethodArguments()
167  {
168  $methodParameters = $this->reflectionService
169  ->getClassSchema(static::class)
170  ->getMethod($this->commandMethodName)['params'] ?? [];
171 
172  foreach ($methodParameters as $parameterName => $parameterInfo) {
173  $dataType = null;
174  if (isset($parameterInfo['type'])) {
175  $dataType = $parameterInfo['type'];
176  } elseif ($parameterInfo['array']) {
177  $dataType = 'array';
178  }
179  if ($dataType === null) {
180  throw new ‪InvalidArgumentTypeException(sprintf('The argument type for parameter $%s of method %s->%s() could not be detected.', $parameterName, static::class, $this->commandMethodName), 1306755296);
181  }
182  $defaultValue = ($parameterInfo['defaultValue'] ?? null);
183  $this->arguments->addNewArgument($parameterName, $dataType, $parameterInfo['optional'] === false, $defaultValue);
184  }
185  }
186 
191  {
193  foreach ($this->arguments as $argument) {
194  $argumentName = $argument->getName();
195  if ($this->request->hasArgument($argumentName)) {
196  $argument->setValue($this->request->getArgument($argumentName));
197  continue;
198  }
199  if (!$argument->isRequired()) {
200  continue;
201  }
202  $argumentValue = null;
203  $commandArgumentDefinition = $this->objectManager->get(CommandArgumentDefinition::class, $argumentName, true, null);
204  while ($argumentValue === null) {
205  $argumentValue = $this->‪output->ask(sprintf('<comment>Please specify the required argument "%s":</comment> ', $commandArgumentDefinition->getDashedName()));
206  }
207  $argument->setValue($argumentValue);
208  }
209  }
210 
214  protected function ‪initializeBackendAuthentication()
215  {
216  $backendUserAuthentication = $this->‪getBackendUserAuthentication();
217  if ($backendUserAuthentication !== null) {
218  $backendUserAuthentication->backendCheckLogin();
219  }
220  }
221 
233  protected function ‪forward($commandName, $controllerObjectName = null, array ‪$arguments = [])
234  {
235  $this->request->setDispatched(false);
236  $this->request->setControllerCommandName($commandName);
237  if ($controllerObjectName !== null) {
238  $this->request->setControllerObjectName($controllerObjectName);
239  }
240  $this->request->setArguments(‪$arguments);
241 
242  $this->arguments->removeAll();
243  throw new StopActionException('forward', 1476107661);
244  }
245 
253  protected function ‪callCommandMethod()
254  {
255  $preparedArguments = [];
257  foreach ($this->arguments as $argument) {
258  $preparedArguments[] = $argument->getValue();
259  }
260  $commandResult = call_user_func_array([$this, $this->commandMethodName], $preparedArguments);
261  if (is_string($commandResult) && $commandResult !== '') {
262  $this->response->appendContent($commandResult);
263  } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
264  $this->response->appendContent((string)$commandResult);
265  }
266  }
267 
276  protected function ‪output($text, array ‪$arguments = [])
277  {
278  $this->‪output->output($text, ‪$arguments);
279  }
280 
288  protected function ‪outputLine($text = '', array ‪$arguments = [])
289  {
290  $this->‪output->outputLine($text, ‪$arguments);
291  }
292 
302  protected function ‪outputFormatted($text = '', array ‪$arguments = [], $leftPadding = 0)
303  {
304  $this->‪output->outputFormatted($text, ‪$arguments, $leftPadding);
305  }
306 
314  protected function ‪quit($exitCode = 0)
315  {
316  $this->response->setExitCode($exitCode);
317  throw new ‪StopActionException('quit', 1476107681);
318  }
319 
326  protected function ‪sendAndExit($exitCode = 0)
327  {
328  $this->response->send();
329  exit($exitCode);
330  }
331 
337  protected function ‪getBackendUserAuthentication()
338  {
339  return ‪$GLOBALS['BE_USER'] ?? null;
340  }
341 }
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\canProcessRequest
‪bool canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
Definition: CommandController.php:101
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:26
‪TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
Definition: UnsupportedRequestTypeException.php:21
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\mapRequestArgumentsToControllerArguments
‪mapRequestArgumentsToControllerArguments()
Definition: CommandController.php:182
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\resolveCommandMethodName
‪string resolveCommandMethodName()
Definition: CommandController.php:142
‪TYPO3\CMS\Extbase\Mvc\ResponseInterface
Definition: ResponseInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException
Definition: NoSuchCommandException.php:21
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\quit
‪quit($exitCode=0)
Definition: CommandController.php:306
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$request
‪Request $request
Definition: CommandController.php:40
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:22
‪TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException
Definition: InvalidArgumentTypeException.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$output
‪ConsoleOutput $output
Definition: CommandController.php:72
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\initializeCommandMethodArguments
‪initializeCommandMethodArguments()
Definition: CommandController.php:158
‪TYPO3\CMS\Extbase\Mvc\Controller
Definition: AbstractController.php:2
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$commandMethodName
‪string $commandMethodName
Definition: CommandController.php:54
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:23
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:27
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\processRequest
‪processRequest(RequestInterface $request, ResponseInterface $response)
Definition: CommandController.php:113
‪TYPO3\CMS\Extbase\Mvc\Cli\Request
Definition: Request.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\outputLine
‪outputLine($text='', array $arguments=[])
Definition: CommandController.php:280
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\callCommandMethod
‪callCommandMethod()
Definition: CommandController.php:245
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$response
‪Response $response
Definition: CommandController.php:44
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput
Definition: ConsoleOutput.php:34
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: CommandController.php:82
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$requestAdminPermissions
‪bool $requestAdminPermissions
Definition: CommandController.php:60
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\__construct
‪__construct()
Definition: CommandController.php:74
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\forward
‪forward($commandName, $controllerObjectName=null, array $arguments=[])
Definition: CommandController.php:225
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\output
‪output($text, array $arguments=[])
Definition: CommandController.php:268
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\sendAndExit
‪sendAndExit($exitCode=0)
Definition: CommandController.php:318
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$arguments
‪Arguments $arguments
Definition: CommandController.php:48
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:21
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\outputFormatted
‪outputFormatted($text='', array $arguments=[], $leftPadding=0)
Definition: CommandController.php:294
‪TYPO3\CMS\Extbase\Mvc\Cli\Response
Definition: Response.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\initializeBackendAuthentication
‪initializeBackendAuthentication()
Definition: CommandController.php:206
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition
Definition: CommandArgumentDefinition.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController
Definition: CommandController.php:37
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\injectReflectionService
‪injectReflectionService(ReflectionService $reflectionService)
Definition: CommandController.php:90
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$objectManager
‪ObjectManagerInterface $objectManager
Definition: CommandController.php:68
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandControllerInterface
Definition: CommandControllerInterface.php:23
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\$reflectionService
‪ReflectionService $reflectionService
Definition: CommandController.php:64
‪TYPO3\CMS\Extbase\Mvc\Controller\CommandController\getBackendUserAuthentication
‪BackendUserAuthentication null getBackendUserAuthentication()
Definition: CommandController.php:329
‪TYPO3\CMS\Extbase\Mvc\Cli\Request\setDispatched
‪setDispatched($flag)
Definition: Request.php:97