tabuna/failback

创建不可访问分支的小型包装器

2.0 2020-01-08 03:17 UTC

This package is auto-updated.

Last update: 2024-09-22 04:40:43 UTC


README

Failback 行为是一个轻量级的包装器,可让您优雅地处理不可用情况。

安装

要安装此包,您可以使用 Composer。只需运行以下命令

$ composer require tabuna/failback

使用方法

设置默认值

您可以使用以下代码创建具有默认值的行为

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')();

添加回退行为

您可以使用 fail 方法定义多个回退行为

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

使用类

要使用类指定回退行为,请创建一个具有 __invoke 方法的匿名类

$class = new class {

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

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

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件