PHPackages                             psr-discovery/discovery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. psr-discovery/discovery

ActiveLibrary[HTTP &amp; Networking](/categories/http)

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.

1.2.0(1y ago)98.5M—7.3%39MITPHPPHP ^8.2

Since Mar 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/psr-discovery/discovery)[ Packagist](https://packagist.org/packages/psr-discovery/discovery)[ Docs](https://github.com/psr-discovery/discovery)[ RSS](/packages/psr-discovery-discovery/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (7)Dependencies (9)Versions (8)Used By (9)

**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](https://github.com/psr-discovery/http-client-implementations), [PSR-17 HTTP Factories](https://github.com/psr-discovery/http-factory-implementations), [PSR-14 Event Dispatchers](https://github.com/psr-discovery/event-dispatcher-implementations), [PSR-11 Containers](https://github.com/psr-discovery/container-implementations), [PSR-6 Caches](https://github.com/psr-discovery/cache-implementations) and [PSR-3 Logs](https://github.com/psr-discovery/log-implementations).

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](#requirements)
- [Installation](#installation)
    - [Meta-Packages](#meta-packages)
    - [PSR-18 HTTP Clients](#psr-18-http-clients)
    - [PSR-17 HTTP Factories](#psr-17-http-factories)
    - [PSR-14 Event Dispatchers](#psr-14-event-dispatchers)
    - [PSR-11 Containers](#psr-11-containers)
    - [PSR-6 Caches](#psr-6-caches)
    - [PSR-3 Logs](#psr-3-logs)
- [Handling Failures](#handling-failures)
- [Exceptions](#exceptions)
- [Singletons](#singletons)
- [Mocking Priority](#mocking-priority)
- [Preferring an Implementation](#preferring-an-implementation)
- [Using a Specific Implementation](#using-a-specific-implementation)

Requirements
------------

[](#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
------------

[](#installation)

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

### Meta-Packages

[](#meta-packages)

The [psr-discovery/all](https://github.com/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

[](#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](https://github.com/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

[](#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](https://github.com/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

[](#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](https://github.com/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

[](#psr-11-containers)

Installation:

```
composer require psr-discovery/container-implementations
```

Usage:

```
use PsrDiscovery\Discover;

$container = Discover::container();
```

Please see the [psr-discovery/container-implementations](https://github.com/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

[](#psr-6-caches)

Installation:

```
composer require psr-discovery/cache-implementations
```

Usage:

```
use PsrDiscovery\Discover;

$cache = Discover::cache();
```

Please see the [psr-discovery/cache-implementations](https://github.com/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

[](#psr-3-logs)

Installation:

```
composer require psr-discovery/log-implementations
```

Usage:

```
use PsrDiscovery\Discover;

$log = Discover::log();
```

Please see the [psr-discovery/log-implementations](https://github.com/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
-----------------

[](#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
----------

[](#exceptions)

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

Singletons
----------

[](#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
----------------

[](#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
----------------------------

[](#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
-------------------------------

[](#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.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity53

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 94.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~88 days

Recently: every ~69 days

Total

8

Last Release

529d ago

PHP version history (3 changes)1.0.0PHP ^8.0

1.1.1PHP ^8.1

1.2.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3093?v=4)[Evan Sims](/maintainers/evansims)[@evansims](https://github.com/evansims)

---

Top Contributors

[![evansims](https://avatars.githubusercontent.com/u/3093?v=4)](https://github.com/evansims "evansims (55 commits)")[![aidan-casey](https://avatars.githubusercontent.com/u/6686277?v=4)](https://github.com/aidan-casey "aidan-casey (1 commits)")[![flavioheleno](https://avatars.githubusercontent.com/u/471860?v=4)](https://github.com/flavioheleno "flavioheleno (1 commits)")[![reyostallenberg](https://avatars.githubusercontent.com/u/3579090?v=4)](https://github.com/reyostallenberg "reyostallenberg (1 commits)")

---

Tags

discoverypsrpsr-11psr-14psr-17psr-18psr-3psr-6psrpsr-3PSR-11psr-17psr-18psr-14discoverypsr-6

###  Code Quality

TestsPest

Static AnalysisPHPStan, Psalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/psr-discovery-discovery/health.svg)

```
[![Health](https://phpackages.com/badges/psr-discovery-discovery/health.svg)](https://phpackages.com/packages/psr-discovery-discovery)
```

###  Alternatives

[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[psr/http-client

Common interface for HTTP clients

1.7k680.7M2.1k](/packages/psr-http-client)[laminas/laminas-diactoros

PSR HTTP Message implementations

548105.8M965](/packages/laminas-laminas-diactoros)[mezzio/mezzio

PSR-15 Middleware Microframework

3883.6M97](/packages/mezzio-mezzio)[mezzio/mezzio-hal

Hypertext Application Language implementation for PHP and PSR-7

20456.9k6](/packages/mezzio-mezzio-hal)[elastic/transport

HTTP transport PHP library for Elastic products

2020.6M7](/packages/elastic-transport)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
