/** * Builds a CLI request object from a command line. * * The given command line may be a string (e.g. "myextension:foo do-that-thing --force") or * an array consisting of the individual parts. The array must not include the script * name (like in $argv) but start with command right away. * * @param mixed $commandLine The command line, either as a string or as an array * @param string $callingScript The calling script (usually ./typo3/cli_dispatch.phpsh) * @return \TYPO3\CMS\Extbase\Mvc\Cli\Request The CLI request as an object */ public function build($commandLine = '', $callingScript = './typo3/cli_dispatch.phpsh extbase') { $request = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Request::class); $request->setCallingScript($callingScript); $request->setControllerObjectName(\TYPO3\CMS\Extbase\Command\HelpCommandController::class); $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine); if (empty($rawCommandLineArguments)) { $request->setControllerCommandName('helpStub'); return $request; } $commandIdentifier = trim(array_shift($rawCommandLineArguments)); try { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); $this->configurationManager->setConfiguration(array('extensionName' => $command->getExtensionName())); } catch (\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception) { $request->setArgument('exception', $exception); $request->setControllerCommandName('error'); return $request; } $controllerObjectName = $command->getControllerClassName(); $controllerCommandName = $command->getControllerCommandName(); $request->setControllerObjectName($controllerObjectName); $request->setControllerCommandName($controllerCommandName); list($commandLineArguments, $exceedingCommandLineArguments) = $this->parseRawCommandLineArguments($rawCommandLineArguments, $controllerObjectName, $controllerCommandName); $request->setArguments($commandLineArguments); $request->setExceedingArguments($exceedingCommandLineArguments); return $request; }
/** * @test * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException * @author Bastian Waidelich <*****@*****.**> */ public function getCommandByIdentifierThrowsExceptionIfMoreThanOneMatchingCommandWasFound() { $mockCommand1 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, array(), array(), '', FALSE); $mockCommand1->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extensionkey:controller:command')); $mockCommand2 = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, array(), array(), '', FALSE); $mockCommand2->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('otherextensionkey:controller:command')); $mockCommands = array($mockCommand1, $mockCommand2); $this->commandManager->expects($this->once())->method('getAvailableCommands')->will($this->returnValue($mockCommands)); $this->commandManager->getCommandByIdentifier('controller:command'); }
/** * @test * @author Robert Lemke <*****@*****.**> * @author Bastian Waidelich <*****@*****.**> */ public function ifCommandCantBeResolvedTheHelpScreenIsShown() { // The following call is only made to satisfy PHPUnit. For some weird reason PHPUnit complains that the // mocked method ("getObjectNameByClassName") does not exist _if the mock object is not used_. $this->mockCommandManager->getCommandByIdentifier('some_extension_name:default:list'); $mockCommandManager = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class); $mockCommandManager->expects($this->any())->method('getCommandByIdentifier')->with('test:default:list')->will($this->throwException(new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException())); $this->requestBuilder->_set('commandManager', $mockCommandManager); $request = $this->requestBuilder->build('test:default:list'); $this->assertSame(\TYPO3\CMS\Extbase\Command\HelpCommandController::class, $request->getControllerObjectName()); }
/** * Gets the default value of argument * * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument * @return mixed */ protected function getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument) { $type = $this->getArgumentType($argument); $argumentName = $argument->getName(); $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier()); $argumentReflection = $this->reflectionService->getMethodParameters($command->getControllerClassName(), $command->getControllerCommandName() . 'Command'); $defaultValue = $argumentReflection[$argumentName]['defaultValue']; if ($type === 'boolean') { $defaultValue = (bool) $defaultValue ? 1 : 0; } return $defaultValue; }
/** * Execute Task * * If errors occur during Task execution they are thrown as Exceptions which * must be caught manually if you manually execute Tasks through your code. * * @param \TYPO3\CMS\Extbase\Scheduler\Task $task the task to execute * @return void */ public function execute(\TYPO3\CMS\Extbase\Scheduler\Task $task) { $commandIdentifier = $task->getCommandIdentifier(); list($extensionKey, $controllerName, $commandName) = explode(':', $commandIdentifier); $extensionName = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($extensionKey); $this->initialize(array('extensionName' => $extensionName)); // execute command $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); $this->request->setControllerObjectName($command->getControllerClassName()); $this->request->setControllerCommandName($command->getControllerCommandName()); $this->request->setArguments($task->getArguments()); $this->dispatcher->dispatch($this->request, $this->response); $this->shutdown(); }
/** * Render help text for a single command * * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command * @return void */ protected function displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command) { $this->outputLine(); $this->outputLine($command->getShortDescription()); $this->outputLine(); $this->outputLine('COMMAND:'); $this->outputLine('%-2s%s', array(' ', $command->getCommandIdentifier())); $commandArgumentDefinitions = $command->getArgumentDefinitions(); $usage = ''; $hasOptions = FALSE; foreach ($commandArgumentDefinitions as $commandArgumentDefinition) { if (!$commandArgumentDefinition->isRequired()) { $hasOptions = TRUE; } else { $usage .= sprintf(' <%s>', strtolower(preg_replace('/([A-Z])/', ' $1', $commandArgumentDefinition->getName()))); } } $usage = $this->request->getCallingScript() . ' ' . $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage; $this->outputLine(); $this->outputLine('USAGE:'); $this->outputLine(' ' . $usage); $argumentDescriptions = array(); $optionDescriptions = array(); if ($command->hasArguments()) { foreach ($commandArgumentDefinitions as $commandArgumentDefinition) { $argumentDescription = $commandArgumentDefinition->getDescription(); $argumentDescription = wordwrap($argumentDescription, $this->output->getMaximumLineLength() - 23, PHP_EOL . str_repeat(' ', 23), TRUE); if ($commandArgumentDefinition->isRequired()) { $argumentDescriptions[] = vsprintf(' %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription)); } else { $optionDescriptions[] = vsprintf(' %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription)); } } } if (!empty($argumentDescriptions)) { $this->outputLine(); $this->outputLine('ARGUMENTS:'); foreach ($argumentDescriptions as $argumentDescription) { $this->outputLine($argumentDescription); } } if (!empty($optionDescriptions)) { $this->outputLine(); $this->outputLine('OPTIONS:'); foreach ($optionDescriptions as $optionDescription) { $this->outputLine($optionDescription); } } if ($command->getDescription() !== '') { $this->outputLine(); $this->outputLine('DESCRIPTION:'); $descriptionLines = explode(LF, $command->getDescription()); foreach ($descriptionLines as $descriptionLine) { $this->outputLine('%-2s%s', array(' ', $descriptionLine)); } } $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers(); if ($relatedCommandIdentifiers !== array()) { $this->outputLine(); $this->outputLine('SEE ALSO:'); foreach ($relatedCommandIdentifiers as $commandIdentifier) { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, $command->getShortDescription())); } } $this->outputLine(); }