/** * 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; }
/** * Builds an index of available commands. For each of them a Command object is * added to the commands array of this class. * * @return void */ protected function buildCommandsIndex() { $availableCommands = $this->commandManager->getAvailableCommands(); foreach ($availableCommands as $command) { if ($command->isInternal()) { continue; } $commandIdentifier = $command->getCommandIdentifier(); $extensionKey = strstr($commandIdentifier, ':', TRUE); $commandControllerClassName = $command->getControllerClassName(); $commandName = $command->getControllerCommandName(); $this->commandsByExtensionsAndControllers[$extensionKey][$commandControllerClassName][$commandName] = $command; } }
/** * 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(); }
/** * Make sure the objet manager is set * * @return \TYPO3\CMS\Extbase\Mvc\Cli\Command[] */ public function getAvailableCommands() { $this->initialize(); if ($this->availableCommands === NULL) { $commandControllerRegistry =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers']; if (!empty($commandControllerRegistry) && is_array($commandControllerRegistry)) { $commandControllerRegistry = array_merge($commandControllerRegistry, $this->commandControllers); } else { $commandControllerRegistry = $this->commandControllers; } $this->availableCommands = parent::getAvailableCommands(); } return $this->availableCommands; }