For middleware to use with Relay, please review middlewares/psr15-middlewares.

Relay is a PSR-15 server request handler (aka “dispatcher”) for a queue of PSR-15 middleware entries.

Request Handling

First, create an array or traversable $queue of middleware entries:

$queue[] = new FooMiddleware();
$queue[] = new BarMiddleware();
$queue[] = new BazMiddleware();
// ...
$queue[] = new ResponseFactoryMiddleware {
    return new Response();
};

Then create a Relay with the $queue and call the handle() method with a server request.

/**
 * @var \Psr\Http\Message\ServerRequestInterface $request
 * @var \Psr\Http\Message\ResponseInterface $response
 */

use Relay\Relay;

$relay = new Relay($queue);
$response = $relay->handle($request);

Queue Entry Resolvers

You may wish to use $queue entries other than already-instantiated objects. If so, you can pass a $resolver callable to Relay that will convert the $queue entry to an instance. Thus, using a $resolver allows you to pass in your own factory mechanism for $queue entries.

For example, this $resolver will naively convert $queue string entries to new class instances:

$resolver = function ($entry) {
    return new $entry();
};

You can then add $queue entries as class names, and Relay will use the $resolver to create the objects in turn.

$queue[] = 'FooMiddleware';
$queue[] = 'BarMiddleware';
$queue[] = 'BazMiddleware';
$queue[] = 'ResponseFactoryMiddleware';

$relay = new Relay($queue, $resolver);

Callable Middleware

Relay can also handle any middleware entries that are callables with the following signature:

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

function (
    Request $request, // the request
    callable $next // the next middleware or handler
) : Response {
    // ...
}

Callable middleware may be intermingled with PSR-15 middleware.