Returns the executed command.
public getCommand ( ) : |
||
return | The executed command. |
/** * @param PreHandleEvent $event */ public function printVersion(PreHandleEvent $event) { if ($event->getArgs()->isOptionSet('version')) { $version = new NameVersion($event->getCommand()->getApplication()->getConfig()); $version->render($event->getIO()); $event->setHandled(true); } }
/** * @param PreHandleEvent $event */ protected function onPreHandle(PreHandleEvent $event) { // convert console args to command $this->commandConverter->setArgs($event->getArgs()); $this->commandConverter->setFormat($event->getCommand()->getArgsFormat()); /** @var CommandConfig $commandConfig */ $commandConfig = $event->getCommand()->getConfig(); if ($event->getCommand()->getName() !== 'help') { if ($commandConfig->className() === null && count($commandConfig->getSubCommandConfigs()) === 0) { throw new \RuntimeException(sprintf('The command %s definition must set the className using ->setClass() method', $event->getCommand()->getName())); } } $command = $this->commandConverter->getCommandFrom($commandConfig->className()); $this->defaultHandler->setIo($event->getIO()); $this->defaultHandler->clearEventHandlers(); if ($commandConfig->preDispatchEventHandler() !== null) { $this->defaultHandler->addPreDispatchEventHandler($commandConfig->preDispatchEventHandler()); } if ($commandConfig->postDispatchEventHandler()) { $this->defaultHandler->addPostDispatchEventHandler($commandConfig->postDispatchEventHandler()); } $commandConfig->setEventBus($this->eventBus); $commandConfig->addEventSubscriber($this->defaultHandler); if ($command !== null) { if ($command instanceof ConsoleCommandInterface) { $command->setIo($event->getIO()); } try { $this->commandBus->dispatch($command); $event->setHandled(true); $event->setStatusCode(0); } catch (NotFoundException $e) { } } else { if ($event->getCommand()->getName() !== 'help') { $helpCommand = $this->getCommand('help'); $args = $event->getArgs(); $args = $helpCommand->parseArgs($args->getRawArgs()); $args->setArgument('command', $event->getCommand()->getName()); $helpConfig = $this->getConfig()->getCommandConfig('help'); $commandHandler = $helpConfig->getHandler(); $handlerMethod = $helpConfig->getHandlerMethod(); $commandHandler->{$handlerMethod}($args, $event->getIO(), $helpCommand); $event->setHandled(true); $event->setStatusCode(0); } } }