定制 shiftonelabs/laravel-db-events 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shiftonelabs/laravel-db-events

最新稳定版本:1.0.0

Composer 安装命令:

composer require shiftonelabs/laravel-db-events

包简介

Add extra database events.

README 文档

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:

Attribute Description
public $connector The Connector object making the connection. This package extends each of the built-in connectors.
public $connectionName The name of the selected database connection configuration.
public $config The configuration array used to connect to the database.

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:

Attribute Description
public $connector The Connector object making the connection. This package extends each of the built-in connectors.
public $connectionName The name of the selected database connection configuration.
public $config The configuration array that was used to connect to the database.
public $pdo The connected PDO (or potentially Doctrine\DBAL\Driver\PDOConnection, as of 5.3) object.

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.

统计信息

  • 总下载量: 8.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-01