Broker is a dead simple PSR-15 middleware dispatcher. Broker implements
both RequestHandlerInterface
and MiddlewareInterface
for maximum flexibility.
composer require northwoods/broker
use Acme\Middleware;
use Northwoods\Broker\Broker;
/** @var \Psr\Http\Message\ServerRequestInterface */
$request = /* any server request */;
// Use append() or prepend() to add middleware
$broker = new Broker();
$broker->append(new Middleware\ParseRequest());
$broker->prepend(new Middleware\CheckIp());
/** @var \Psr\Http\Message\ResponseInterface */
$response = $broker->handle($request);
Add one or more middleware to the end of the stack.
Add one or more middleware to be beginning of the stack.
Dispatch the middleware stack as a request handler. If the end of the stack is
reached and no response has been generated, an OutOfBoundsException
will
be thrown.
Dispatch the middleware stack as a middleware. If the end of the stack is reached
and no response has been generated, the $handler
will be called.
- Conditional middleware execution can be provided by northwoods/conditional-middleware
- Lazy middleware instantiation can be provided by northwoods/lazy-middleware
- Response sending can be provided by http-interop/response-sender