waska14/laravel-with-db-transactions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

waska14/laravel-with-db-transactions

Composer 安装命令:

composer require waska14/laravel-with-db-transactions

包简介

Use middleware globally for all routes to put every controller in database transaction

README 文档

README

Are you tired of writing database transactions for every controller method?

This package solves the problem with middleware.

Middleware begins database transaction and depending response (and config also) commits/rollbacks it.

Docs

Installation

Add the package in your composer.json by executing the command.

composer require waska14/laravel-with-db-transactions

For Laravel versions before 5.5 or if not using auto-discovery, register the service provider in config/app.php

'providers' => [
    /*
     * Package Service Providers...
     */
    \Waska\LaravelWithDBTransactions\WithDBTransactionsServiceProvider::class,
],

Configuration

If you want to change default configuration, you must publish default configuration file to your project by running this command in console:

php artisan vendor:publish --tag=waska-with-db-transactions-config

This command will copy file [/vendor/waska14/laravel-with-db-transactions/config/waska.with_db_transactions.php] to [/config/waska.with_db_transactions.php]

Default waska.with_db_transactions.php looks like:

return [
    /*
     * Route names that will be processed without transaction by WithDBTransactions middleware
     */
    'ignore_route_names' => [
        // login
    ],

    /*
     * Request methods that will be processed without transaction by WithDBTransactions middleware
     */
    'ignore_request_methods' => [
        'get',
    ],

    /*
     * Maximum attempts for transaction by default.
     * Note: You can ignore route name, than use middleware with_db_transactions:N where N is the number of attempts.
     */
    'maximum_attempts' => 1,

    /*
     * If response's http status code is any of those, the transaction will be committed.
     * Note: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
     */
    'commit_http_statuses' => [
        200, // OK
        201, // Created
        202, // Accepted
        203, // Non-Authoritative Information
        204, // No Content
        205, // Reset Content
        206, // Partial Content
        207, // Multi-Status (WebDAV)
        208, // Already Reported (WebDAV)
        226, // IM Used

        300, // Multiple Choices
        301, // Moved Permanently
        302, // Found (Previously "Moved temporarily")
        303, // See Other (since HTTP/1.1)
        304, // Not Modified (RFC 7232)
        305, // Use Proxy (since HTTP/1.1)
        306, // Switch Proxy
        307, // Temporary Redirect (since HTTP/1.1)
        308, // Permanent Redirect (RFC 7538)
    ],

    /*
     * Middleware default class.
     * Anytime you can extend/override the class and change namespace of middleware_class
     */
    'middleware_class' => \Waska\LaravelWithDBTransactions\Http\Middleware\WithDBTransactions::class,

    /*
     * Default alias for middleware
     */
    'middleware_alias' => 'with_db_transactions',

    /*
     * List of middleware groups, where will be pushed with_db_transactions middleware by default (from ServiceProvider)
     */
    'middleware_groups' => [
//        'api',
//        'web',
    ],

    /*
     * Maximum attempts for middleware_groups
     * Note: if value is null, default maximum_attempts will be used.
     */
    'maximum_attempts_for_groups' => null,

    /*
     * Before and after action events
     */
    'before_begin_transaction_event' => \Waska\LaravelWithDBTransactions\Events\BeforeBeginTransactionEvent::class,
    'after_begin_transaction_event' => \Waska\LaravelWithDBTransactions\Events\AfterBeginTransactionEvent::class,
    'before_commit_event' => \Waska\LaravelWithDBTransactions\Events\BeforeCommitEvent::class,
    'after_commit_event' => \Waska\LaravelWithDBTransactions\Events\AfterCommitEvent::class,
    'before_rollback_event' => \Waska\LaravelWithDBTransactions\Events\BeforeRollbackEvent::class,
    'after_rollback_event' => \Waska\LaravelWithDBTransactions\Events\AfterRollbackEvent::class,
    'before_every_rollback_event' => \Waska\LaravelWithDBTransactions\Events\BeforeEveryRollbackEvent::class,
    'after_every_rollback_event' => \Waska\LaravelWithDBTransactions\Events\AfterEveryRollbackEvent::class,
];

You can manage route middleware globally (automatically) from config:

