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

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

php-ddd/domain-event
====================

Library to manage domain and event

v1.0.1(10y ago)434.6k2[3 issues](https://github.com/php-ddd/domain-event/issues)3MITPHPPHP &gt;=5.3.3

Since Dec 31Pushed 9y ago1 watchersCompare

[ Source](https://github.com/php-ddd/domain-event)[ Packagist](https://packagist.org/packages/php-ddd/domain-event)[ RSS](/packages/php-ddd-domain-event/feed)WikiDiscussions master Synced yesterday

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

Domain [![Build Status](https://camo.githubusercontent.com/20980a6b236d419426cb2735c8b239243cafed88c5ee167e5ce821bb5c583c49/68747470733a2f2f7472617669732d63692e6f72672f7068702d6464642f646f6d61696e2d6576656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/php-ddd/domain-event) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/b201bb1226587fb9227e0bb3a9f969d409b921fcea1368f33c58b17fd583cba2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7068702d6464642f646f6d61696e2d6576656e742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/php-ddd/domain-event/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/f0aef4ffc3d8f44ef23584379898145022ef079b4b894cda3a265aecfe1ed940/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7068702d6464642f646f6d61696e2d6576656e742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/php-ddd/domain-event/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/2e7d0363258c2607d7f3e2fd91d60dbfc3e23986ec7e0ee5d54c5f0fe0d3ae5d/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f61336234663664392d343439652d346663612d393761642d3731306632636134313634322f6d696e692e706e67)](https://insight.sensiolabs.com/projects/a3b4f6d9-449e-4fca-97ad-710f2ca41642)
==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#domain----)

This library allows you to concentrate to what matters most in your application: the domain.
According to Eric Evan's book Domain-Driven Design, your domain should be composed of aggregate.
Each aggregate is composed by an AggregateRoot and possibly some ObjectValue.

This library focus on the AggregateRoot and provides helper to manage Event happening to your aggregate.

Usage
-----

[](#usage)

An example will better explain the purpose of this library:

```
use PhpDDD\Domain\AbstractAggregateRoot;

class PurchaseOrder extends AbstractAggregateRoot
{
    /**
     * @var mixed
     */
    private $customerId;

    /**
     * Try to create a new PurchaseOrder for a client.
     */
    public function __construct(Customer $customer)
    {
        // Tests that some business rules are followed.
        if (false === $customer->isActive()) {
            throw new MyDomainException('The customer need to be active.');
        }

        // ...
        // Since every rules are validated, we can simulate the creation of the Event associated
        // This allows us to have less redundant code

        $this->apply(new PurchaseOrderCreated($customer, new DateTime()));

        // This is equivalent as
        // $this->customerId = $customer->getId();
        // $this->events[] = new PurchaseOrderCreated($customer, new DateTime());
        // but we are not allowing to add event directly.
    }

    /**
     * The apply() method will automatically call this method.
     * Since it's an event you should never do some tests in this method.
     * Try to think that an Event is something that happened in the past.
     * You can not modify what happened. The only thing that you can do is create another event to compensate.
     */
    protected function applyPurchaseOrderCreated(PurchaseOrderCreated $event)
    {
        $this->customerId = $event->getCustomer()->getId();
    }
}
```

What's good with this approach is that you can then listen to every events produced by your AggregateRoot and do some additional stuff that is not really relevant to your business domain like sending an email.

```
use PhpDDD\Domain\Event\Listener\AbstractEventListener;

class SendEmailOnPurchaseOrderCreated extends AbstractEventListener
{
    private $mailer;

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

    /**
     * {@inheritDoc}
     */
    public function getSupportedEventClassName()
    {
        return 'PurchaseOrderCreated'; // Should be the fully qualified class name of the event
    }

    /**
     * {@inheritDoc}
     */
    public function handle(EventInterface $event)
    {
        $this->mailer->send('to@you.com', 'Purchase order created for customer #' . $event->getCustomer()->getId());
    }
}
```

For your project to know that an EventListener is bound to the Event, you should use the EventBus:

```
// first the locator
$locator = new \PhpDDD\Domain\Event\Listener\Locator\EventListenerLocator();
$locator->register('PurchaseOrderCreated', new SendEmailOnPurchaseOrderCreated(/* $mailer */));

// then the EventBus
$bus = new \PhpDDD\Domain\Event\Bus\EventBus($locator);

// do what you need to do on your Domain
$aggregateRoot = new PurchaseOrder(new Customer(1));

// Then apply EventListeners
$events = $aggregateRoot->pullEvents(); // this will clear the list of event in your AggregateRoot so an Event is trigger only once

// You can have more than one event at a time.
foreach($events as $event) {
    $bus->publish($event);
}
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity60

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

Total

2

Last Release

3982d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5cc2f661073d68100db53e02e0892a69076f0b44bc0ced487b043c6aff28ad0c?d=identicon)[JulienDufresne](/maintainers/JulienDufresne)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[lizhichao/one-sm

国密sm3

1891.5M8](/packages/lizhichao-one-sm)[protonemedia/laravel-task-runner

Write Shell scripts like Blade Components and run them locally or on a remote server

1335.5k1](/packages/protonemedia-laravel-task-runner)

PHPackages © 2026

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