All Projects → fntneves → Laravel Transactional Events

fntneves / Laravel Transactional Events

Licence: mit
Transaction-aware Event Dispatcher for Laravel

Projects that are alternatives of or similar to Laravel Transactional Events

Laravel Multilingual Routes
A package to handle multilingual routes in your Laravel application.
Stars: ✭ 241 (-8.37%)
Mutual labels:  laravel, package
Cms
Decoupled CMS for any Laravel app, gain control of: pages, blogs, galleries, events, images, custom modules and more.
Stars: ✭ 498 (+89.35%)
Mutual labels:  events, laravel
Laravel Gitscrum
GitScrum is a Project Management Tool, developed to help entrepreneurs, freelancers, managers, and teams Skyrocket their Productivity with the Agile methodology and Gamification.
Stars: ✭ 2,686 (+921.29%)
Mutual labels:  laravel, package
Laravelpackage.com
Documentation for LaravelPackage.com: Learn to create Laravel specific PHP packages from scratch, following this open documentation.
Stars: ✭ 214 (-18.63%)
Mutual labels:  laravel, package
Laravel Event Broadcast
Laravel event broadcasting with Node.js, Redis & Socket.io
Stars: ✭ 5 (-98.1%)
Mutual labels:  events, laravel
Laravel Package Tools
Tools for creating Laravel packages
Stars: ✭ 216 (-17.87%)
Mutual labels:  laravel, package
Laravel Relationship Events
Missing relationship events for Laravel
Stars: ✭ 383 (+45.63%)
Mutual labels:  events, laravel
Skeleton Nova Tool
A skeleton repository for Spatie's Nova Packages
Stars: ✭ 191 (-27.38%)
Mutual labels:  laravel, package
Laravel Google Calendar
Manage events on a Google Calendar
Stars: ✭ 787 (+199.24%)
Mutual labels:  events, laravel
Laravel Event Projector
Event sourcing for Artisans 📽
Stars: ✭ 650 (+147.15%)
Mutual labels:  events, laravel
Laravel Multisite
Multiple sites on one codebase
Stars: ✭ 214 (-18.63%)
Mutual labels:  laravel, package
Noel
A universal, human-centric, replayable javascript event emitter.
Stars: ✭ 158 (-39.92%)
Mutual labels:  events, dispatcher
Laravel Custom Casts
Make your own custom cast type for Laravel model attributes
Stars: ✭ 213 (-19.01%)
Mutual labels:  laravel, package
Laravelmetatags
The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
Stars: ✭ 226 (-14.07%)
Mutual labels:  laravel, package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (-24.71%)
Mutual labels:  laravel, package
Attendize
Attendize is an open-source ticket selling and event management platform built on Laravel.
Stars: ✭ 3,285 (+1149.05%)
Mutual labels:  events, laravel
Laravel Feature
A package to manage feature flagging in a Laravel project.
Stars: ✭ 184 (-30.04%)
Mutual labels:  laravel, package
Nebula
Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.
Stars: ✭ 190 (-27.76%)
Mutual labels:  laravel, package
Kledex
.NET Standard framework to create simple and clean design. Advanced features for DDD, CQRS and Event Sourcing.
Stars: ✭ 502 (+90.87%)
Mutual labels:  events, dispatcher
Rabbitevents
Nuwber's events provide a simple observer implementation, allowing you to listen for various events that occur in your current and another application. For example, if you need to react to some event published from another API.
Stars: ✭ 84 (-68.06%)
Mutual labels:  events, laravel

Transaction-aware Event Dispatcher for Laravel

Latest Stable Version TravisCI Status Scrutinizer Code Quality Total Downloads

This Laravel package introduces Transaction-aware Event Dispatcher.
It ensures the events dispatched within a database transaction are dispatched only if the outer transaction successfully commits. Otherwise, the events are discarded and never dispatched.

