2017 © Pedro Peláez
 

library laravel-doctrine-entity-events

Easily hook in and fire native laravel events with laravel-doctrine

image

randomstate/laravel-doctrine-entity-events

Easily hook in and fire native laravel events with laravel-doctrine

  • Friday, March 2, 2018
  • by CImrie
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Laravel-Doctrine Entity Events

This package provides a simple way to hook into Doctrine2 Entity events and remap them to native Laravel events., (*1)

Getting Started

  • Install using composer require randomstate/laravel-doctrine-entity-events
  • Add RandomState\LaravelDoctrineEntityEvents\LaravelDoctrineEntityEventsServiceProvider::class to the providers section of config/app.php

Usage

Configuration

Helper Method (recommended), (*2)

<?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, (*3)

<?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., (*4)

<?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., (*5)

<?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); 
        });

The Versions

02/03 2018

dev-master

9999999-dev

Easily hook in and fire native laravel events with laravel-doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

02/03 2018

v0.2.0

0.2.0.0

Easily hook in and fire native laravel events with laravel-doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

02/03 2018

v0.1.0

0.1.0.0

Easily hook in and fire native laravel events with laravel-doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

02/03 2018

dev-develop

dev-develop

Easily hook in and fire native laravel events with laravel-doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires