http-interop/http-factory-discovery
Composer 安装命令:
composer require http-interop/http-factory-discovery
包简介
Utility to locate available HTTP factory implementations
README 文档
README
Package for automatic discovery of available implementations providing HTTP functionality. Allows for fast switching between different implementations with minimal effort.
Automatic discovery of HTTP Factories and HTTP Clients is supported.
By default, the following implementations can be discovered:
HTTP Factory
HTTP Client
Additional implementations can be registered.
Install
composer require http-interop/http-factory-discovery
Usage
HTTP Factory
use Http\Factory\Discovery\HttpFactory; /** @var \Psr\Http\Message\RequestFactoryInterface */ $requestFactory = HttpFactory::requestFactory(); /** @var \Psr\Http\Message\ResponseFactoryInterface */ $responseFactory = HttpFactory::responseFactory(); /** @var \Psr\Http\Message\ServerRequestFactoryInterface */ $serverRequestFactory = HttpFactory::serverRequestFactory(); /** @var \Psr\Http\Message\StreamFactoryInterface */ $streamFactory = HttpFactory::streamFactory(); /** @var \Psr\Http\Message\UriFactoryInterface */ $uriFactory = HttpFactory::uriFactory(); /** @var \Psr\Http\Message\UploadedFileFactoryInterface */ $uploadedFileFactory = HttpFactory::uploadedFileFactory();
HTTP Client
use Http\Factory\Discovery\HttpClient; /** @var \Psr\Http\Client\ClientInterface */ $client = HttpClient::client();
Best Practices
Because this package acts as a service locator it should be used to supplement dependency injection.
HTTP Factory
A prime example for using HTTP Factories would be when writing PSR-15 middleware:
namespace Acme\Middleware; use Http\Factory\Discovery\HttpFactory; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface as Handler; class CatchErrors extends MiddlewareInterface { /** @var ResponseFactoryInterface */ private $responseFactory; /** @var StreamFactoryInterface */ private $streamFactory; public function __construct( ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null ) { $this->responseFactory = $responseFactory ?? HttpFactory::responseFactory(); $this->streamFactory = $streamFactory ?? HttpFactory::streamFactory(); } public function process(Request $request, Handler $handler): Response { try { return $handler->handle($request); } catch (\Throwable $error) { $stream = $this->streamFactory->createStream($e->getMessage()); $response = $this->responseFactory->createResponse(500); $response = $response->withHeader('content-type', 'text/plain'); $response = $response->withBody($stream); return $response; } } }
HTTP Client
An example for using both HTTP Client and HTTP Factories would be when writing functionality sending HTTP requests:
namespace Acme; use Http\Factory\Discovery\HttpClient; use Http\Factory\Discovery\HttpFactory; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; class Api { /** @var ClientInterface */ private $client; /** @var RequestFactoryInterface */ private $requestFactory; public function __construct( ClientInterface $client = null, RequestFactoryInterface $requestFactory = null ) { $this->client = $client ?? HttpClient::client(); $this->requestFactory = $requestFactory ?? HttpFactory::requestFactory(); } public function query(): string { $request = $this->requestFactory->createRequest('GET', 'http://acme.com/api'); return $this->client->sendRequest($request)->getBody()->getContents(); } }
Registering Additional Implementations
Additional implementations can be registered:
HTTP Factory
use Acme\RequestFactory; use Http\Factory\Discovery\FactoryLocator; use Psr\Http\Message\RequestFactoryInterface; FactoryLocator::register(RequestFactoryInterface::class, RequestFactory::class);
HTTP Client
use Acme\Client; use Http\Factory\Discovery\ClientLocator; use Psr\Http\Client\ClientInterface; ClientLocator::register(ClientInterface::class, Client::class);
Implementations can also be unregistered, if you prefer not to use them:
HTTP Factory
use Http\Factory\Discovery\FactoryLocator; use Http\Factory\Guzzle\UriFactory; use Psr\Http\Message\UriFactoryInterface; FactoryLocator::unregister(UriFactoryInterface::class, UriFactory::class);
HTTP Client
use Http\Factory\Discovery\ClientLocator; use Http\Adapter\Guzzle6\Client; use Psr\Http\Client\ClientInterface; ClientLocator::unregister(ClientInterface::class, Client::class);
Clearing Cache
The cache of discovered implementations can be cleared:
HTTP Factory
use Http\Factory\Discovery\HttpFactory; use Psr\Http\Message\UriFactoryInterface; // Clear a single interface HttpFactory::clearCache(UriFactoryInterface::class); // Clear all interfaces HttpFactory::clearCache();
HTTP Client
use Http\Factory\Discovery\HttpClient; use Psr\Http\Client\ClientInterface; // Clear a single interface HttpClient::clearCache(ClientInterface::class); // Clear all interfaces HttpClient::clearCache();
Note: Cache is automatically cleared when FactoryLocator::unregister() or
ClientLocator::unregister() is called.
http-interop/http-factory-discovery 适用场景与选型建议
http-interop/http-factory-discovery 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 236.08k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「client」 「factory」 「discovery」 「psr-7」 「psr7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 http-interop/http-factory-discovery 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 http-interop/http-factory-discovery 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 http-interop/http-factory-discovery 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Testing object factory for PHP
Testing object factory for PHP
Laravel 5 Model Factory Generator.
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 236.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 27
- 依赖项目数: 9
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-11