psr-discovery/discovery 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

psr-discovery/discovery

Composer 安装命令:

composer require psr-discovery/discovery

包简介

Lightweight library that discovers available PSR implementations by searching for a list of well-known classes that implement the relevant interfaces, and returning an instance of the first one that is found.

README 文档

README

Lightweight library that discovers available PSR implementations by searching for a list of well-known classes that implement the relevant interfaces, and returning an instance of the first one that is found.

The library currently supports PSR-18 HTTP Clients, 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 interfaces without requiring hard dependencies on specific implementations or demanding extra configuration by users.

Requirements

  • PHP 8.2+
  • Composer 2.0+

The discovery of a particular interface requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.

Installation

You should install the appropriate dependencies from the list below for the PSRs you wish to have discovery support for.

Meta-Packages

The psr-discovery/all meta-package includes all of the discovery suite packages. If you're looking for an all-in-one solution, this is the one you want.

Installation:

composer require psr-discovery/all

See the following sections for more information on the individual usage of each package.

PSR-18 HTTP Clients

Installation:

composer require psr-discovery/http-client-implementations

Usage:

use PsrDiscovery\Discover;

$httpClient = Discover::httpClient();

Please see the psr-discovery/http-client-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

PSR-17 HTTP Factories

Installation:

composer require psr-discovery/http-factory-implementations

Usage:

use PsrDiscovery\Discover;

// Returns a PSR-17 RequestFactoryInterface instance
$requestFactory = Discover::httpRequestFactory();

// Returns a PSR-17 ResponseFactoryInterface instance
$responseFactory = Discover::httpResponseFactory();

// Returns a PSR-17 StreamFactoryInterface instance
$streamFactory = Discover::httpStreamFactory();

// Returns a PSR-7 RequestInterface instance
$request = $requestFactory->createRequest('GET', 'https://example.com');

Please see the psr-discovery/http-factory-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

PSR-14 Event Dispatchers

Installation:

composer require psr-discovery/event-dispatcher-implementations

Usage:

use PsrDiscovery\Discover;

$eventDispatcher = Discover::eventDispatcher();

Please see the psr-discovery/event-dispatcher-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

PSR-11 Containers

Installation:

composer require psr-discovery/container-implementations

Usage:

use PsrDiscovery\Discover;

$container = Discover::container();

Please see the psr-discovery/container-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

PSR-6 Caches

Installation:

composer require psr-discovery/cache-implementations

Usage:

use PsrDiscovery\Discover;

$cache = Discover::cache();

Please see the psr-discovery/cache-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

PSR-3 Logs

Installation:

composer require psr-discovery/log-implementations

Usage:

use PsrDiscovery\Discover;

$log = Discover::log();

Please see the psr-discovery/log-implementations repository for a list of the supported implementations. If a particular implementation is missing you'd like to see, please open a pull request adding support.

Handling Failures

If the library is unable to discover a suitable implementation, the relevant 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();
}

Exceptions

The library will expose a PsrDiscovery\Exceptions\SupportPackageNotFoundException when a discovery method is called, but the required support package is not installed.

Singletons

By default, the discovery methods 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 an available PSR mocking library, like psr-mock/http-client-implementation or php-http/mock-client.

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 use the prefer() on any installed discovery support libraries.

Example using psr-discovery/http-factories-implementations:

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\RequestFactories;

// Prefer the a specific implementation of PSR-17 over others.
RequestFactories::prefer('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$factory = Discover::httpRequestFactory();

This will cause the discovery 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 the use() on any installed discovery support libraries.

Example using psr-discovery/http-factories-implementations:

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr17\RequestFactories;

// Only discover a specific implementation of PSR-17.
RequestFactories::use('nyholm/psr7');

// Return an instance of Nyholm\Psr7\Factory\Psr17Factory,
// or null if it is not available.
$factory = Discover::httpRequestFactory();

This will cause the discovery 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/discovery 适用场景与选型建议

psr-discovery/discovery 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.05M 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「psr」 「psr-3」 「discovery」 「psr-6」 「PSR-11」 「psr-14」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 psr-discovery/discovery 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 psr-discovery/discovery 我们能提供哪些服务?
定制开发 / 二次开发

基于 psr-discovery/discovery 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.05M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 46
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-23