jerowork/middleware-request-handler

This package is abandoned and no longer maintained. No replacement package was suggested.

Minimalist PSR-15 middleware based request handler (dispatcher)

0.5.0 2019-04-05 19:23 UTC

This package is auto-updated.

Last update: 2019-11-21 18:01:17 UTC


README

Minimalist PSR-15 middleware based request handler (dispatcher).

Installation

Install via Composer:

$ composer require jerowork/middleware-request-handler

Usage

Use with a PSR-7 request implementation, like Zend Diactoros.

Example

use Jerowork\MiddlewareDispatcher\Middleware\FinalResponseMiddleware;
use Jerowork\MiddlewareDispatcher\MiddlewareRequestHandler;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\SapiEmitter;
use Zend\Diactoros\ServerRequestFactory;

// Setup a list of PSR-15 middlewares
$middlewares = [
    new SomeMiddleware(),
    new AnotherMiddleware()
];

// Setup request handler
$handler = new MiddlewareRequestHandler($middlewares);

// Add other middlewares after construct
$handler->addMiddleware(
    new ThirdMiddleware(),
    new FourthMiddleware()
);

// Add final reversal order middleware
$handler->addMiddleware(new FinalResponseMiddleware(new Response());

// Handle a PSR-7 server request to response by the request handler (PSR-15)
$response = $handler->handle(ServerRequestFactory::fromGlobals());

// Output PSR-7 response with a response emitter implementation of your choice
(new SapiEmitter())->emit($response);