Download the PHP package mahdi-mh/fallback-chain without Composer
On this page you can find all versions of the php package mahdi-mh/fallback-chain. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Please rate this library. Is it a good library?
Informations about the package fallback-chain
Fallback Chain
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:
Usage
Basic Example
How It Works
- Create a chain with a shared context (any value - array, object, etc.)
- Add stages using the fluent
add()method - 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 optionalonFailurecallback runs - Execution continues to the next stage
- Handle total failure - if all stages fail,
AllStagesFailedExceptionis 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
Accessing Failed Exceptions
When all stages fail, you can inspect all the exceptions:
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of fallback-chain with dependencies
PHP Build Version
Package Version
Requires
php Version
^7.1 || ^8.0
The package mahdi-mh/fallback-chain contains the following files
Loading the files please wait ...