PHPackages                             affinity4/middleware-factory - 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. affinity4/middleware-factory

Abandoned → [middlewares/utils](/?search=middlewares%2Futils)Library[HTTP &amp; Networking](/categories/http)

affinity4/middleware-factory
============================

Use Psr17Factories to dispatch PSR-15 middleware packages. Default Psr17Factory is Nyholm\\Psr7\\Factory\\Psr17Factory

3.0.1(7y ago)023MITPHPPHP &gt;=7.2

Since Sep 30Pushed 7y agoCompare

[ Source](https://github.com/affinity4/middleware-factory)[ Packagist](https://packagist.org/packages/affinity4/middleware-factory)[ Docs](https://github.com/affinity4/middleware-factory)[ RSS](/packages/affinity4-middleware-factory/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (8)Versions (30)Used By (0)

Affinity4: Middleware Factory
=============================

[](#affinity4-middleware-factory)

A fork of [Middlewares\\Utils](https://github.com/middlewares/utils) package after they removed support and tests for Nyholm\\Psr7.

This fork also upgrades PHPUnit to 8.1+, requires PHP7.2+ and drops support for GuzzleHttp, Slim/Http and Zend Diactoros. The versions of these Http libraries in Middlewares\\Utils were quite old anyways, and many of those libraries will soon have their own Psr17 implementations anyways

Nyholm\\Psr7\\Factory\\Psr17Factory was chosen due to it's performance compared the other Psr7 implementations

Factory
-------

[](#factory)

Used to create psr-7 instances of `ServerRequestInterface`, `ResponseInterface`, `StreamInterface` and `UriInterface`. Comes with support for [Nyholm/psr7](https://github.com/Nyholm/psr7) out of the box, but you can register any different factory using the [psr/http-factory](https://github.com/php-fig/http-factory) interface.

```
use Affinity4\MiddlewareFactory\Factory;

$request = Factory::createServerRequest('GET', '/');
$response = Factory::createResponse(200);
$stream = Factory::createStream('Hello world');
$uri = Factory::createUri('http://example.com');

// By default MiddlewareFactory uses Nyholm\Psr7\Factory\Psr17Factory,
// but you can change it and add other classes
use Acme\Psr17Factory as AcmePsr17Factory
Factory::setStrategies([
    AcmePsr17Factory::class
]);

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

$fooResponse = Factory::createResponse();

// Get the PSR-17 factory used
$uriFactory = Factory::getUriFactory();
$uri = $uriFactory->createUri('http://hello-world.com');
```

Dispatcher
----------

[](#dispatcher)

Minimalist PSR-15 compatible dispatcher

```
use Affinity4\MiddlewareFactory\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 Affinity4\MiddlewareFactory\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 Affinity4\MiddlewareFactory\HttpErrorException;

$exception = HttpErrorException::create(500, [
    'problem' => 'Something bad happened',
]);

// Additional context can be get and set on the exception
$context = $exception->getContext();
```

Traits
------

[](#traits)

Common utilities shared between many middlewares like the ability to customize PSR-17 factories

- `HasResponseFactory`
- `HasStreamFactory`

---

What Next
---------

[](#what-next)

- Code style cleanup
- More tests to improve code coverage using @covers annotation to reduce false positives and ensure isolation of tests

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

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 90.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 ~37 days

Recently: every ~75 days

Total

27

Last Release

2589d ago

Major Versions

v0.14.0 → v1.0.02018-01-24

v1.2.0 → v2.0.02018-08-01

v2.2.0 → 3.02019-05-30

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

v0.13.0PHP ^7.0

3.0PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8245712?v=4)[Affinity4](/maintainers/Affinity4)[@affinity4](https://github.com/affinity4)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (92 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)")[![Sevavietl](https://avatars.githubusercontent.com/u/1844827?v=4)](https://github.com/Sevavietl "Sevavietl (1 commits)")[![ecolinet](https://avatars.githubusercontent.com/u/65506?v=4)](https://github.com/ecolinet "ecolinet (1 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (1 commits)")[![MaSpeng](https://avatars.githubusercontent.com/u/18685557?v=4)](https://github.com/MaSpeng "MaSpeng (1 commits)")[![sagikazarmark](https://avatars.githubusercontent.com/u/1226384?v=4)](https://github.com/sagikazarmark "sagikazarmark (1 commits)")

---

Tags

httppsr-7middlewarePSR-11psr-17psr-15

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/affinity4-middleware-factory/health.svg)

```
[![Health](https://phpackages.com/badges/affinity4-middleware-factory/health.svg)](https://phpackages.com/packages/affinity4-middleware-factory)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.1B3.8k](/packages/guzzlehttp-psr7)[cakephp/cakephp

The CakePHP framework

8.8k19.1M1.7k](/packages/cakephp-cakephp)[mezzio/mezzio

PSR-15 Middleware Microframework

3913.8M120](/packages/mezzio-mezzio)[mezzio/mezzio-authentication-oauth2

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

28545.4k3](/packages/mezzio-mezzio-authentication-oauth2)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

577.0M95](/packages/laminas-laminas-stratigility)[middlewares/utils

Common utils for PSR-15 middleware packages

503.6M94](/packages/middlewares-utils)

PHPackages © 2026

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