jeromejhipolito/laravel-saga
Composer 安装命令:
composer require jeromejhipolito/laravel-saga
包简介
Laravel package for Saga transaction pattern - manage distributed transactions with automatic rollback support
README 文档
README
Laravel package for implementing the Saga transaction pattern - manage distributed transactions with automatic rollback support.
Features
- Step-by-step execution - Execute multiple steps in sequence with automatic tracking
- Automatic rollback - When a step fails, previously executed steps are automatically rolled back in reverse order
- Database persistence - Transaction and step states are persisted for debugging and recovery
- Status tracking - Detailed status enums for both transactions and steps
- Easy integration - Simple trait for Jobs and Consumers
Installation
composer require jeromejhipolito/laravel-saga
Publish and run the migrations:
php artisan vendor:publish --tag="saga-migrations"
php artisan migrate
Usage
Creating a Saga Step
Create a step by extending AbstractSagaStep:
<?php namespace App\Saga\Steps; use JeromeJHipolito\Saga\Steps\AbstractSagaStep; class CreateOrderStep extends AbstractSagaStep { public function execute(array $data): mixed { // Create the order $order = Order::create([ 'user_id' => $data['user_id'], 'total' => $data['total'], ]); return ['order_id' => $order->id]; } public function rollback(mixed $result, array $data): void { // Rollback: Delete the created order if (isset($result['order_id'])) { Order::find($result['order_id'])?->delete(); } } }
Using the UsesSaga Trait in Jobs
<?php namespace App\Jobs; use App\Saga\Steps\CreateOrderStep; use App\Saga\Steps\ReserveInventoryStep; use App\Saga\Steps\ProcessPaymentStep; use JeromeJHipolito\Saga\Traits\UsesSaga; class ProcessOrderJob implements ShouldQueue { use UsesSaga; public function handle(): void { $data = [ 'user_id' => $this->userId, 'items' => $this->items, 'total' => $this->total, ]; // Initialize the saga $saga = $this->initializeSaga($data); // Add steps $saga->addStep(new CreateOrderStep()) ->addStep(new ReserveInventoryStep()) ->addStep(new ProcessPaymentStep()); // Execute - if any step fails, previous steps are rolled back $this->executeSaga($data); } }
Direct Usage with SagaTransactionManager
<?php use JeromeJHipolito\Saga\Services\SagaTransactionManager; $saga = SagaTransactionManager::createTransaction( transactionId: 'order-'.Str::uuid(), jobClass: 'ProcessOrderJob', payload: $data ); $saga->addStep(new CreateOrderStep()) ->addStep(new ReserveInventoryStep()) ->addStep(new ProcessPaymentStep()); try { $result = $saga->execute($data); // Transaction completed successfully } catch (Exception $e) { // Transaction failed, steps were rolled back $saga->rollback(); // Manual rollback for additional cleanup }
Checking Transaction Status
// Find an existing transaction $saga = SagaTransactionManager::findByTransactionId('order-123'); $saga->isCompleted(); // true if all steps succeeded $saga->hasFailed(); // true if any step failed
Transaction and Step Statuses
Transaction Statuses:
PENDING- Transaction created, not yet startedRUNNING- Transaction is executing stepsCOMPLETED- All steps executed successfullyFAILED- A step failed during executionROLLING_BACK- Rollback is in progressROLLED_BACK- Rollback completed
Step Statuses:
PENDING- Step not yet executedEXECUTED- Step completed successfullyFAILED- Step execution failedROLLED_BACK- Step was rolled backCANCELLED- Step was never executed (skipped due to earlier failure)
Database Schema
The package creates two tables:
saga_transactions
id- Primary keyuuid- Unique identifiertransaction_id- Your custom transaction IDjob_class- The class that initiated the sagapayload- JSON payload datastatus- Current transaction statusstarted_at,completed_at,failed_at- Timestampsrollback_started_at,rollback_completed_at- Rollback timestampserror_message- Error details if failed
saga_steps
id- Primary keyuuid- Unique identifiersaga_transaction_id- Foreign key to transactionstep_name- Name of the stepstep_class- Full class name of the stepstep_order- Execution orderstatus- Current step statusexecuted_at,rollback_at- Timestampsresult_data- JSON result from executionerror_message- Error details if failed
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
jeromejhipolito/laravel-saga 适用场景与选型建议
jeromejhipolito/laravel-saga 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「transaction」 「distributed」 「rollback」 「orchestration」 「saga」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeromejhipolito/laravel-saga 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeromejhipolito/laravel-saga 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeromejhipolito/laravel-saga 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DUKPT implementation in PHP
The BlockTrail PHP SDK, for integration of Bitcoin functionality through the BlockTrail API
Send mails after DB transaction is committed
This is a transaction.
Yii 2 Models load with relation, & transaction save with relation
A Symfony to process jobs from AbcJobServerBundle
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-20