Download the PHP package ttlove/laravel-transactional-events without Composer
On this page you can find all versions of the php package ttlove/laravel-transactional-events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ttlove/laravel-transactional-events
More information about ttlove/laravel-transactional-events
Files in ttlove/laravel-transactional-events
Package laravel-transactional-events
Short Description Transactional layer for Laravel Event Dispatcher
License MIT
Informations about the package laravel-transactional-events
Transaction-aware Event Dispatcher for Laravel
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.
Table of Contents
- Motivation
- Installation
- Laravel
- Lumen
- Usage
- Configuration
- F.A.Q.
- Known Issues
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.
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.5.x-5.7.x | 1.4.x |
5.8.x-7.x | 1.8.x |
Laravel
-
Install this package via
composer
: - Publish the provided
transactional-events.php
configuration file:
Lumen
-
Install this package via
composer
: -
Manually copy the provided
transactional-events.php
configuration file to theconfig
folder: - Register the configuration file and the service provider in
bootstrap/app.php
:
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; - 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:
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:
Configuration
The configuration file includes the following parameters:
Enable or disable the transaction-aware behavior:
By default, the transaction-aware behavior will be applied to all events under the App\Events
namespace.
Feel free to use patterns and namespaces.
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.
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.
Known issues
Transaction-aware events are not dispatched in tests.
This issue is fixed for Laravel 5.6.16+ (see #23832).
For previous versions, it is associated with the RefreshDatabase
or DatabaseTransactions
trait, namely when it uses database transactions to reset database after each test.
This package relies on events dispached when transactions begin/commit/rollback and as each test is executed within a transaction that is rolled back when test finishes, the dispatched application events are never actually dispatched. In order to get the expected behavior, use the Neves\Testing\RefreshDatabase
or Neves\Testing\DatabaseTransactions
trait in your tests instead of the ones originally provided by Laravel.
License
This package is open-sourced software licensed under the MIT license.
All versions of laravel-transactional-events with dependencies
illuminate/database Version ~5.8.0|^6.0|^7.0
illuminate/events Version ~5.8.0|^6.0|^7.0
illuminate/support Version ~5.8.0|^6.0|^7.0