Note: Laravel 8.17 introduced a new method DB::afterCommit that allows one to achieve the same of this package. Yet, it lacks transaction-aware behavior support for Eloquent events.

Table of Contents

Motivation

Consider the following example of ordering tickets that involves changes to the database.
The orderTickets dispatches the custom OrderCreated event. In turn, its listener sends an email to the user with the order details.

DB::transaction(function() {
    ...
    $order = $concert->orderTickets($user, 3); // internally dispatches 'OrderCreated' event
    PaymentService::registerOrder($order);
});

In the case of transaction failure, due to an exception in the orderTickets method or even a deadlock, the database changes are completely discarded.

Unfortunately, this is not true for the already dispatched OrderCreated event. This results in sending the order confirmation email to the user, even after the order failure.

The purpose of this package is thus to hold events dispatched within a database transaction until it successfully commits. In the above example the OrderCreated event would never be dispatched in the case of transaction failure.

Installation

Laravel Package
5.8.x-7.x 1.8.x
8.x 2.x

Laravel

  • Install this package via composer:
composer require fntneves/laravel-transactional-events
  • Publish the provided transactional-events.php configuration file:
php artisan vendor:publish --provider="Neves\Events\EventServiceProvider"

Lumen

  • Install this package via composer:
composer require fntneves/laravel-transactional-events
  • Manually copy the provided transactional-events.php configuration file to the config folder:
cp vendor/fntneves/laravel-transactional-events/src/config/transactional-events.php config/transactional-events.php
  • Register the configuration file and the service provider in bootstrap/app.php:
// Ensure the original EventServiceProvider is registered first, otherwise your event listeners are overriden.
$app->register(App\Providers\EventServiceProvider::class);

$app->configure('transactional-events');
$app->register(Neves\Events\EventServiceProvider::class);

Usage

The transaction-aware layer is enabled out of the box for the events under the App\Events namespace.

This package offers three distinct ways to dispatch transaction-aware events:

  • Implement the Neves\Events\Contracts\TransactionalEvent contract;
  • Use the generic TransactionalClosureEvent event;
  • Use the Neves\Events\transactional helper;
  • Change the configuration file.

Use the contract, Luke:

The simplest way to mark events as transaction-aware events is implementing the Neves\Events\Contracts\TransactionalEvent contract:

namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
...
use Neves\Events\Contracts\TransactionalEvent;

class TicketsOrdered implements TransactionalEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    ...
}

And that's it. There are no further changes required.

What about Jobs?

This package provides a generic TransactionalClosureEvent event for bringing the transaction-aware behavior to custom behavior without requiring specific events.

One relevant use case is to ensure that Jobs are dispatched only after the transaction successfully commits:

DB::transaction(function () {
    ...
    Event::dispatch(new TransactionalClosureEvent(function () {
        // Job will be dispatched only if the transaction commits.
        ProcessOrderShippingJob::dispatch($order);
    });
    ...
});

And that's it. There are no further changes required.

Configuration

The configuration file includes the following parameters:

Enable or disable the transaction-aware behavior:

'enable' => true

By default, the transaction-aware behavior will be applied to all events under the App\Events namespace.
Feel free to use patterns and namespaces.

'transactional' => [
    'App\Events'
]

Choose the events that should always bypass the transaction-aware layer, i.e., should be handled by the original event dispatcher. By default, all *ed Eloquent events are excluded. The main reason for this default value is to avoid interference with your already existing event listeners for Eloquent events.

'excluded' => [
    // 'eloquent.*',
    'eloquent.booted',
    'eloquent.retrieved',
    'eloquent.saved',
    'eloquent.updated',
    'eloquent.created',
    'eloquent.deleted',
    'eloquent.restored',
],

Frequently Asked Questions

Can I use it for Jobs?

Yes. As mentioned in Usage, you can use the generic TransactionalClosureEvent(Closure $callable) event to trigger jobs only after the transaction commits.

License

This package is open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].