PHPackages                             n1215/http-context - 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. n1215/http-context

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

n1215/http-context
==================

Additional interfaces for handling PSR-7 HTTP messages.

v0.1.0(10y ago)7311MITPHPPHP &gt;=7.0.0

Since Apr 2Pushed 6y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (2)Used By (1)

HttpContextInterface and HttpHandlerInterface
=============================================

[](#httpcontextinterface-and-httphandlerinterface)

Additional interfaces for PSR-7 HTTP Messages. Make PSR-7 HTTP middlewares (or applications) simpler and more composable.

HttpContext
-----------

[](#httpcontext)

HttpContext holds PSR-7 HTTP request, HTTP response, and state.

```
interface HttpContextInterface
{
    public function getRequest() : ServerRequestInterface;

    public function getResponse() : ResponseInterface;

    public function isTerminated(): bool;

    public function withRequest(ServerRequestInterface $request): HttpContextInterface;

    public function withResponse(ResponseInterface $response): HttpContextInterface;

    public function withIsTerminated(bool $isTerminated): HttpContextInterface;

    public function handledBy(HttpHandlerInterface $handler): HttpContextInterface;

}
```

HttpHandler
-----------

[](#httphandler)

Handles HttpContext. An abstraction of Http middlewares, HTTP applications, or controller actions in typical MVC web frameworks.

```
interface HttpHandlerInterface
{
    public function __invoke(HttpContextInterface $context) : HttpContextInterface;
}
```

Comparison with popular PSR-7 middlewares
=========================================

[](#comparison-with-popular-psr-7-middlewares)

- No callable chain
- Naturally compose pipeline

[![middleware comparison](img/middleware_comparison.png)](img/middleware_comparison.png)

Example
=======

[](#example)

```
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Context\HttpContextInterface;
use N1215\Http\Context\HttpHandlerInterface;

class HttpHandler implements HttpHandlerInterface {

    public function __invoke(HttpContextInterface $context): HttpContextInterface
    {
        //do stuff
        $context->getResponse()->getBody()->write('Hello, world!');
        return $context;
    }

}

/**
 * @var ServerRequestInterface $request
 */
$request = ServerRequestFactory::fromGlobals();

/**
 * @var ResponseInterface $response
 */
$response = new Response();

$context = new HttpContext($request, $response); // implements HttpContextInterface

$handler = new HttpHandler();

$newContext = $handler->__invoke($context); // or $handler($context);

$newResponse = $newContext->getResponse();
```

sequential context handling
---------------------------

[](#sequential-context-handling)

```
$context = new HttpContext($request, $response);

$first = new FirstHttpHandler(); // implements HttpHandlerInterface
$second = new SecondHttpHandler(); // implements HttpHandlerInterface

$newContext = $second($first($context));
```

sequential context handling (method chain)
------------------------------------------

[](#sequential-context-handling-method-chain)

```
$context = new HttpContext($request, $response);

$newContext = $context
    ->handledBy(new FirstHttpHandler());
    ->handledBy(new SecondHttpHandler());
```

compose handler pipeline as a HttpHandler
-----------------------------------------

[](#compose-handler-pipeline-as-a-httphandler)

```
class HandlerPipeline implements HttpHandlerInterface {

    /**
     * @var HttpHandlerInterface[]
     */
    private $handlers = [];

    public function __construct(array $handlers = []) {
        $this->handlers = $handlers;
    }

    public function __invoke(HttpContextInterface $context) : HttpContextInterface
    {
        foreach($this->handlers as $handler) {
            if($context->isTerminated()) {
                return $context;
            }
            $context = $handler->__invoke($context);
        }

        return $context;
    }
}

$context = new HttpContext($request, $response);

$pipeline = new HandlerPipeline([
    new FirstHttpHandler(),
    new SecondHttpHandler(),
]);

$newContext = $pipeline($context);
```

License
=======

[](#license)

MIT License.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

3690d ago

### Community

Maintainers

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

---

Top Contributors

[![n1215](https://avatars.githubusercontent.com/u/2157593?v=4)](https://github.com/n1215 "n1215 (13 commits)")

---

Tags

psr-7httppsr-7http-messageContexthandler

### Embed Badge

![Health badge](/badges/n1215-http-context/health.svg)

```
[![Health](https://phpackages.com/badges/n1215-http-context/health.svg)](https://phpackages.com/packages/n1215-http-context)
```

###  Alternatives

[symfony/psr-http-message-bridge

PSR HTTP message bridge

1.3k296.6M804](/packages/symfony-psr-http-message-bridge)[psr/http-server-handler

Common interface for HTTP server-side request handler

175101.3M917](/packages/psr-http-server-handler)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

86874.0k94](/packages/httpsoft-http-message)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[art4/requests-psr18-adapter

Use WordPress/Requests as a PSR-18 HTTP client

153.3k](/packages/art4-requests-psr18-adapter)

PHPackages © 2026

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