# Http Handler

Simple PSR-15 Http Handler

### Installation

```shell
composer require sonsofphp/http-handler
```

### Usage

Usage is pretty simple.

```php
<?php

use SonsOfPHP\Component\HttpHandler\HttpHandler;
use SonsOfPHP\Component\HttpHandler\MiddlewareStack;

$stack = new MiddlewareStack();
$stack->add(new RouterMiddleware());
$stack->add(new CookieMiddleware());
$stack->add(function ($request, $handler) {
    // ...
});
// ...

$app = new HttpHandler($stack);
$response = $app->handle($request);
```

The `MiddlewareStack` accepts objects that implement `Psr\Http\Server\MiddlewareInterface` and anonymous functions.

#### Middleware Priorities

An optional second argument may be passed to the `MiddlewareStack` which is for the priority of the middleware. Priorities are ordered in ascending order.

```php
<?php

use SonsOfPHP\Component\HttpHandler\MiddlewareStack;

$stack = new MiddlewareStack();
$stack->add(new NotFoundMiddleware(), 1025);
$stack->add(new RouterMiddleware(), 255);
$stack->add(new CookieMiddleware(), -255);
$stack->add(new DefaultMiddleware());
```

In the above example, the `CookieMiddleware` will be processed first and `NotFoundMiddleware` will be processed last.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sonsofphp.com/components/http-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
