northwoods / broker
Dead simple PSR-15 middleware dispatcher
Installs: 82 829
Dependents: 5
Suggesters: 1
Security: 0
Stars: 27
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- psr/http-server-middleware: ^1.0
Requires (Dev)
- eloquent/phony-phpunit: ^4.0
- eloquent/phpstan-phony: ^0.3.0
- phpstan/phpstan: ^0.10.3
- phpstan/phpstan-phpunit: ^0.10.0
- phpunit/phpunit: ^7.3
- squizlabs/php_codesniffer: ^3.3
Suggests
- northwoods/conditional-middleware: Execute middleware based on request conditions
- northwoods/lazy-middleware: Lazy load middleware from a container
README
Broker is a dead simple PSR-15 middleware dispatcher. Broker implements
both RequestHandlerInterface
and MiddlewareInterface
for maximum flexibility.
Install
composer require northwoods/broker
Usage
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);
append(...$middleware)
Add one or more middleware to the end of the stack.
prepend(...$middleware)
Add one or more middleware to be beginning of the stack.
handle($request)
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.
process($request, $handler)
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.
Suggested Packages
- 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