/** * @param DispatcherInterface $dispatcher * @param callable $logger */ public function __construct(DispatcherInterface $dispatcher, callable $logger = null) { $dispatcher->addListener('*', array($this, 'loggingListener')); if ($logger === null) { $logger = function ($msg) { error_log($msg); }; } $this->addHelper('*', array($this, 'defaultHelper')); $this->dispatcher = $dispatcher; $this->logger = $logger; }
/** * A callback function to which the consumed message will be passed. * All messages are dispatched as events. * Catched errors are dispatched as well using special event "kemer.error". * NotConsumedException is threw - when dispatched message is not marked * as `consumed` either by acknowledging or rejecting * * @param AMQPEnvelope $envelope * @param AMQPQueue $queue * @return void * @throws Exceptions\ConsumerException * @throws Exceptions\NotConsumedException */ public function __invoke(\AMQPEnvelope $envelope, \AMQPQueue $queue) { $event = new ConsumeEvent($envelope, $queue); try { $this->dispatcher->dispatch($event->getRoutingKey(), $event); } catch (\Exception $e) { $this->dispatcher->dispatch(static::ERROR_EVENT, new SymfonyEvent($event, ["error" => $e])); if (!$event->isConsumed()) { throw new Exceptions\ConsumerException($event, $e); } } if (!$event->isConsumed()) { throw new Exceptions\NotConsumedException($event); } }
/** * Dispatches an event to all registered listeners. * * @param EventInterface $event The event to pass to the event handlers/listeners. * * @return EventInterface The event after being passed through all listeners. * * @since __DEPLOY_VERSION__ */ public function dispatch(EventInterface $event) { return $this->dispatcher->dispatch($event); }
/** * Trigger an event. * * @param EventInterface|string $event The event object or name. * * @return EventInterface The event after being passed through all listeners. * * @since 1.0 */ public function triggerEvent($event) { return $this->dispatcher->triggerEvent($event); }
/** * Adds an event listener that listens on the specific event. * * @param string $eventName The event to listen on. * @param callable $callback The listener. * @param int $priority The higher this value, the earlier an event * listener will be triggered in the chain (defaults to 0). * @return self */ public function on($eventName, callable $callback, $priority = 0) { $listener = new Listener($eventName, $callback); $this->dispatcher->addListener($eventName, $listener, $priority); return $this; }