cbaconnier/laravel-http-pool-callback
Composer 安装命令:
composer require cbaconnier/laravel-http-pool-callback
包简介
A Laravel package that enhances the HTTP pool functionality by allowing users to apply callbacks to the results.
README 文档
README
Laravel HTTP Pool - Callback is a package that enhances the HTTP pool functionality in Laravel. It allows users to apply callbacks to the results of HTTP requests made using Laravel's HTTP pool. This makes it easier to process and manipulate the responses as needed.
Installation
You can install the package via Composer:
composer require cbaconnier/laravel-http-pool-callback
Usage
Prepare the requests
You must implement the HttpPoolAware interface and use the HasHttpPool trait in your repository classes.
This will allow you to register callbacks to be applied to the responses of the requests.
use GuzzleHttp\Promise\PromiseInterface; use Cbaconnier\HttpPool\HttpPoolAware; use Cbaconnier\HttpPool\HasHttpPool; use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\Response; class InvoiceRepository implements HttpPoolAware { use HasHttpPool; public function findAsync(int $id): PromiseInterface { $promise = $this->http()->get("https://example.com/invoices/{$id}"); // Register the callback that will be executed on the response $this->onPromiseResolved(function (Response $response) { return new InvoiceDto($response->getBody()->getContents()); }); return $promise; } public function find(int $id): InvoiceDto { // You can still use the repository without async requests as well $response = $this->http()->get("https://example.com/invoices/{$id}"); return new InvoiceDto($response->getBody()->getContents()); } // Not necessary but here to demonstrate a common use case // Then use $this->client()->get(...) instead of $this->http()->get(...) public function client(): PendingRequest { return $this->http() ->withHeaders([ 'Authorizations' => 'Bearer ...' ]) ->withOptions([ // .. ]); } }
Pool the requests and apply the callbacks
runAsync will clone your repositories instances to no infer with other requests and delegate the requests to the native Http::pool() method.
By doing so, you can still benefit from the Http methods and testing helpers.
When you call getResponses, it will returns the default Laravel Response objects.
When you call getResolved, it will apply the callbacks to the responses and returns the results.
use Cbaconnier\HttpPool\Facades\HttpPool; $pool = HttpPool::runAsync([ 'invoice' => $invoiceRepository->async(fn (InvoiceRepository $repository) => $repository->findAsync(123)), 'client' => $clientRepository->async(fn (ClientRepository $repository) => $repository->findAsync(123)), ]); $pool->getResponses(); // Returns ['invoice' => Response, 'client' => Response] $pool->getResolved(); // Returns ['invoice' => InvoiceDto, 'client' => ClientDto] // You can also use macro instead of facade Http::runAsync([ ... ])->getResolved();
Testing
composer test
Security
If you discover any security related issues, please email the author instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
cbaconnier/laravel-http-pool-callback 适用场景与选型建议
cbaconnier/laravel-http-pool-callback 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.06k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cbaconnier/laravel-http-pool-callback 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cbaconnier/laravel-http-pool-callback 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-02