tabuna/failback

Small wrapper for creating a branch of inaccessibility

2.0 2020-01-08 03:17 UTC

This package is auto-updated.

Last update: 2024-11-22 05:02:59 UTC


README

Actions for Failback is a lightweight wrapper that empowers you to handle unavailability gracefully.

Installation

To install the package, you can use Composer. Simply run the following command:

$ composer require tabuna/failback

Usage

Setting a Default Value:

You can create an action with a default value using the following code:

use Tabuna\FailBack\Action;

// $result = 'default';
$result = Action::make(function () {
    throw new \Exception();
}, 'default')->run();

// Alternatively, you can use the short helper
$result = failBack(function () {
    throw new \Exception();
}, 'default')();

Adding Fallback Actions:

You can define multiple fallback actions using the fail method:

// $result = true;
$result = failBack(function () {
    throw new \Exception();
})->fail(function () {
    throw new \Error();
})->fail(function () {
    return true;
})();

Utilizing Classes:

To specify a fallback action using a class, create an anonymous class with an __invoke method:

$class = new class {

    /**
     * @return bool
     */
    public function __invoke(): bool
    {
        return true;
    }
};

// $result = true;
$result = failBack(function () {
    throw new Exception();
})->fail($class)->run();

License

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