siriusphp/middleware

Lightweight middleware implementation. 1 class,

1.1.0 2016-06-21 11:33 UTC

This package is auto-updated.

Last update: 2024-11-24 04:47:33 UTC


README

Source Code Latest Version Software License Build Status Coverage Status Quality Score

Regular middleware

$middlewares = array();

$middlewares[] = function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
    // do your thing
    return $response;
};

$middlewares[] = function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
    // do your thing
    return $response;
};

$runner = Sirius\Middleware\Runner::factory($middlewares);

$response = $runner(Zend\Diactoros\ServerRequestFactory::fromGlobals(), new Zend\Diactoros\Response);

The middleware runners are immutable so adding a new middleware returns a new runner. If you want to create the runner by hand

// this is WRONG!!!!!!!!!
$runner = new Sirius\Middleware\Runner;
$runner->add(function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
    // do your thing
    return $response;
});
$runner->add(function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
    // do your thing
    return $response;
});


// this is CORRECT
$runner =  (new Sirius\Middleware\Runner)
            ->add(function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
              // do your thing
              return $response;
            })
            ->add(function(RequestInterface $request, ResponseInterface $response, callable $next = null) {
              // do your thing
              return $response;
            });

Frame middleware

Inspired by this article http://blog.ircmaxell.com/2016/05/all-about-middleware.html

$middlewares = array();

// first middleware in the stack must return an response
$middlewares[] = function(RequestInterface $request, callable $next = null) {
    return new Zend\Diactoros\Response;
};

$middlewares[] = function(RequestInterface $request, callable $next = null) {
    // do your thing
    return $response;
};

$middlewares[] = function(RequestInterface $request, callable $next = null) {
    // do your thing
    return $response;
};

$runner = Sirius\Middleware\FrameRunner::factory($middlewares);

$response = $runner(Zend\Diactoros\ServerRequestFactory::fromGlobals());