phoole/middleware

Slim and simple PSR-15 compliant middleware runner library for PHP

1.1.0 2019-11-19 06:46 UTC

This package is auto-updated.

Last update: 2024-10-19 17:35:01 UTC


README

Build Status Scrutinizer Code Quality Code Climate PHP 7 Latest Stable Version License

Slim and simple PSR-15 compliant middleware runner library for PHP.

Installation

Install via the composer utility.

composer require "phoole/middleware"

or add the following lines to your composer.json

{
    "require": {
       "phoole/middleware": "1.*"
    }
}

Features

  • Able to use PSR-15 compliant middlewares out there.

  • Able to use a middleware queue (a group of middlewares) as a generic middleware in another(or the main) queue.

Usage

Create the middleware queue, then process all the middlewares.

use Phoole\Middleware\Queue;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;

// create middleware queue with 404 response as default(last)
$mws = new Queue(new Response(404));

// add middlewares
$mws->add(
    new LoggerMiddleware(),
    new DispatcherMiddleware()
);

// process the request with middlewares
$response = $mws->handle(new ServerRequest('GET', 'http://bingo.com/get'));

Advanced

  • Subqueue

    Phoole\Middleware\Queue implements the Psr\Http\Server\MiddlewareInterface, so the queue itself can be used as a generic middleware.

    // subqueue may need no default response if not the last in the main queue
    $subQueue = (new Queue())->add(
        new ResponseTimeMiddleware(),
        new LoggingMiddleware()
    );
    
    // main middleware queue
    $mws = (new Queue(new Response(404)))->add(
        $subQueue,
        new DispatcherMiddleware()
    );

Testing

$ composer test

Dependencies

  • PHP >= 7.2.0

  • phoole/base 1.*

  • A PSR-7 HTTP message implementation, such as guzzle/psr7.

License