/** * @param callable|ApplicationConfig $config * * @return ConsoleApplication */ protected function createApplication($config) { $commandBus = CommandBus::create(); $eventBus = DomainEventPublisher::eventBus(); $migrationsService = new MigrationsService(new Migrator($this->getClassMetadataFactory(), new InMemoryMigrationStore(), new InMemoryEventStore(), $this->migrationsDirectory)); $commandBus->addHandler(MigrationsGenerateCommand::class, $migrationsService); $commandBus->addHandler(MigrationsMigrateCommand::class, $migrationsService); $commandBus->addHandler(MigrationsStatusCommand::class, $migrationsService); return new ConsoleApplication($config, $commandBus, $eventBus); }
/** * @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); } } }
/** * Test handlerMiddleware method. */ public function testHandlerMiddleware() { $this->given($commandBus = CommandBus::create())->when($middleware = $commandBus->handlerMiddleware())->then()->object($middleware)->isInstanceOf(CommandHandlerMiddleware::class); }
/** * @param callable|ApplicationConfig $config * * @return ConsoleApplication */ protected function createApplication($config) { $commandBus = CommandBus::create(); $eventBus = EventBus::create(); $postService = new PostService($eventBus); $commandBus->addHandler(CreateBlogCommand::class, new BlogService($eventBus)); $commandBus->addHandler(CreatePostCommand::class, $postService); $commandBus->addHandler(ChangePostTitleCommand::class, $postService); return new ConsoleApplication($config, $commandBus, $eventBus); }