2017 © Pedro Peláez
 

library middleland

PSR-15 middleware dispatcher

image

oscarotero/middleland

PSR-15 middleware dispatcher

  • Wednesday, January 24, 2018
  • by oscarotero
  • Repository
  • 2 Watchers
  • 19 Stars
  • 1,647 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 10 Versions
  • 48 % Grown

The README.md

Middleland

Latest Version on Packagist ![Software License][ico-license] Testing ![Quality Score][ico-scrutinizer], (*1)

Simple (but powerful) PSR-15 middleware dispatcher:, (*2)

Requirements

Example

use Middleland\Dispatcher;

$middleware = [
    new Middleware1(),
    new Middleware2(),
    new Middleware3(),

    // A dispatcher can be used to group middlewares
    new Dispatcher([
        new Middleware4(),
        new Middleware5(),
    ]),

    // You can use closures
    function ($request, $next) {
        $response = $next->handle($request);
        return $response->withHeader('X-Foo', 'Bar');
    },

    // Or use a string to create the middleware on demand using a PSR-11 container
    'middleware6'

    // USE AN ARRAY TO ADD CONDITIONS:

    // This middleware is processed only in paths starting by "/admin"
    ['/admin', new MiddlewareAdmin()],

    // This is processed in DEV
    [ENV === 'DEV', new MiddlewareAdmin()],

    // Use callables to create other conditions
    [
        function ($request) {
            return $request->getUri()->getScheme() === 'https';
        },
        new MiddlewareHttps()
    ],

    // There are some matchers included in this library to create conditions
    [
        new Pattern('*.png'),
        new MiddlewareForPngFiles()
    ],

    //And use several for each middleware
    [
        ENV === 'DEV',
        new Pattern('*.png'),
        new MiddlewareForPngFilesInDev()
    ],
];

$dispatcher = new Dispatcher($middleware, new Container());

$response = $dispatcher->dispatch(new Request());

Matchers

As you can see in the example above, you can use an array of "matchers" to filter the requests that receive middlewares. You can use callables, instances of Middleland\Matchers\MatcherInterface or booleans, but for comodity, the string values are also used to create Middleland\Matchers\Path instances. The available matchers are:, (*3)

Name Description Example
Path Filter requests by base path. Use exclamation mark for negative matches new Path('/admin'), new Path('!/not-admin')
Pattern Filter requests by path pattern. Use exclamation mark for negative matches new Pattern('*.png') new Pattern('!*.jpg')
Accept Filter requests by Accept header. Use exclamation mark for negative matches new Accept('text/html') new Accept('!image/png')

How to create matchers

Just use a callable or an instance of the Middleland\Matchers\MatcherInterface. Example:, (*4)

use Middleland\Matchers\MatcherInterface;
use Psr\Http\Message\ServerRequestInterface;

class IsAjax implements MatcherInterface
{
    public function __invoke(ServerRequestInterface $request): bool
    {
        return $request->getHeaderLine('X-Requested-With') === 'xmlhttprequest';
    }
}

Please see CHANGELOG for more information about recent changes., (*5)

The MIT License (MIT). Please see LICENSE for more information., (*6)

The Versions

24/01 2018
24/01 2018
22/09 2017
27/02 2017
28/01 2017
09/01 2017
09/01 2017

v0.1.0

0.1.0.0 https://github.com/oscarotero/dispatcher

PSR-15 middleware dispatcher

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware psr-7 http psr-15