TYPO3 CMS  TYPO3_6-2
RequestBuilder.php
Go to the documentation of this file.
1 <?php
3 
22 
27  protected $objectManager;
28 
33  protected $reflectionService;
34 
39  protected $commandManager;
40 
46 
58  public function build($commandLine = '', $callingScript = './typo3/cli_dispatch.phpsh extbase') {
59  $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Request');
60  $request->setCallingScript($callingScript);
61  $request->setControllerObjectName('TYPO3\\CMS\\Extbase\\Command\\HelpCommandController');
62  $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine);
63  if (count($rawCommandLineArguments) === 0) {
64  $request->setControllerCommandName('helpStub');
65  return $request;
66  }
67  $commandIdentifier = trim(array_shift($rawCommandLineArguments));
68  try {
69  $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
70  $this->configurationManager->setConfiguration(array('extensionName' => $command->getExtensionName()));
71  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception) {
72  $request->setArgument('exception', $exception);
73  $request->setControllerCommandName('error');
74  return $request;
75  }
76  $controllerObjectName = $command->getControllerClassName();
77  $controllerCommandName = $command->getControllerCommandName();
78  $request->setControllerObjectName($controllerObjectName);
79  $request->setControllerCommandName($controllerCommandName);
80  list($commandLineArguments, $exceedingCommandLineArguments) = $this->parseRawCommandLineArguments($rawCommandLineArguments, $controllerObjectName, $controllerCommandName);
81  $request->setArguments($commandLineArguments);
82  $request->setExceedingArguments($exceedingCommandLineArguments);
83  return $request;
84  }
85 
96  protected function parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName) {
97  $commandLineArguments = array();
98  $exceedingArguments = array();
99  $commandMethodName = $controllerCommandName . 'Command';
100  $commandMethodParameters = $this->reflectionService->getMethodParameters($controllerObjectName, $commandMethodName);
101  $requiredArguments = array();
102  $optionalArguments = array();
103  $argumentNames = array();
104  foreach ($commandMethodParameters as $parameterName => $parameterInfo) {
105  $argumentNames[] = $parameterName;
106  if ($parameterInfo['optional'] === FALSE) {
107  $requiredArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']);
108  } else {
109  $optionalArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']);
110  }
111  }
112  $decidedToUseNamedArguments = FALSE;
113  $decidedToUseUnnamedArguments = FALSE;
114  $argumentIndex = 0;
115  while (count($rawCommandLineArguments) > 0) {
116  $rawArgument = array_shift($rawCommandLineArguments);
117  if ($rawArgument[0] === '-') {
118  if ($rawArgument[1] === '-') {
119  $rawArgument = substr($rawArgument, 2);
120  } else {
121  $rawArgument = substr($rawArgument, 1);
122  }
123  $argumentName = $this->extractArgumentNameFromCommandLinePart($rawArgument);
124  if (isset($optionalArguments[$argumentName])) {
125  $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $optionalArguments[$argumentName]['type']);
126  $commandLineArguments[$optionalArguments[$argumentName]['parameterName']] = $argumentValue;
127  } elseif (isset($requiredArguments[$argumentName])) {
128  if ($decidedToUseUnnamedArguments) {
129  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);
130  }
131  $decidedToUseNamedArguments = TRUE;
132  $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $requiredArguments[$argumentName]['type']);
133  $commandLineArguments[$requiredArguments[$argumentName]['parameterName']] = $argumentValue;
134  unset($requiredArguments[$argumentName]);
135  }
136  } else {
137  if (count($requiredArguments) > 0) {
138  if ($decidedToUseNamedArguments) {
139  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);
140  }
141  $argument = array_shift($requiredArguments);
142  $commandLineArguments[$argument['parameterName']] = $rawArgument;
143  $decidedToUseUnnamedArguments = TRUE;
144  } else {
145  if ($argumentIndex < count($argumentNames)) {
146  $commandLineArguments[$argumentNames[$argumentIndex]] = $rawArgument;
147  } else {
148  $exceedingArguments[] = $rawArgument;
149  }
150  }
151  }
152  $argumentIndex++;
153  }
154  return array($commandLineArguments, $exceedingArguments);
155  }
156 
163  protected function extractArgumentNameFromCommandLinePart($commandLinePart) {
164  $nameAndValue = explode('=', $commandLinePart, 2);
165  return strtolower(str_replace('-', '', $nameAndValue[0]));
166  }
167 
176  protected function getValueOfCurrentCommandLineOption($currentArgument, array &$rawCommandLineArguments, $expectedArgumentType) {
177  if (!isset($rawCommandLineArguments[0]) && strpos($currentArgument, '=') === FALSE || isset($rawCommandLineArguments[0]) && $rawCommandLineArguments[0][0] === '-' && strpos($currentArgument, '=') === FALSE) {
178  return TRUE;
179  }
180  if (strpos($currentArgument, '=') === FALSE) {
181  $possibleValue = trim(array_shift($rawCommandLineArguments));
182  if (strpos($possibleValue, '=') === FALSE) {
183  if ($expectedArgumentType !== 'boolean') {
184  return $possibleValue;
185  }
186  if (array_search($possibleValue, array('on', '1', 'y', 'yes', 'true', 'TRUE')) !== FALSE) {
187  return TRUE;
188  }
189  if (array_search($possibleValue, array('off', '0', 'n', 'no', 'false', 'FALSE')) !== FALSE) {
190  return FALSE;
191  }
192  array_unshift($rawCommandLineArguments, $possibleValue);
193  return TRUE;
194  }
195  $currentArgument .= $possibleValue;
196  }
197  $splitArgument = explode('=', $currentArgument, 2);
198  while ((!isset($splitArgument[1]) || trim($splitArgument[1]) === '') && count($rawCommandLineArguments) > 0) {
199  $currentArgument .= array_shift($rawCommandLineArguments);
200  $splitArgument = explode('=', $currentArgument);
201  }
202  $value = isset($splitArgument[1]) ? $splitArgument[1] : '';
203  return $value;
204  }
205 }
parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName)
extractArgumentNameFromCommandLinePart($commandLinePart)
getValueOfCurrentCommandLineOption($currentArgument, array &$rawCommandLineArguments, $expectedArgumentType)
build($commandLine='', $callingScript='./typo3/cli_dispatch.phpsh extbase')