/** * Forwards the request to another command and / or CommandController. * * Request is directly transferred to the other command / controller * without the need for a new request. * * @param string $commandName * @param string $controllerObjectName * @param array $arguments * @return void * @throws StopActionException */ protected function forward($commandName, $controllerObjectName = NULL, array $arguments = array()) { $this->request->setDispatched(FALSE); $this->request->setControllerCommandName($commandName); if ($controllerObjectName !== NULL) { $this->request->setControllerObjectName($controllerObjectName); } $this->request->setArguments($arguments); $this->arguments->removeAll(); throw new StopActionException(); }
/** * Checks if compile time command was not recognized as such, then runlevel was * booted but it turned out that in fact the command is a compile time command. * * This happens if the user doesn't specify the full command identifier. * * @param string $runlevel * @return void * @throws \TYPO3\Flow\Mvc\Exception\InvalidCommandIdentifierException */ public function exitIfCompiletimeCommandWasNotCalledCorrectly($runlevel) { if ($runlevel === 'Runtime') { $command = $this->request->getCommand(); if ($this->bootstrap->isCompiletimeCommand($command->getCommandIdentifier())) { $this->response->appendContent(sprintf("<b>Unrecognized Command</b>\n\n" . "Sorry, but he command \"%s\" must be specified by its full command\n" . "identifier because it is a compile time command which cannot be resolved\n" . "from an abbreviated command identifier.\n\n", $command->getCommandIdentifier())); $this->response->send(); $this->shutdown($runlevel); exit(1); } } }
/** * @test */ public function setControllerObjectNameAndSetControllerCommandNameUnsetTheBuiltCommandObject() { $request = new Request(); $request->setControllerObjectName(\TYPO3\Flow\Command\CacheCommandController::class); $request->setControllerCommandName('flush'); $request->getCommand(); $request->setControllerObjectName('TYPO3\\Flow\\Command\\BeerCommandController'); $request->setControllerCommandName('drink'); $command = $request->getCommand(); $this->assertEquals('typo3.flow:beer:drink', $command->getCommandIdentifier()); }
/** * Calls the specified command method and passes the arguments. * * If the command returns a string, it is appended to the content in the * response object. If the command doesn't return anything and a valid * view exists, the view is rendered automatically. * * @return void */ protected function callCommandMethod() { $preparedArguments = array(); /** @var Argument $argument */ foreach ($this->arguments as $argument) { $preparedArguments[] = $argument->getValue(); } $command = new Command(get_class($this), $this->request->getControllerCommandName()); if ($command->isDeprecated()) { $suggestedCommandMessage = ''; $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers(); if ($relatedCommandIdentifiers !== array()) { $suggestedCommandMessage = sprintf(', use the following command%s instead: %s', count($relatedCommandIdentifiers) > 1 ? 's' : '', implode(', ', $relatedCommandIdentifiers)); } $this->outputLine('<b>Warning:</b> This command is <b>DEPRECATED</b>%s%s', array($suggestedCommandMessage, PHP_EOL)); } $commandResult = call_user_func_array(array($this, $this->commandMethodName), $preparedArguments); if (is_string($commandResult) && strlen($commandResult) > 0) { $this->response->appendContent($commandResult); } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) { $this->response->appendContent((string) $commandResult); } }
/** * Builds a CLI request object from a command line. * * The given command line may be a string (e.g. "mypackage: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 * @return Request The CLI request as an object * @throws InvalidArgumentMixingException * @throws InvalidArgumentNameException */ public function build($commandLine) { $request = new Request(); $request->setControllerObjectName(HelpCommandController::class); if (is_array($commandLine) === true) { $rawCommandLineArguments = $commandLine; } else { preg_match_all(self::ARGUMENT_MATCHING_EXPRESSION, $commandLine, $commandLineMatchings, PREG_SET_ORDER); $rawCommandLineArguments = []; foreach ($commandLineMatchings as $match) { if (isset($match['NoQuotes'])) { $rawCommandLineArguments[] = str_replace(['\\ ', '\\"', "\\'", '\\\\'], [' ', '"', "'", '\\'], $match['NoQuotes']); } elseif (isset($match['DoubleQuotes'])) { $rawCommandLineArguments[] = str_replace('\\"', '"', $match['DoubleQuotes']); } elseif (isset($match['SingleQuotes'])) { $rawCommandLineArguments[] = str_replace('\\\'', '\'', $match['SingleQuotes']); } else { throw new InvalidArgumentNameException(sprintf('Could not parse the command line "%s" - specifically the part "%s".', $commandLine, $match[0])); } } } if (count($rawCommandLineArguments) === 0) { $request->setControllerCommandName('helpStub'); return $request; } $commandIdentifier = trim(array_shift($rawCommandLineArguments)); try { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); } catch (CommandException $exception) { $request->setArgument('exception', $exception); $request->setControllerCommandName('error'); return $request; } $controllerObjectName = $this->objectManager->getObjectNameByClassName($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; }
/** * Builds a CLI request object from a command line. * * The given command line may be a string (e.g. "mypackage: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 * @return \TYPO3\Flow\Cli\Request The CLI request as an object */ public function build($commandLine) { $request = new Request(); $request->setControllerObjectName('TYPO3\\Flow\\Command\\HelpCommandController'); $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine); if (count($rawCommandLineArguments) === 0) { $request->setControllerCommandName('helpStub'); return $request; } $commandIdentifier = trim(array_shift($rawCommandLineArguments)); try { $command = $this->commandManager->getCommandByIdentifier($commandIdentifier); } catch (\TYPO3\Flow\Mvc\Exception\CommandException $exception) { $request->setArgument('exception', $exception); $request->setControllerCommandName('error'); return $request; } $controllerObjectName = $this->objectManager->getObjectNameByClassName($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; }
/** * Lists all available users. */ public function listUsersCommand() { // If we're in Neos context, we pass on the command. if ($this->shouldUseNeosService()) { $cliRequest = new Request($this->request); $cliRequest->setControllerObjectName(UserCommandController::class); $cliRequest->setControllerCommandName('list'); $cliResponse = new Response($this->response); $this->dispatcher->dispatch($cliRequest, $cliResponse); return; } /** @var User[] $users */ $users = $this->userRepository->findAll()->toArray(); usort($users, function ($a, $b) { /** @var User $a */ /** @var User $b */ return $a->getEmail() > $b->getEmail(); }); $tableRows = []; $headerRow = ['Email', 'Name', 'Role(s)']; foreach ($users as $user) { $tableRows[] = [$user->getEmail(), $user->getFullName(), implode(' ,', $user->getAccount()->getRoles())]; } $this->output->outputTable($tableRows, $headerRow); $this->outputLine(sprintf(' <b>%s users total.</b>', count($users))); }