eugenganshorn/guzzle-bundle-retry-plugin

Guzzle Bundle 重试插件,一个用于构建 RESTful 网络服务客户端的 PHP HTTP 客户端库和框架

1.4.0 2023-09-20 12:55 UTC

This package is auto-updated.

Last update: 2024-09-20 16:11:36 UTC


README

Build Status Coverage Status

需求

安装

使用 Composer

composer.json
{
    "require": {
        "eugenganshorn/guzzle-bundle-retry-plugin": "^1.0"
    }
}
命令行
$ composer require eugenganshorn/guzzle-bundle-retry-plugin

用法

启用组件

Symfony 4

插件将通过组件构造函数在 app/AppKernel.php 中激活/连接,例如:找到以下行

foreach ($contents as $class => $envs) {
    if (isset($envs['all']) || isset($envs[$this->environment])) {
        yield new $class();
    }
}

并替换为

foreach ($contents as $class => $envs) {
    if (isset($envs['all']) || isset($envs[$this->environment])) {
        if ($class === \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class) {
            yield new $class([
                new \EugenGanshorn\Bundle\GuzzleBundleRetryPlugin\GuzzleBundleRetryPlugin(),
            ]);
        } else {
            yield new $class();
        }
    }
}

Symfony 5 & 6

重写 registerBundles 方法在 src/Kernel.php 中以将插件连接到组件

public function registerBundles(): iterable
{
    $contents = require $this->getBundlesPath();
    foreach ($contents as $class => $envs) {
        if ($envs[$this->environment] ?? $envs['all'] ?? false) {
            if ($class === EightPointsGuzzleBundle::class) {
                yield new $class([
                    new GuzzleBundleRetryPlugin(),
                ]);
            } else {
                yield new $class();
            }
        }
    }
}

基本配置

# app/config/config.yml // config/packages/eight_points_guzzle.yaml

eight_points_guzzle:
    clients:
        your_client:
            base_url: "http://api.domain.tld"

            # plugin settings
            plugin:
                retry:
                    ~

高级配置

查看中间件选项:https://github.com/caseyamcl/guzzle_retry_middleware#options

许可证

此中间件受 MIT 许可证的许可 - 有关详细信息,请参阅 LICENSE 文件