定制 mahdi-mh/fallback-chain 二次开发

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

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

mahdi-mh/fallback-chain

Composer 安装命令:

composer require mahdi-mh/fallback-chain

包简介

A lightweight PHP package for building fallback chains with callable handlers and failure callbacks.

README 文档

README

Latest Version on Packagist Tests License

A lightweight PHP package for building fallback chains with callable handlers and failure callbacks.

Features

  • Fluent Interface: Build chains with a clean, readable syntax
  • Automatic Fallback: Automatically continues to the next stage when one fails
  • Failure Callbacks: Execute custom logic when a stage fails (logging, metrics, etc.)
  • Context Sharing: Share data between all stages via a context object
  • Exception Collection: Access all exceptions when the entire chain fails
  • PHP 7.1+ Compatible: Works with PHP 7.1 through PHP 8.x

Installation

Install the package via Composer:

composer require mahdi-mh/fallback-chain

Usage

Basic Example

use MahdiMh\FallbackChain\FallbackChain;
use MahdiMh\FallbackChain\Stage;
use MahdiMh\FallbackChain\AllStagesFailedException;

$context = [
    'to'   => '09121234567',
    'text' => 'Hello, this is a test message',
];

$chain = new FallbackChain($context);

$chain
    ->add(Stage::handler(function ($ctx) {
        return SmsProvider1::send($ctx['to'], $ctx['text']);
    })->onFailure(function (\Throwable $e, $ctx) {
        error_log('SMS Provider 1 failed: ' . $e->getMessage());
    }))

    ->add(Stage::handler(function ($ctx) {
        return SmsProvider2::send($ctx['to'], $ctx['text']);
    }))

    ->add(Stage::handler(function ($ctx) {
        return SmsProvider3::send($ctx['to'], $ctx['text']);
    })->onFailure(function (\Throwable $e, $ctx) {
        error_log('Provider 3 unavailable: ' . $e->getMessage());
    }));

try {
    $result = $chain->execute();
    echo "Message sent successfully: " . $result;
} catch (AllStagesFailedException $e) {
    echo "All providers failed.";

    // Access all exceptions that occurred
    foreach ($e->getExceptions() as $exception) {
        error_log($exception->getMessage());
    }
}

How It Works

  1. Create a chain with a shared context (any value - array, object, etc.)
  2. Add stages using the fluent add() method
  3. Execute the chain - it tries each stage in order:
    • If a stage succeeds (no exception), its result is returned immediately
    • If a stage fails (throws any Throwable), the optional onFailure callback runs
    • Execution continues to the next stage
  4. Handle total failure - if all stages fail, AllStagesFailedException is thrown

Use Cases

  • Multiple SMS/Email Providers: Try primary provider, fall back to secondary
  • Payment Gateways: Attempt multiple payment processors
  • API Endpoints: Try different API servers or mirrors
  • Cache Layers: Check memory cache, then Redis, then database
  • File Storage: Try local storage, then S3, then another cloud provider

Creating Stages

// Stage with just a handler
$stage = Stage::handler(function ($context) {
    return doSomething($context);
});

// Stage with handler and failure callback
$stage = Stage::handler(function ($context) {
    return doSomething($context);
})->onFailure(function (\Throwable $e, $context) {
    // Log, send metrics, cleanup, etc.
});

Accessing Failed Exceptions

When all stages fail, you can inspect all the exceptions:

try {
    $chain->execute();
} catch (AllStagesFailedException $e) {
    $exceptions = $e->getExceptions();

    foreach ($exceptions as $index => $exception) {
        echo "Stage {$index} failed: " . $exception->getMessage() . "\n";
    }
}

Testing

composer test

License

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

mahdi-mh/fallback-chain 适用场景与选型建议

mahdi-mh/fallback-chain 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 15, 最近一次更新时间为 2025 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 mahdi-mh/fallback-chain 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-17