psr-discovery/http-client-implementations
Composer 安装命令:
composer require psr-discovery/http-client-implementations
包简介
Lightweight library that discovers available PSR-18 HTTP Client implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
关键字:
README 文档
README
Lightweight library that discovers available PSR-18 HTTP Client implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
This package is part of the PSR Discovery utility suite, which also supports PSR-17 HTTP Factories, PSR-14 Event Dispatchers, PSR-11 Containers, PSR-6 Caches and PSR-3 Logs.
This is largely intended for inclusion in libraries like SDKs that wish to support PSR-18 HTTP Clients without requiring hard dependencies on specific implementations or demanding extra configuration by users.
- Requirements
- Implementations
- Installation
- Usage
- Handling Failures
- Exceptions
- Singletons
- Mocking Priority
- Preferring an Implementation
- Using a Specific Implementation
Requirements
- PHP 8.2+
- Composer 2.0+
Successful discovery requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.
Implementations
The following psr/http-client-implementation implementations are discovered and instantiated automatically:
- guzzlehttp/guzzle ^7.0 | ^8.0
- joomla/http ^2.0 | ^3.0
- kriswallsmith/buzz ^1.0
- php-http/curl-client ^2.1
- php-http/guzzle5-adapter ^2.0
- php-http/guzzle6-adapter ^2.0
- php-http/guzzle7-adapter ^0.1 | ^1.0
- php-http/socket-client ^2.0
- symfony/http-client ^4.3 | ^5.0 | ^6.0 | ^7.0 | ^8.0
- voku/httpful ^2.2 | ^3.0
- nimbly/shuttle ^1.0
The following mock implementations are also available:
If a particular implementation is missing that you'd like to see, please open a pull request adding support.
Installation
composer require psr-discovery/http-client-implementations
Usage
use PsrDiscovery\Discover; // Return an instance of the first discovered PSR-18 HTTP Client implementation. $httpClient = Discover::httpClient(); // Send a request using the discovered HTTP Client. $httpClient->sendRequest(...);
Handling Failures
If the library is unable to discover a suitable PSR-18 implementation, the Discover::httpClient() discovery method will simply return null. This allows you to handle the failure gracefully, for example by falling back to a default implementation.
Example:
use PsrDiscovery\Discover; $httpClient = Discover::httpClient(); if ($httpClient === null) { // No suitable HTTP Client implementation was discovered. // Fall back to a default implementation. $httpClient = new DefaultHttpClient(); }
Singletons
By default, the Discover::httpClient() method will always return a new instance of the discovered implementation. If you wish to use a singleton instance instead, simply pass true to the $singleton parameter of the discovery method.
Example:
use PsrDiscovery\Discover; // $httpClient1 !== $httpClient2 (default) $httpClient1 = Discover::httpClient(); $httpClient2 = Discover::httpClient(); // $httpClient1 === $httpClient2 $httpClient1 = Discover::httpClient(singleton: true); $httpClient2 = Discover::httpClient(singleton: true);
Mocking Priority
This library will give priority to searching for a known, available mocking library before searching for a real implementation. This is to allow for easier testing of code that uses this library.
The expectation is that these mocking libraries will always be installed as development dependencies, and therefore if they are available, they are intended to be used.
Preferring an Implementation
If you wish to prefer a specific implementation over others, you can prefer() it by package name:
use PsrDiscovery\Discover; use PsrDiscovery\Implementations\Psr18\Clients; // Prefer the a specific implementation of PSR-18 over others. Clients::prefer('guzzlehttp/guzzle'); // Return an instance of GuzzleHttp\Client, // or the next available from the list of candidates, // Returns null if none are discovered. $client = Discover::httpClient();
This will cause the httpClient() method to return the preferred implementation if it is available, otherwise, it will fall back to the default behavior.
Note that assigning a preferred implementation will give it priority over the default preference of mocking libraries.
Using a Specific Implementation
If you wish to force a specific implementation and ignore the rest of the discovery candidates, you can use() its package name:
use PsrDiscovery\Discover; use PsrDiscovery\Implementations\Psr18\Clients; // Only discover a specific implementation of PSR-18. Clients::use('guzzlehttp/guzzle'); // Return an instance of GuzzleHttp\Client, // or null if it is not available. $client = Discover::httpClient();
This will cause the httpClient() method to return the preferred implementation if it is available, otherwise, it will return null.
This library is not produced or endorsed by, or otherwise affiliated with, the PHP-FIG.
psr-discovery/http-client-implementations 适用场景与选型建议
psr-discovery/http-client-implementations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.04M 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「psr」 「discovery」 「psr-18」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psr-discovery/http-client-implementations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psr-discovery/http-client-implementations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psr-discovery/http-client-implementations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel package to integrate the Httplug generic HTTP client into Laravel
Simple PSR-16 cache implementations for WordPress transient the OOP way
Lightweight mock implementations of PSR-18, PSR-17 and PSR-7 tailored to simplify unit testing.
Safely namespaced Guzzle for WP2Static
Symfony Opentracing bundle extension for Zipkin tracers
REST client for PHP 7.4+
统计信息
- 总下载量: 10.04M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 39
- 依赖项目数: 20
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-23