2017 © Pedro Peláez
 

library middleman

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

image

mindplay/middleman

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

  • Thursday, May 10, 2018
  • by mindplay.dk
  • Repository
  • 7 Watchers
  • 79 Stars
  • 11,527 Installations
  • PHP
  • 11 Dependents
  • 1 Suggesters
  • 11 Forks
  • 3 Open issues
  • 11 Versions
  • 11 % Grown

The README.md

mindplay/middleman

PHP Version CI Code Coverage, (*1)

Dead simple PSR-15 / PSR-7 middleware dispatcher., (*2)

Provides (optional) integration with a variety of dependency injection containers compatible with PSR-11., (*3)

To upgrade between major releases, please see UPGRADING.md., (*4)

A growing catalog of PSR-15 middleware-components is available from github.com/middlewares., (*5)

Usage

The constructor expects an array of PSR-15 MiddlewareInterface instances:, (*6)

use mindplay\middleman\Dispatcher;

$dispatcher = new Dispatcher([
    new ErrorHandlerMiddleware(...)
    new RouterMiddleware(...),
    new NotFoundMiddleware(...),
]);

The Dispatcher implements the PSR-15 RequestHandlerInterface. This package only provides the middleware stack - to run a PSR-15 handler, for example in your index.php file, you need a PSR-15 host or a similar facility., (*7)

Note that the middleware-stack in the Dispatcher is immutable - if you need a stack you can manipulate, array, ArrayObject, SplStack etc. are all fine choices., (*8)

Anonymous Functions as Middleware

You can implement simple middleware "in place" by using anonymous functions in a middleware-stack, using a PSR-7/17 implementation such as nyholm/psr7:, (*9)

use Psr\Http\Message\ServerRequestInterface;
use mindplay\middleman\Dispatcher;
use Nyholm\Psr7\Factory\Psr17Factory;

$factory = new Psr17Factory();

$dispatcher = new Dispatcher([
    function (ServerRequestInterface $request, callable $next) {
        return $next($request); // delegate control to next middleware
    },
    function (ServerRequestInterface $request) use ($factory) {
        return $factory->createResponse(200)->withBody(...); // abort middleware stack and return the response
    },
    // ...
]);

$response = $dispatcher->handle($request);

Dependency Injection via the Resolver Function

If you want to integrate with an IOC container you can use the ContainerResolver - a "resolver" is a callable which gets applied to every element in your middleware stack, with a signature like:, (*10)

function (string $name) : MiddlewareInterface

The following example obtains middleware components on-the-fly from a DI container:, (*11)

$dispatcher = new Dispatcher(
    [
        RouterMiddleware::class,
        ErrorMiddleware::class,
    ],
    new ContainerResolver($container)
);

If you want the Dispatcher to integrate deeply with your framework of choice, you can implement this as a class implementing the magic __invoke() function (as ContainerResolver does) - or "in place", as an anonymous function with a matching signature., (*12)

If you want to understand precisely how this component works, the whole thing is just one class with a few lines of code - if you're going to base your next project on middleware, you can (and should) understand the whole mechanism., (*13)

, (*14)

Middleware?

Middleware is a powerful, yet simple control facility., (*15)

If you're new to the concept of middleware, the following section will provide a basic overview., (*16)

In a nutshell, a middleware component is a function (or MiddlewareInterface instance) that takes an incoming (PSR-7) RequestInterface object, and returns a ResponseInterface object., (*17)

It does this in one of three ways: by assuming, delegating, or sharing responsibility for the creation of a response object., (*18)

1. Assuming Responsibility

A middleware component assumes responsibility by creating and returning a response object, rather than delegating to the next middleware on the stack:, (*19)

use Zend\Diactoros\Response;

function ($request, $next) {
    return (new Response())->withBody(...); // next middleware won't be run
}

Middleware near the top of the stack has the power to completely bypass middleware further down the stack., (*20)

2. Delegating Responsibility

By calling $next, middleware near the top of the stack may choose to fully delegate the responsibility for the creation of a response to other middleware components further down the stack:, (*21)

function ($request, $next) {
    if ($request->getMethod() !== 'POST') {
        return $next($request); // run the next middleware
    } else {
        // ...
    }
}

Note that exhausting the middleware stack will result in an exception - it's assumed that the last middleware component on the stack always produces a response of some sort, typically a "404 not found" error page., (*22)

3. Sharing Responsibility

Middleware near the top of the stack may choose to delegate responsibility for the creation of the response to middleware further down the stack, and then make additional changes to the returned response before returning it:, (*23)

function ($request, $next) {
    $result = $next($request); // run the next middleware

    return $result->withHeader(...); // then modify it's response
}

The middleware component at the top of the stack ultimately has the most control, as it may override any properties of the response object before returning., (*24)

The Versions

30/11 2016

dev-discussion

dev-discussion

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

21/11 2016
29/09 2016
30/12 2015

1.1.0

1.1.0.0

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

06/11 2015

1.0.0

1.0.0.0

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires