Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Domain Event Dispatcher Bundle Laravel Package

ashleydawson/domain-event-dispatcher-bundle

View on GitHub
Deep Wiki
Context7

Domain Event Dispatcher Bundle

Brings the domain event dispatcher (singleton) to Symfony projects. For full usage instructions, please see the full documentation shipped with the library.

Installation

Install the bundle via Composer:

$ composer require ashleydawson/domain-event-dispatcher-bundle

Then, register the bundle with the Symfony kernel app/AppKernel.php:

$bundles = [
    // ...
    new AshleyDawson\DomainEventDispatcherBundle\AshleyDawsonDomainEventDispatcherBundle(),
];

Configuration

Deferred events are configured to be dispatched from the Symfony kernel.terminate kernel event. To change this, add the following to your app/config/config.yml file:

ashley_dawson_domain_event_dispatcher:
    dispatch_deferred_events_from_kernel_event: kernel.terminate
    dispatch_deferred_events_from_kernel_event_priority: 0

Usage

Please refer to the full documentation for an in-depth look at how to use the domain event dispatcher. However, please find a simple example below:

Create an event:

<?php

namespace AppBundle\DomainEvent;

class MyDomainEvent
{
    private $myEntityId;
    
    public function __construct($myEntityId)
    {
        $this->myEntityId = $myEntityId;
    }
    
    public function getMyEntityId()
    {
        return $this->myEntityId;
    }
}

Create a listener:

<?php

namespace AppBundle\DomainEventListener;

use AppBundle\DomainEvent\MyDomainEvent;

class MyDomainEventListener
{
    public function __invoke(MyDomainEvent $event)
    {
        // Do something with the event...
    }
}

Add the listener to the event dispatcher via the Symfony Dependency Injection Container using the tag ashley_dawson.domain_event_listener:

# app/config/services.yml

services:
    app.my_domain_event_listener:
        class: AppBundle\DomainEventListener\MyDomainEventListener
        tags:
            - { name: ashley_dawson.domain_event_listener }

Dispatch an event from your model:

<?php

namespace AppBundle\Entity;

use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use AppBundle\DomainEvent\MyDomainEvent;

class MyEntity
{
    private $id;
    
    public function mySpecialCommand()
    {
        DomainEventDispatcher::getInstance()->dispatch(
            new MyDomainEvent($this->id)
        );
    }
}

Symfony Profiler

The map of events that have been deferred/dispatched during a request can be found in the Symfony Profiler. Simply click on the domain events icon and the profile screen containing the map will be displayed.

Toolbar Info:

Toolbar

Full Profiler Screen:

Profiler Screen

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle