randomstate / laravel-doctrine-entity-events
Easily hook in and fire native laravel events with laravel-doctrine
v0.2.3
2023-05-17 12:44 UTC
Requires
- laravel-doctrine/orm: ~1.3
Requires (Dev)
- laravel/laravel: 6.0.*
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-10-17 15:49:31 UTC
README
This package provides a simple way to hook into Doctrine2 Entity events and remap them to native Laravel events.
Getting Started
- Install using
composer require randomstate/laravel-doctrine-entity-events
- Add
RandomState\LaravelDoctrineEntityEvents\LaravelDoctrineEntityEventsServiceProvider::class
to theproviders
section ofconfig/app.php
Usage
Configuration
Helper Method (recommended)
<?php use RandomState\LaravelDoctrineEntityEvents\EventRedirector; public class AppServiceProvider extends ServiceProvider { public function register() { EventRedirector::register(function(EventRedirector $redirector) { $redirector->redirect(MyEntity::class) ->postPersist(MyEntityWasCreated::class) ->postUpdate(MyEntityWasUpdated::class) ->postFlush() // falls back to default as no destination is provided ->default(SomethingHappenedToMyEntityEvent::class); }); } }
Intercept Service Instantiation
<?php use RandomState\LaravelDoctrineEntityEvents\EventRedirector; public class AppServiceProvider extends ServiceProvider { public function register() { $this->app->resolving(EventRedirector::class, (function(EventRedirector $redirector) { $redirector->redirect(MyEntity::class) ->postPersist(MyEntityWasCreated::class) ->postUpdate(MyEntityWasUpdated::class) ->postFlush() // falls back to default as no destination is provided ->default(SomethingHappenedToMyEntityEvent::class); })); } }
Events
Every Laravel Event you specify as a destination will be supplied the entity and doctrine event arguments on creation. The entity is supplied as the first argument so you can conveniently ignore other event arguments when you are only interested in the entity itself.
<?php class MyEntityWasCreated { public function __construct(MyEntity $entity, LifecycleEventArgs $eventArgs) { // do something } public function handle() { // do something } }
Advanced Usage
If you need to customise the way the event is instantiated, supply a closure as your 'destination' when defining the redirects.
<?php EventRedirector::register(function(EventRedirector $redirector) { $redirector->redirect(MyEntity::class) ->postPersist(MyEntityWasCreated::class) ->postUpdate(function(MyEntity $entity) { $mailer = app('mailer'); event(new MyEntityWasUpdated($entity, $mailer)); // customised instantiation }) ->postFlush() // falls back to default as no destination is provided ->default(SomethingHappenedToMyEntityEvent::class); });