Northwoods Broker
Broker is a PSR-15 middleware dispatcher that uses conditional matching. As opposed to most middleware dispatcher, Broker verifies each middleware can be applied to the request before executing it. This allows for a centralized configuration of middleware without compromising flexibility.
Attempts to be PSR-1, PSR-2, PSR-4, and PSR-15 compliant.
Install
composer require northwoods/broker
Usage
Broker works best when combined with a PSR-11 container. When a container is provided middleware can be added by class name. Without the container middleware must be fully constructed.
use Northwoods\Broker\Broker;
$broker = new Broker($container);
Broker operates on conditions. The middleware will not be executed unless the
condition returns true
. Because some middleware is not conditional the always()
method makes it easy to add middleware that must be executed on every request.
The when()
method accepts a condition and a list of middleware to execute if
the condition passes. All middleware is executed in the order added.
// Always execute these middleware
$broker->always([
Acme\HandleErrors::class,
Acme\DetectClientIp::class,
]);
// Do we need to redirect to a secure connection?
$broker->when(
function (ServerRequestInterface $request) {
$scheme = $request->getUri()->getScheme();
return $scheme !== 'https';
},
Acme\RedirectHttps::class,
);
// Add more middleware to be executed
$broker->always([
Acme\ParseBody::class,
Acme\HandleRequest::class,
]);
To dispatch the middleware call the handle()
method with a request instance
and a callable $default
that returns the response, typically a 404 Not Found
,
if no middleware can handle the request.
// Define the callable to create a 404 response
$default = [Acme\ResponseFactor::class, 'notFound'];
// Execute the middleware as a dispatcher
$response = $broker->handle($request, $default);
Broker also implements MiddlewareInterface
and can be used as a middleware:
// Execute the broker like any other PSR-15 middleware
$response = $broker->process($request, $delegate);
License
MIT