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

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

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

mahdi-mh/fallback-chain

最新稳定版本:1.1.1

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固