PHPackages                             webit/message-bus-sf-event-dispatcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. webit/message-bus-sf-event-dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

webit/message-bus-sf-event-dispatcher
=====================================

1.0.1(8y ago)0301MITPHPPHP &gt;=7.0

Since Jan 1Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dbojdo/message-bus-sf-event-dispatcher)[ Packagist](https://packagist.org/packages/webit/message-bus-sf-event-dispatcher)[ RSS](/packages/webit-message-bus-sf-event-dispatcher/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (5)Versions (3)Used By (1)

Message Bus - Symfony Event Dispatcher Infrastructure
=====================================================

[](#message-bus---symfony-event-dispatcher-infrastructure)

Symfony Event Dispatcher infrastructure for Message Bus

Installation
------------

[](#installation)

```
composer require webit/message-bus-sf-event-dispatcher=^1.0.0
```

Usage
-----

[](#usage)

### Publisher integration

[](#publisher-integration)

To publish ***Message*** via Symfony Event Dispatcher use ***EventDispatcherPublisher***

### MessageBusEventFactory

[](#messagebuseventfactory)

You need to tell the ***EventDispatcherPublisher*** how to translate your ***Message***into the event name and event object of Symfony Event Dispatcher. Implement and configure ***MessageBusEventFactory***.

#### MessageBusEventFactory: Example

[](#messagebuseventfactory-example)

Let's say you're going to publish messages of two types: **type-1** and **type-2**and you want to map then to two different Events of Symfony Event Dispatcher **Event1** and **Event2**.

```
use Symfony\Component\EventDispatcher\Event;

class Event1 extends Event
{
    private $x;

    public function __construct($x) {
        $this->x = $x;
    }

    public function x()
    {
        return $this->x;
    }
}

class Event2 extends Event
{
    private $y;

    private $z;

    public function __construct($y, $z) {
        $this->y = $y;
        $this->z = $z;
    }

    public function y()
    {
        return $this->y;
    }

    public function z()
    {
        return $this->z;
    }
}
```

##### Option 1: implement MessageBusEventFactory

[](#option-1-implement-messagebuseventfactory)

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\Event\MessageBusEventFactory;

class MessageBusEvent1Factory implements MessageBusEventFactory
{
    public function create(Message $message): MessageBusEventFactory
    {
        $arContent = json_decode($message->content(), true);
        return new MessageBusEvent(
            $message->type(),
            new Event1(isset($arContent['x']) ? $arContent['x'] : '')
        );
    }
}

class MessageBusEvent2Factory implements MessageBusEventFactory
{
    public function create(Message $message): MessageBusEventFactory
    {
        $arContent = json_decode($message->content(), true);
        return new MessageBusEvent(
            $message->type(),
            new Event2(
                isset($arContent['y']) ? $arContent['y'] : '',
                isset($arContent['z']) ? $arContent['z'] : '',
            )
        );
    }
}
```

Then combine both factories together

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\Event\ByMessageTypeMessageBusEventFactory;
$messageBusEventFactory = new ByMessageTypeMessageBusEventFactory([
    'type-1' => new MessageBusEvent1Factory(),
    'type-2' => new MessageBusEvent2Factory()
]);
```

##### Option 2: Use GenericMessageBusEventFactory and implement its dependencies

[](#option-2-use-genericmessagebuseventfactory-and-implement-its-dependencies)

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\Event\Symfony\CallbackSymfonyEventFactory;

$eventFactory1 = new CallbackSymfonyEventFactory(
    function (Message $message) {
        $arContent = json_decode($message->content(), true);
        return new MessageBusEvent(
            $message->type(),
            new Event1(isset($arContent['x']) ? $arContent['x'] : '')
        );
    }
);

$eventFactory2 = new CallbackSymfonyEventFactory(
    function (Message $message) {
        $arContent = json_decode($message->content(), true);
        return new MessageBusEvent(
            $message->type(),
            new Event2(
                isset($arContent['y']) ? $arContent['y'] : '',
                isset($arContent['z']) ? $arContent['z'] : '',
            )
        );
    }
);
```

then

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\Event\GenericMessageBusEventFactory;
$messageBusEventFactory1 = new GenericMessageBusEventFactory(
    $eventFactory1,
    new FromMessageTypeEventNameResolver() // optional, used be default, you can provide a different implemenation
);

$messageBusEventFactory2 = new GenericMessageBusEventFactory(
    $eventFactory2
);

// combine both factories together
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\Event\ByMessageTypeMessageBusEventFactory;

$messageBusEventFactory = new ByMessageTypeMessageBusEventFactory([
    'type-1' => $messageBusEventFactory1,
    'type-2' => $messageBusEventFactory2
]);
```

##### Option 3: Implement your own strategy

[](#option-3-implement-your-own-strategy)

As ***EventDispatcherPublisher*** expects an interface ***MessageBusEventFactory*** as a dependency, you can provide your own implementation for it. Also you can provide and combine inner interfaces used by ***GenericMessageBusEventFactory***: ***SymfonyEventFactory*** and ***EventNameResolver***.

If you like [JMSSerializer](https://jmsyst.com/libs/serializer) to produce Symfony Event object, use ***JMSSerializerSymfonyEventFactory***.

#### Putting the stuff together

[](#putting-the-stuff-together)

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Publisher\EventDispatcherPublisher;
use Webit\MessageBus\Message;
use Symfony\Component\EventDispatcher\EventDispatcher;

$eventDispatcher = new EventDispatcher();

$publisher = new EventDispatcherPublisher(
    $eventDispatcher,
    $messageBusEventFactory
);

$message = new Message('type-1', '{"x":"some-x"}');
$publisher->publish($message); // will be dispatched as "event-1" and event of "Event1" class

$message = new Message('type-2', '{"y":"some-y","z":"some-z"}');
$publisher->publish($message); // will be dispatched as "event-1" and event of "Event1" class
```

### Event consumption

[](#event-consumption)

#### Why to consume events at all?

[](#why-to-consume-events-at-all)

1. Dispatches public events to the Message Bus If you want some events to be public and other applications be able to listen to them, use ***PublishingConsumer*** to publish them using different infrastructure (AMQP for example).
2. Asynchronous events processing If you want some events to be processed asynchronously, use ***PublishingConsumer*** to publish them using different infrastructure (AMQP for example), then listen for them.

To consume ***Message*** created from Event of Symfony Event Dispatcher, use ***EventConsumingListener***. It requires ***MessageFromEventFactory*** and ***Consumer*** to be provided.

#### GenericMessageFromEventFactory

[](#genericmessagefromeventfactory)

It requires ***EventSerialiser*** and ***MessageTypeResolver*** (by default uses event name)

##### Option 1: Implement own EventSerialiser

[](#option-1-implement-own-eventserialiser)

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\Message\Content\EventSerialiser;
use Symfony\Component\EventDispatcher\Event;

class Event1Serializer implements EventSerialiser
{
    public function serialise(MessageBusEvent $event): string
    {
        $symfonyEvent = $event->event();
        if ($symfonyEvent instanceof Event1) {
            return json_encode(['x' => $symfonyEvent->x()]);
        }
        throw new \InvalidArgumentException('Event must be an instance of Event1.');
    }
}
```

##### Option 2: Use JMSSerializer to Serialise Event

[](#option-2-use-jmsserializer-to-serialise-event)

```
use JMS\Serializer\SerializerBuilder;
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\Message\Content\JmsEventSerialiser;
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\Message\Content\EventOnlySerialisationDataProvider;

$serializerBuilder = SerializerBuilder::create();

// configure Serializer

$serializer = $serializerBuilder->build();

$jsmEventSerialiser = new JmsEventSerialiser(
    $serializer,
    new EventOnlySerialisationDataProvider(), // used by default, provides data to be passed to the JMSSerializer,
    JmsEventSerialiser::FORMAT_JSON // JSON by default, can be JmsEventSerialiser::FORMAT_XML as well
);
```

#### Use FromMessageAwareEventMessageFromEventFactory

[](#use-frommessageawareeventmessagefromeventfactory)

Your event can optionally implements ***MessageAwareEvent*** interface.

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\Message\MessageAwareEvent;
use Symfony\Component\EventDispatcher\Event;

class EventX extends Event implements MessageAwareEvent
{
    public function createMessage(string $eventName): Message
    {
        return new Message($eventName, json_decode(['some'=>'stuff']));
    }
}
```

Then you can use ***FromMessageAwareEventMessageFromEventFactory*** to produce an event

#### Putting all together

[](#putting-all-together)

Configure ***MessageFromEventFactory***

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\Message\ByEventNameMessageFromEventFactory;

$messageFactory = new ByEventNameMessageFromEventFactory([
    'type-1' => new GenericMessageFromEventFactory(
        new Event1Serializer()
    ),
    'type-2' => new GenericMessageFromEventFactory($jsmEventSerialiser)
]);
```

Create a listener

```
use Webit\MessageBus\Infrastructure\Symfony\EventDispatcher\Listener\EventConsumingListener;

$listener = new EventConsumingListener(
    new VoidConsumer(),
    $messageFactory
);
```

Register the listener on Symfony Event Dispatcher for all required events

```
$eventDispatcher->addListener('type-1', $listener);
$eventDispatcher->addListener('type-2', $listener);

// will produce new Message('type-1', '{"x":"xxx"}') and pass to the consumer
$eventDispatcher->dispatch('type-1', new Event1('xxx'));
```

Running tests
-------------

[](#running-tests)

Install dependencies with composer

```
docker-compose run --rm composer
docker-compose run --rm spec
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

2

Last Release

3096d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a6cc50814a7b150c2a02d719d33c6e11040a6d0d010b2f986a24794edce7f52f?d=identicon)[dbojdo](/maintainers/dbojdo)

---

Top Contributors

[![dbojdo8x8](https://avatars.githubusercontent.com/u/76156710?v=4)](https://github.com/dbojdo8x8 "dbojdo8x8 (1 commits)")

---

Tags

symfonyevent dispatchermessage queuemessage bus

### Embed Badge

![Health badge](/badges/webit-message-bus-sf-event-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/webit-message-bus-sf-event-dispatcher/health.svg)](https://phpackages.com/packages/webit-message-bus-sf-event-dispatcher)
```

###  Alternatives

[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.6k38.2k](/packages/matomo-matomo)[phpro/soap-client

A general purpose SoapClient library

8895.9M52](/packages/phpro-soap-client)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751284.3k37](/packages/civicrm-civicrm-core)[pugx/autocompleter-bundle

Add an autocomplete type to forms

93867.3k3](/packages/pugx-autocompleter-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
