/** * @param EventInterface $event * @return bool|void * @throws BusException */ public function publishEvent(EventInterface $event) { $eventClass = get_class($event); if (!isset($this->eventListenerMap[$eventClass])) { return false; } foreach ($this->eventListenerMap[$eventClass] as $callableOrDefinition) { if (is_callable($callableOrDefinition)) { call_user_func($callableOrDefinition, $event); } if (is_array($callableOrDefinition)) { if (is_null($this->eventListenerLoader)) { throw BusException::loaderNotExistError(sprintf('Can not load the event listener <%s>. No EventListenerLoader found.', $callableOrDefinition['alias'])); } $eventListener = $this->eventListenerLoader->getEventListener($callableOrDefinition['alias']); $method = $callableOrDefinition['method']; // instead of invoking the handler method directly // we call the execute function of the implemented trait and pass along a reference to the gate $usedTraits = class_uses($eventListener); if (!isset($usedTraits['Malocher\\Cqrs\\Adapter\\AdapterTrait'])) { throw BusException::traitError('Adapter Trait is missing! Use it!'); } $eventListener->executeEvent($this, $eventListener, $method, $event); } } return true; }
/** * {@inheritDoc} */ public function publishEvent(EventInterface $event) { // Check if event exists after invoking the PublishEventCommand because // the PublishEventCommand tells that a event is dispatched but does not care // if it succeeded. Later the EventPublishedEvent can be used to check if a // event succeeded. if (!is_null($this->gate->getSystemBus())) { $publishEventCommand = new PublishEventCommand(); $publishEventCommand->setMessageClass(get_class($event)); $publishEventCommand->setMessageVars($event->getMessageVars()); $publishEventCommand->setBusName($this->getName()); $this->gate->getSystemBus()->invokeCommand($publishEventCommand); } try { $response = $this->bus->publishEvent($event); if ($response === false) { return false; } } catch (BusException $ex) { //throw it again throw $ex; } catch (\Exception $ex) { throw BusException::defaultBusError($ex->getMessage(), null, $ex); } // Dispatch the EventPublishedEvent here! If for example a event could not be dispatched // because it does not exist in the eventListenerMap[<empty>] this Event would never // be dispatched! if (!is_null($this->gate->getSystemBus())) { $eventPublishedEvent = new EventPublishedEvent(); $eventPublishedEvent->setMessageClass(get_class($event)); $eventPublishedEvent->setMessageVars($event->getMessageVars()); $eventPublishedEvent->setBusName($this->getName()); $this->gate->getSystemBus()->publishEvent($eventPublishedEvent); } return $response; }
/** * get bus by name * * If no name is provided, the gate tries to find a default bus: * 1. you can set a default bus via {@see setDefaultBusName} * 2. if only one bus is attached, it is treated as default bus * * @param string $name * @return BusInterface|null If the system bus is requested but not enabled, method returns null * @throws Bus\BusException If default bus can not be detected or bus not exists */ public function getBus($name = null) { if ($name == AbstractBus::SYSTEMBUS) { return $this->systemBus; } if (is_null($name)) { if (!is_null($this->defaultBusName)) { $name = $this->defaultBusName; } else { if (count($this->buses) == 1) { return current($this->buses); } else { throw Bus\BusException::defaultBusError('Default bus can not be detected. Please set the default bus or provide a bus name'); } } } if (!isset($this->buses[$name])) { throw Bus\BusException::busNotExistError(sprintf('The bus <%s> can not be found', $name)); } return $this->buses[$name]; }