PHPackages                             spriebsch/domain-event - 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. spriebsch/domain-event

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

spriebsch/domain-event
======================

Domain Events, Done Right.

2.1.0(3w ago)1722BSD-3-ClausePHPPHP &gt;=8.5

Since Oct 1Pushed 3w agoCompare

[ Source](https://github.com/spriebsch/domain-event)[ Packagist](https://packagist.org/packages/spriebsch/domain-event)[ RSS](/packages/spriebsch-domain-event/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (8)Versions (26)Used By (2)

DomainEvent
===========

[](#domainevent)

Domain Events, Done Right.

Features
--------

[](#features)

- **Structured Topics**: Define topics using a `vendor.domain.context.name` format.
- **Event Enveloping**: Wrap domain events in an `Envelope` that carries essential metadata:
    - `EventId` (UUIDv4) to uniquely identify the event
    - `Timestamp` (Received and Persisted)
    - `Topic`
    - optional `CausationId` and `CorrelationId` for tracing
    - an optional `SchemaVersion` to support event schema versioning
- **Serialization**: Built-in JSON serialization and deserialization (powered by `Crell/Serde`).
- **Event Sourcing Support**: Traits to easily implement event recording and applying in your code.
- **Attribute-based Configuration**: Use PHP 8 attributes to map events to topics and identify correlation IDs.
- **Topic Mapping**: Tools to generate and use a mapping between topics and PHP classes.

Getting Started
---------------

[](#getting-started)

### 1. Define a Domain Event

[](#1-define-a-domain-event)

Implement the `DomainEvent` interface and use the `MapToTopic` attribute.

```
use spriebsch\DomainEvent\DomainEvent;
use spriebsch\DomainEvent\MapToTopic;

#[MapToTopic('my-vendor.my-domain.my-context.something_happened')]
final readonly class SomethingHappened implements DomainEvent
{
    public function __construct(
        public string $data
    ) {}
}
```

### 2. Wrap the Event in an Envelope

[](#2-wrap-the-event-in-an-envelope)

The `Envelope` provides the metadata needed for handling the event.

```
use spriebsch\DomainEvent\Envelope;

$event = new SomethingHappened('some data');
$envelope = Envelope::from($event);

echo $envelope->topic()->asString(); // my-vendor.my-domain.my-context.something_happened
echo $envelope->eventId()->asString(); // (a random UUIDv4)
```

### 3. Serialization

[](#3-serialization)

```
use spriebsch\DomainEvent\JsonDomainEventSerializer;

$serializer = new JsonDomainEventSerializer();
$json = $serializer->serialize($event);
```

### 4. Event Sourcing

[](#4-event-sourcing)

Use the provided traits in your code.

```
use spriebsch\DomainEvent\CanRecordDomainEventsTrait;
use spriebsch\DomainEvent\CanApplyDomainEventsTrait;

final class MyDecision
{
    use CanRecordDomainEventsTrait;
    use CanApplyDomainEventsTrait;

    public function doSomething(string $data): void
    {
        $this->record(new SomethingHappened($data));
    }

    private function applySomethingHappened(SomethingHappened $event): void
    {
        // Update object's state
    }
}
```

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

[](#installation)

```
composer require spriebsch/domain-event
```

Development
-----------

[](#development)

You can use the `php-devbox` container () to run tests and static analysis.

### Run Tests

[](#run-tests)

```
php-devbox phpunit
```

### Static Analysis

[](#static-analysis)

```
php-devbox phpstan
```

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance95

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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 ~12 days

Recently: every ~3 days

Total

25

Last Release

21d ago

Major Versions

0.9.1 → 1.0.02025-10-17

1.8.0 → 2.0.02026-07-14

PHP version history (2 changes)0.1.0PHP &gt;=8.4

1.5.0PHP &gt;=8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/2064fee9073bee511de4bec7616a42c24e79c2dd8312f0363b5b3850f00b83ab?d=identicon)[spriebsch](/maintainers/spriebsch)

---

Top Contributors

[![spriebsch](https://avatars.githubusercontent.com/u/61342?v=4)](https://github.com/spriebsch "spriebsch (55 commits)")

### Embed Badge

![Health badge](/badges/spriebsch-domain-event/health.svg)

```
[![Health](https://phpackages.com/badges/spriebsch-domain-event/health.svg)](https://phpackages.com/packages/spriebsch-domain-event)
```

###  Alternatives

[10up/wp-content-connect

WordPress library that enables direct relationships for posts to posts and posts to users.

9617.3k](/packages/10up-wp-content-connect)

PHPackages © 2026

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