PHPackages                             echo-fusion/middlewaremanager - 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. echo-fusion/middlewaremanager

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

echo-fusion/middlewaremanager
=============================

a middleware manager implements PSR-15 and PSR-7

v1.0.2(1y ago)18[1 PRs](https://github.com/echo-fusion/middlewaremanager/pulls)MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0

Since Sep 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/echo-fusion/middlewaremanager)[ Packagist](https://packagist.org/packages/echo-fusion/middlewaremanager)[ RSS](/packages/echo-fusion-middlewaremanager/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (6)Used By (0)

MiddlewareManager
=================

[](#middlewaremanager)

This is a flexible package for managing middleware in PHP applications. It enables you to create a middleware pipeline, where middleware processes a request and passes it on to the next, providing a clean and organized way to handle middleware logic.

This package is compliant with PSR-1, PSR-2, PSR-7 and PSR-15.

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

[](#installation)

Install the package via Composer:

```
composer require echo-fusion/middlewaremanager
```

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

[](#requirements)

The following versions of PHP are supported by this version.

- PHP 8.1
- PHP 8.2
- PHP 8.3

Usage
-----

[](#usage)

Here’s how to use the MiddlewareManager to set up and run a middleware pipeline.

### 1. Define handler

[](#1-define-handler)

Define your handlers in proper directory and pass one of them to middleware manager to run it at the end of the pipeline:

```
use Psr\Http\Message\ResponseFactoryInterface
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class MyMiddlewareHandler implements RequestHandlerInterface {
    public function __construct(private readonly ResponseFactoryInterface $responseFactory)
    {
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        // write your code here

        return $this->responseFactory->createResponse();
    }
}
```

### 2. Define middlewares

[](#2-define-middlewares)

Define your middlewares in proper directory in separate files:

```
use EchoFusion\MiddlewareManager\MiddlewareManager;
use EchoFusion\MiddlewareManager\MiddlewarePipeline;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

class CoreMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // ex: manipulate request
        $request = $request->withAttribute('key','value');

        return $handler->handle($request);
    }
}

class OtherMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response =  $handler->handle($request);

        // ex: manipulate response
        return $this->convertedResponse($response);
    }
}
```

### 3. Run MiddlewareManager

[](#3-run-middlewaremanager)

To add middleware to the manager, you have two options:

- Add middleware as 'core middleware', which always runs before any middleware added later.
- Add middleware dynamically (e.g., useful for adding route-specific middleware).

Here is an example of how you can run MiddlewareManager:

Note: Don't forget to instantiate below injected dependencies in your container.

```
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use EchoFusion\MiddlewareManager\MiddlewarePipelineInterface;

function (ContainerInterface $container, ServerRequestInterface $request, MiddlewarePipelineInterface $pipeline) {

    // optional
    $coreMiddlewares = [
        CoreMiddleware::class,
    ];

    $middlewareManager = new MiddlewareManager(
        $container,
        $pipeline,
        $coreMiddlewares
    );

    // add or remove your necessary middlewares if you want
    $middlewareManager->add(OtherMiddleware::class);
    $middlewareManager->remove(OtherMiddleware::class);
    // ...

    // instantiate handler directly or get from container to resolve possible dependencies
    $handler = new MyMiddlewareHandler();

    $response = $middlewareManager->dispatch($request, $handler);

    echo $response;
}
```

Testing
-------

[](#testing)

Testing includes PHPUnit and PHPStan (Level 7).

```
$ composer test
```

Credits
-------

[](#credits)

Developed and maintained by [Amir Shadanfar](https://github.com/amir-shadanfar).
Connect on [LinkedIn](https://www.linkedin.com/in/amir-shadanfar).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/echo-fusion/middlewaremanager/blob/main/LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

5

Last Release

594d ago

Major Versions

v0.x-dev → v1.0.22024-10-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8a199998d8b5af726b9c5062f97228244822eb7b64b51adf7b050ff4bee378f?d=identicon)[echo-fusion](/maintainers/echo-fusion)

---

Top Contributors

[![echo-fusion](https://avatars.githubusercontent.com/u/182804396?v=4)](https://github.com/echo-fusion "echo-fusion (18 commits)")

---

Tags

psr-7psr-15middleware manager

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/echo-fusion-middlewaremanager/health.svg)

```
[![Health](https://phpackages.com/badges/echo-fusion-middlewaremanager/health.svg)](https://phpackages.com/packages/echo-fusion-middlewaremanager)
```

###  Alternatives

[league/route

Fast routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

6633.1M115](/packages/league-route)[mezzio/mezzio

PSR-15 Middleware Microframework

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

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)[middlewares/utils

Common utils for PSR-15 middleware packages

503.4M92](/packages/middlewares-utils)[mezzio/mezzio-authentication-oauth2

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

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

A powerful solution as the foundation of your project.

16249.8k10](/packages/sunrise-http-router)

PHPackages © 2026

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