https://github.com/northwoods/broker
Dead simple PSR-15 middleware dispatcher
https://github.com/northwoods/broker
dispatcher http middleware psr-15 server
Last synced: about 1 month ago
JSON representation
Dead simple PSR-15 middleware dispatcher
- Host: GitHub
- URL: https://github.com/northwoods/broker
- Owner: northwoods
- License: mit
- Created: 2017-07-21T19:41:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-17T16:14:42.000Z (over 7 years ago)
- Last Synced: 2025-04-01T15:19:52.251Z (11 months ago)
- Topics: dispatcher, http, middleware, psr-15, server
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 26
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-psr15-middlewares - northwoods/broker - A conditional PSR-15 dispatcher that supports lazy middleware resolution via container (Packages / Dispatcher)
README
Northwoods Broker
=================
[](https://www.patreon.com/shadowhand)
[](https://packagist.org/packages/northwoods/broker)
[](https://github.com/northwoods/broker/blob/master/LICENSE)
[](https://travis-ci.com/northwoods/broker)
[](https://scrutinizer-ci.com/g/northwoods/broker/?branch=master)
[](https://scrutinizer-ci.com/g/northwoods/broker/?branch=master)
Broker is a dead simple [PSR-15][psr-15] middleware dispatcher. Broker implements
both `RequestHandlerInterface` and `MiddlewareInterface` for maximum flexibility.
[psr-15]: http://www.php-fig.org/psr/psr-15/
## Install
```
composer require northwoods/broker
```
## Usage
```php
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](https://github.com/northwoods/conditional-middleware)
- Lazy middleware instantiation can be provided by [northwoods/lazy-middleware](https://github.com/northwoods/lazy-middleware)
- Response sending can be provided by [http-interop/response-sender](https://github.com/http-interop/response-sender)