public function index() { // Setup the command handler $commandHandler = new ExampleCommandHandler(); // Create a command bus and subscribe the command handler at the command bus $commandBus = new SimpleCommandBus(); $commandBus->subscribe($commandHandler); // Create and dispatch the command! $command = new ExampleCommand('Hi from command!'); $commandBus->dispatch($command); }
/** * @param ServiceLocatorInterface $serviceLocator * @return SimpleCommandBus */ public function __invoke(ServiceLocatorInterface $serviceLocator) { $commandBus = new SimpleCommandBus(); $config = $serviceLocator->get('Config'); if (!isset($config['broadway']['command_handlers'])) { throw new InvalidArgumentException('Missing command handlers config'); } $handlerList = $config['broadway']['command_handlers']; foreach ($handlerList as $handlerName) { /* @var CommandHandlerInterface $handler */ $handler = $serviceLocator->get($handlerName); if (!$handler instanceof CommandHandlerInterface) { throw new InvalidArgumentException(sprintf('Command handler must be an instance of %s, %s given.', CommandHandlerInterface::class, is_object($handler) ? get_class($handler) : gettype($handler))); } $commandBus->subscribe($handler); } return $commandBus; }