Key Value(s) Comment
middleware_groups Keys of protected $middlewareGroups from app/Http/Kernel.php. Every route having the group middleware will be processed with database transaction.
ignore_request_methods HTTP request methods names (GET/HEAD) Those methods won'be processed with database transaction.
ignore_route_names route names Those methods also won't be processed with database transaction.
commit_http_statuses HTTP status codes If transaction has begun and the response has any of those statuses, the transaction will be committed.
maximum_attempts integer Maybe if deadlocks occurs or something else

If you want manually management, you can use middleware_alias (or change it, also) as route middleware.

Manual usage

If you define middleware_groups as an empty array in config, none of the routes will be processed with database transactions by default, until you set manually the middleware for the specific routes/route groups.

Note that you can fill middleware_groups and also use middleware manually anywhere you want the method to be processed with database transaction.

Default middleware alias is with_db_transactions which can be changed in config, also.

Note that all configuration (ignore_route_names, ignore_request_methods, maximum_attempts, commit_http_statuses etc.) will also work when using this package manually.

Actions and Events

After version 2.0.0 you can define closures that will be executed before/after commit/rollback(s).

There are 6 static functions that you can call with Waska\LaravelWithDBTransactions\Helpers\WithDBTransactions class:

WithDBTransactions::beforeCommit(callable $closure); // Before committing database transaction
WithDBTransactions::afterCommit(callable $closure); // After committing database transaction
WithDBTransactions::beforeRollback(callable $closure); // Before last rollback
WithDBTransactions::afterRollback(callable $closure); // After last rollback
WithDBTransactions::beforeEveryRollback(callable $closure); // Before every (except last) rollback
WithDBTransactions::afterEveryRollback(callable $closure); // After every (except last) rollback

Usage:

WithDBTransactions::beforeCommit(function () {
    \Log::info('First log and then commit!');
});
WithDBTransactions::afterCommit(function () {
    \Log::info('First commit and then log!');
});

Note that each function can be called as many times as you want. All closures will be executed with the same sequence. E.x:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Waska\LaravelWithDBTransactions\Helpers\WithDBTransactions;

class Controller extends BaseController
{
    public function create(Request $request)
    {
        // your stuff
        WithDBTransactions::beforeRollback(function () {
            \Log::info('log1 before rollback!');
        });
        WithDBTransactions::afterRollback(function () {
            \Log::info('log1 after rollback!');
        });
        WithDBTransactions::afterRollback(function () {
            \Log::info('log2 after rollback!');
        });
        // your stuff
        \App\User::create($request->validated());

        // Some error happened, so middleware rollbacks the transaction
        throw new \Exception('Something went wrong');
    }
}

After rollback storage/logs/laravel.log looks like:

[Y-m-d H:i:s] local.INFO: log1 before rollback
[Y-m-d H:i:s] local.INFO: log1 after rollback
[Y-m-d H:i:s] local.INFO: log2 after rollback

After version 2.0.0 package dispatches events also.

You can listen them and do whatever you want in listener.

List of events:

  • \Waska\LaravelWithDBTransactions\Events\BeforeBeginTransactionEvent Before begin transaction
  • \Waska\LaravelWithDBTransactions\Events\AfterBeginTransactionEvent After begin transaction
  • \Waska\LaravelWithDBTransactions\Events\BeforeCommitEvent Before commit
  • \Waska\LaravelWithDBTransactions\Events\AfterCommitEvent After commit
  • \Waska\LaravelWithDBTransactions\Events\BeforeRollbackEvent Before latest rollback
  • \Waska\LaravelWithDBTransactions\Events\AfterRollbackEvent After latest rollback
  • \Waska\LaravelWithDBTransactions\Events\BeforeEveryRollbackEvent Before every rollback
  • \Waska\LaravelWithDBTransactions\Events\AfterEveryRollbackEvent After every rollback

waska14/laravel-with-db-transactions 适用场景与选型建议

waska14/laravel-with-db-transactions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60.48k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「laravel」 「transactions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 waska14/laravel-with-db-transactions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 waska14/laravel-with-db-transactions 我们能提供哪些服务?
定制开发 / 二次开发

基于 waska14/laravel-with-db-transactions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-15