Sirius Middleware
, (*1)
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, (*2)
// 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, (*3)
$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());