shiftonelabs/laravel-db-events

Add extra database events.

1.0.0 2016-11-01 04:40 UTC

This package is auto-updated.

Last update: 2024-11-13 08:05:24 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This Laravel/Lumen package provides additional events for the Illuminate Database package. Currently, this package adds:

  • a DatabaseConnecting event that is fired before connecting to the database, which can modify the configuration or cancel the connection
  • a DatabaseConnected event that is fired after connecting to the database
  • a ConnectingException runtime exception that is thrown if the database connection is cancelled by the DatabaseConnecting event

Additional events may be added be added as requested/submitted.

Install

Via Composer

$ composer require shiftonelabs/laravel-db-events

Once composer has been updated and the package has been installed, the service provider will need to be loaded.

For Laravel 4, open app/config/app.php and add following line to the providers array:

'ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider',

For Laravel 5, open config/app.php and add following line to the providers array:

ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class,

For Lumen 5, open bootstrap/app.php and add following line under the "Register Service Providers" section:

$app->register(ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class);

Usage

DatabaseConnecting Event

The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting event allows you to hook into the database connection lifecycle before the connection is established. Additionally, this event provides you the ability to modify the configuration used for the connection, as well as completely cancel the connection attempt.

Attributes

The DatabaseConnecting event provides three public attributes:

Example:

app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
    app('log')->info('Connector class: '.get_class($event->connector));
    app('log')->info('Connection name: '.$event->connectionName);
    app('log')->info('Configuration: '.print_r($event->config, true));
});

Modifying Connection Configuration

The configuration for your database connections is usually stored in your config/database.php file (in conjunction with your .env file). If, however, you need to dynamically modify the configuration used for the connection, this can be done inside a DatabaseConnecting event listener. Any changes made to the configuration in the event listener will be used for the database connection.

Example:

app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
    // don't connect to mysql in strict mode if you like zeroed out dates
    if (i_like_zero_dates()) {
        $event->config['strict'] = false;
    }
});

Cancelling the Connection

There may be situations where you would like to prevent the database from attempting the connection. In this case, the database connection attempt can be cancelled by returning false from a DatabaseConnecting event listener. If the database connection is cancelled, a ShiftOneLabs\LaravelDbEvents\Exceptions\ConnectingException runtime exception will be thrown.

Example:

app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
    if (not_todaaay()) {
        return false;
    }
});

DatabaseConnected Event

The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected event allows you to hook into the database connection lifecycle after the connection is established. Additionally, this event provides access to the final configuration used for the connection, as well as the connection itself.

Attributes

The DatabaseConnected event provides four public attributes:

Example:

app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected', function ($event) {
    app('log')->info('Connector class: '.get_class($event->connector));
    app('log')->info('Connection name: '.$event->connectionName);
    app('log')->info('Configuration: '.print_r($event->config, true));
    app('log')->info('PDO class: '.get_class($event->pdo));
});

Contributing

Contributions are very welcome. Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email patrick@shiftonelabs.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.