PHPackages                             middlewares/utils - 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. middlewares/utils

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

middlewares/utils
=================

Common utils for PSR-15 middleware packages

v4.0.2(1y ago)503.4M↓13.2%12[1 issues](https://github.com/middlewares/utils/issues)20MITPHPPHP &gt;=8.1CI passing

Since Sep 30Pushed 1y ago3 watchersCompare

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

READMEChangelog (10)Dependencies (14)Versions (35)Used By (20)

middlewares/utils
=================

[](#middlewaresutils)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1f08031fea31247689f6e05d5cea31b63e390eba1a89c09310f002ec66b5c754/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f7574696c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/utils)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/utils/workflows/testing/badge.svg)](https://github.com/middlewares/utils/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/fb845c02d4a38e4dc3d7aecccf54e487f10ac16670dc8bf8b286e4c2b0c935f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f7574696c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/utils)

Common utilities used by the middlewares' packages:

- [Factory](#factory)
- [Dispatcher](#dispatcher)
- [CallableHandler](#callablehandler)
- [HttpErrorException](#httperrorexception)

Installation
------------

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/utils](https://packagist.org/packages/middlewares/utils).

```
composer require middlewares/utils
```

Factory
-------

[](#factory)

Used to create PSR-7 and PSR-17 instances. Detects automatically [Diactoros](https://github.com/laminas/laminas-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), [Nyholm/psr7](https://github.com/Nyholm/psr7) and [Sunrise](https://github.com/sunrise-php) but you can register a different factory using the [psr/http-factory](https://github.com/php-fig/http-factory) interface.

```
use Middlewares\Utils\Factory;
use Middlewares\Utils\FactoryDiscovery;

// Create PSR-7 instances
$request = Factory::createRequest('GET', '/');
$serverRequest = Factory::createServerRequest('GET', '/');
$response = Factory::createResponse(200);
$stream = Factory::createStream('Hello world');
$uri = Factory::createUri('http://example.com');
$uploadedFile = Factory::createUploadedFile($stream);

// Get PSR-17 instances (factories)
$requestFactory = Factory::getRequestFactory();
$serverRequestFactory = Factory::getServerRequestFactory();
$responseFactory = Factory::getResponseFactory();
$streamFactory = Factory::getStreamFactory();
$uriFactory = Factory::getUriFactory();
$uploadedFileFactory = Factory::getUploadedFileFactory();

// By default, use the FactoryDiscovery class that detects diactoros, guzzle, slim, nyholm and sunrise (in this order of priority),
// but you can change it and add other libraries

Factory::setFactory(new FactoryDiscovery(
    'MyApp\Psr17Factory',
    FactoryDiscovery::SLIM,
    FactoryDiscovery::GUZZLE,
    FactoryDiscovery::DIACTOROS
));

//And also register directly an initialized factory
Factory::getFactory()->setResponseFactory(new FooResponseFactory());

$fooResponse = Factory::createResponse();
```

Dispatcher
----------

[](#dispatcher)

Minimalist PSR-15 compatible dispatcher. Used for testing purposes.

```
use Middlewares\Utils\Dispatcher;

$response = Dispatcher::run([
    new Middleware1(),
    new Middleware2(),
    new Middleware3(),
    function ($request, $next) {
        $response = $next->handle($request);
        return $response->withHeader('X-Foo', 'Bar');
    }
]);
```

CallableHandler
---------------

[](#callablehandler)

To resolve and execute a callable. It can be used as a middleware, server request handler or a callable:

```
use Middlewares\Utils\CallableHandler;

$callable = new CallableHandler(function () {
    return 'Hello world';
});

$response = $callable();

echo $response->getBody(); //Hello world
```

HttpErrorException
------------------

[](#httperrorexception)

General purpose exception used to represent HTTP errors.

```
use Middlewares\Utils\HttpErrorException;

try {
    $context = ['problem' => 'Something bad happened'];
    throw HttpErrorException::create(500, $context);
} catch (HttpErrorException $exception) {
    $context = $exception->getContext();
}
```

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance43

Moderate activity, may be stable

Popularity55

Moderate usage in the ecosystem

Community40

Growing community involvement

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81.2% 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 ~92 days

Recently: every ~378 days

Total

34

Last Release

480d ago

Major Versions

v0.14.0 → v1.0.02018-01-24

v1.2.0 → v2.0.02018-08-01

v2.x-dev → v3.0.02019-11-29

v3.3.0 → v4.0.02023-12-17

PHP version history (5 changes)v0.1.0PHP ^5.6 || ^7.0

v0.13.0PHP ^7.0

v3.0.0PHP ^7.2

v3.2.0PHP ^7.2 || ^8.0

v4.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/e99df56b617f4af8cf0556a51a0ca20c7420104920c57d7e9eab093f92bb744f?d=identicon)[oscarotero](/maintainers/oscarotero)

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (125 commits)")[![solcik](https://avatars.githubusercontent.com/u/1543737?v=4)](https://github.com/solcik "solcik (6 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (5 commits)")[![fenric](https://avatars.githubusercontent.com/u/2872934?v=4)](https://github.com/fenric "fenric (4 commits)")[![eusonlito](https://avatars.githubusercontent.com/u/644551?v=4)](https://github.com/eusonlito "eusonlito (3 commits)")[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (2 commits)")[![sagikazarmark](https://avatars.githubusercontent.com/u/1226384?v=4)](https://github.com/sagikazarmark "sagikazarmark (1 commits)")[![Sevavietl](https://avatars.githubusercontent.com/u/1844827?v=4)](https://github.com/Sevavietl "Sevavietl (1 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (1 commits)")[![MekDrop](https://avatars.githubusercontent.com/u/342641?v=4)](https://github.com/MekDrop "MekDrop (1 commits)")[![ecolinet](https://avatars.githubusercontent.com/u/65506?v=4)](https://github.com/ecolinet "ecolinet (1 commits)")[![MaSpeng](https://avatars.githubusercontent.com/u/18685557?v=4)](https://github.com/MaSpeng "MaSpeng (1 commits)")[![mbolli](https://avatars.githubusercontent.com/u/722725?v=4)](https://github.com/mbolli "mbolli (1 commits)")[![dirx](https://avatars.githubusercontent.com/u/795259?v=4)](https://github.com/dirx "dirx (1 commits)")[![mikespub](https://avatars.githubusercontent.com/u/1447115?v=4)](https://github.com/mikespub "mikespub (1 commits)")

---

Tags

httpmiddlewarepsr-15psr-17psr-7utilshttppsr-7middlewarePSR-11psr-17psr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/middlewares-utils/health.svg)

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

###  Alternatives

[mezzio/mezzio

PSR-15 Middleware Microframework

3883.6M97](/packages/mezzio-mezzio)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

586.6M81](/packages/laminas-laminas-stratigility)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[chubbyphp/chubbyphp-framework

A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

13544.4k4](/packages/chubbyphp-chubbyphp-framework)

PHPackages © 2026

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