‪TYPO3CMS  9.5
RequestBuilder.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 {
27  protected ‪$objectManager;
28 
32  protected ‪$reflectionService;
33 
37  protected ‪$commandManager;
38 
42  protected ‪$configurationManager;
43 
47  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
48  {
49  $this->objectManager = ‪$objectManager;
50  }
51 
55  public function ‪injectReflectionService(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService ‪$reflectionService)
56  {
57  $this->reflectionService = ‪$reflectionService;
58  }
59 
64  {
65  $this->commandManager = ‪$commandManager;
66  }
67 
71  public function ‪injectConfigurationManager(\‪TYPO3\CMS\‪Extbase\Configuration\ConfigurationManagerInterface ‪$configurationManager)
72  {
73  $this->configurationManager = ‪$configurationManager;
74  }
75 
87  public function ‪build($commandLine = '', $callingScript = './typo3/sysext/core/bin/typo3')
88  {
89  $request = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\Request::class);
90  $request->‪setCallingScript($callingScript);
91  $request->setControllerObjectName(\‪TYPO3\CMS\‪Extbase\‪Command\HelpCommandController::class);
92  $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine);
93  if (empty($rawCommandLineArguments)) {
94  $request->setControllerCommandName('helpStub');
95  return $request;
96  }
97  $commandIdentifier = trim(array_shift($rawCommandLineArguments));
98  try {
99  $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
100  $this->configurationManager->setConfiguration(['extensionName' => $command->getExtensionName()]);
101  } catch (\‪TYPO3\CMS\‪Extbase\Mvc\‪Exception\CommandException $exception) {
102  $request->setArgument('exception', $exception);
103  $request->setControllerCommandName('error');
104  return $request;
105  }
106  $controllerObjectName = $command->getControllerClassName();
107  $controllerCommandName = $command->getControllerCommandName();
108  $request->setControllerObjectName($controllerObjectName);
109  $request->setControllerCommandName($controllerCommandName);
110  list($commandLineArguments, $exceedingCommandLineArguments) = $this->‪parseRawCommandLineArguments($rawCommandLineArguments, $controllerObjectName, $controllerCommandName);
111  $request->setArguments($commandLineArguments);
112  $request->setExceedingArguments($exceedingCommandLineArguments);
113  return $request;
114  }
115 
126  protected function ‪parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName)
127  {
128  $commandLineArguments = [];
129  $exceedingArguments = [];
130  $commandMethodName = $controllerCommandName . 'Command';
131  $commandMethodParameters = $this->reflectionService
132  ->getClassSchema($controllerObjectName)
133  ->getMethod($commandMethodName)['params'] ?? [];
134  $requiredArguments = [];
135  $optionalArguments = [];
136  $argumentNames = [];
137  foreach ($commandMethodParameters as $parameterName => $parameterInfo) {
138  $argumentNames[] = $parameterName;
139  if ($parameterInfo['optional'] === false) {
140  $requiredArguments[strtolower($parameterName)] = ['parameterName' => $parameterName, 'type' => $parameterInfo['type']];
141  } else {
142  $optionalArguments[strtolower($parameterName)] = ['parameterName' => $parameterName, 'type' => $parameterInfo['type']];
143  }
144  }
145  $decidedToUseNamedArguments = false;
146  $decidedToUseUnnamedArguments = false;
147  $argumentIndex = 0;
148  while (!empty($rawCommandLineArguments)) {
149  $rawArgument = array_shift($rawCommandLineArguments);
150  if ($rawArgument[0] === '-') {
151  if ($rawArgument[1] === '-') {
152  $rawArgument = substr($rawArgument, 2);
153  } else {
154  $rawArgument = substr($rawArgument, 1);
155  }
156  $argumentName = $this->‪extractArgumentNameFromCommandLinePart($rawArgument);
157  if (isset($optionalArguments[$argumentName])) {
158  $argumentValue = $this->‪getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $optionalArguments[$argumentName]['type']);
159  $commandLineArguments[$optionalArguments[$argumentName]['parameterName']] = $argumentValue;
160  } elseif (isset($requiredArguments[$argumentName])) {
161  if ($decidedToUseUnnamedArguments) {
162  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected named argument "%s". If you use unnamed arguments, all required arguments must be passed without a name.', $argumentName), 1309971821);
163  }
164  $decidedToUseNamedArguments = true;
165  $argumentValue = $this->‪getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $requiredArguments[$argumentName]['type']);
166  $commandLineArguments[$requiredArguments[$argumentName]['parameterName']] = $argumentValue;
167  unset($requiredArguments[$argumentName]);
168  }
169  } else {
170  if (!empty($requiredArguments)) {
171  if ($decidedToUseNamedArguments) {
172  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected unnamed argument "%s". If you use named arguments, all required arguments must be passed named.', $rawArgument), 1309971820);
173  }
174  $argument = array_shift($requiredArguments);
175  $commandLineArguments[$argument['parameterName']] = $rawArgument;
176  $decidedToUseUnnamedArguments = true;
177  } else {
178  if ($argumentIndex < count($argumentNames)) {
179  $commandLineArguments[$argumentNames[$argumentIndex]] = $rawArgument;
180  } else {
181  $exceedingArguments[] = $rawArgument;
182  }
183  }
184  }
185  $argumentIndex++;
186  }
187  return [$commandLineArguments, $exceedingArguments];
188  }
189 
196  protected function ‪extractArgumentNameFromCommandLinePart($commandLinePart)
197  {
198  $nameAndValue = explode('=', $commandLinePart, 2);
199  return strtolower(str_replace('-', '', $nameAndValue[0]));
200  }
201 
210  protected function ‪getValueOfCurrentCommandLineOption($currentArgument, array &$rawCommandLineArguments, $expectedArgumentType)
211  {
212  if (!isset($rawCommandLineArguments[0]) && strpos($currentArgument, '=') === false || isset($rawCommandLineArguments[0]) && $rawCommandLineArguments[0][0] === '-' && strpos($currentArgument, '=') === false) {
213  return true;
214  }
215  if (strpos($currentArgument, '=') === false) {
216  $possibleValue = trim(array_shift($rawCommandLineArguments));
217  if (strpos($possibleValue, '=') === false) {
218  if ($expectedArgumentType !== 'boolean') {
219  return $possibleValue;
220  }
221  if (in_array($possibleValue, ['on', '1', 'y', 'yes', 'true', 'TRUE'], true)) {
222  return true;
223  }
224  if (in_array($possibleValue, ['off', '0', 'n', 'no', 'false', 'FALSE'], true)) {
225  return false;
226  }
227  array_unshift($rawCommandLineArguments, $possibleValue);
228  return true;
229  }
230  $currentArgument .= $possibleValue;
231  }
232  $splitArgument = explode('=', $currentArgument, 2);
233  while ((!isset($splitArgument[1]) || trim($splitArgument[1]) === '') && !empty($rawCommandLineArguments)) {
234  $currentArgument .= array_shift($rawCommandLineArguments);
235  $splitArgument = explode('=', $currentArgument);
236  }
237  $value = $splitArgument[1] ?? '';
238  return $value;
239  }
240 }
‪TYPO3\CMS\Extbase\Mvc\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\extractArgumentNameFromCommandLinePart
‪string extractArgumentNameFromCommandLinePart($commandLinePart)
Definition: RequestBuilder.php:192
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\injectCommandManager
‪injectCommandManager(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager)
Definition: RequestBuilder.php:59
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\$commandManager
‪TYPO3 CMS Extbase Mvc Cli CommandManager $commandManager
Definition: RequestBuilder.php:34
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\injectReflectionService
‪injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
Definition: RequestBuilder.php:51
‪TYPO3\CMS\Extbase\Mvc\Cli
Definition: Command.php:2
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $configurationManager
Definition: RequestBuilder.php:38
‪TYPO3\CMS\Extbase\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: RequestBuilder.php:26
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
Definition: CommandManager.php:23
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\getValueOfCurrentCommandLineOption
‪string getValueOfCurrentCommandLineOption($currentArgument, array &$rawCommandLineArguments, $expectedArgumentType)
Definition: RequestBuilder.php:206
‪TYPO3\CMS\Extbase\Mvc\Cli\Command
Definition: Command.php:25
‪TYPO3\CMS\Extbase\Mvc\Cli\Request\setCallingScript
‪setCallingScript($callingScript)
Definition: Request.php:79
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder
Definition: RequestBuilder.php:23
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\build
‪TYPO3 CMS Extbase Mvc Cli Request build($commandLine='', $callingScript='./typo3/sysext/core/bin/typo3')
Definition: RequestBuilder.php:83
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: RequestBuilder.php:43
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\$reflectionService
‪TYPO3 CMS Extbase Reflection ReflectionService $reflectionService
Definition: RequestBuilder.php:30
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\parseRawCommandLineArguments
‪array parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName)
Definition: RequestBuilder.php:122
‪TYPO3\CMS\Extbase\Mvc\Cli\RequestBuilder\injectConfigurationManager
‪injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
Definition: RequestBuilder.php:67