Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/equip/dispatch
An HTTP Interop compatible middleware dispatcher
https://github.com/equip/dispatch
Last synced: 2 months ago
JSON representation
An HTTP Interop compatible middleware dispatcher
- Host: GitHub
- URL: https://github.com/equip/dispatch
- Owner: equip
- License: mit
- Created: 2016-11-14T02:28:22.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-04-11T06:13:05.000Z (almost 4 years ago)
- Last Synced: 2024-09-18T01:36:49.569Z (4 months ago)
- Language: PHP
- Size: 36.1 KB
- Stars: 25
- Watchers: 11
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-psr15-middlewares - equip/dispatch - An HTTP Interop compatible middleware dispatcher. (Packages / Dispatcher)
README
Equip Dispatch
==============[![Latest Stable Version](https://img.shields.io/packagist/v/equip/dispatch.svg)](https://packagist.org/packages/equip/dispatch)
[![License](https://img.shields.io/packagist/l/equip/dispatch.svg)](https://github.com/equip/dispatch/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/equip/dispatch.svg)](https://travis-ci.org/equip/dispatch)
[![Code Coverage](https://scrutinizer-ci.com/g/equip/dispatch/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/equip/dispatch/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/equip/dispatch/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/equip/dispatch/?branch=master)An HTTP Interop compatible middleware dispatcher in [Equip](http://equip.github.io/).
Attempts to be [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/),
[PSR-4](http://www.php-fig.org/psr/psr-4/), [PSR-7](http://www.php-fig.org/psr/psr-7/),
and [PSR-15](http://www.php-fig.org/psr/psr-15/) compliant.Heavily influenced by the design of [Tari by ircmaxwell](https://github.com/ircmaxell/Tari-PHP).
For more information, see [the documentation](http://equipframework.readthedocs.org/en/latest/dispatch).
## Install
```
composer require equip/dispatch
```## Usage
The `MiddlewareCollection` is a container for middleware that acts as the entry point.
It takes two arguments:- An array of `$middleware` which must be instances of server middleware.
- A callable `$default` that acts as the terminator for the collection and returns
an empty response.Once the collection is prepared it can dispatched with a server request and will return
the response for output.### Example
```php
use Equip\Dispatch\MiddlewareCollection;// Any implementation of PSR-15 MiddlewareInterface
$middleware = [
new FooMiddleware(),
// ...
];// Default handler for end of collection
$default = function (ServerRequestInterface $request) {
// Any implementation of PSR-7 ResponseInterface
return new Response();
};$collection = new MiddlewareCollection($middleware);
// Any implementation of PSR-7 ServerRequestInterface
$request = ServerRequest::fromGlobals();
$response = $collection->dispatch($request, $default);
```### Nested Collections
The `MiddlewareCollection` also implements the `MiddlewareInterface` to allow
collections to be nested:```php
use Equip\Dispatch\MiddlewareCollection;// Any implementation of PSR-15 MiddlewareInterface
$middleware = [
new FooMiddleware(),// A nested collection
new MiddlewareCollection(...),// More middleware
new BarMiddleware(),
// ...
];$collection = new MiddlewareCollection($middleware);
// HTTP factories can also be used
$default = [$responseFactory, 'createResponse'];
$request = $serverRequestFactory->createRequest($_SERVER);$response = $collection->dispatch($request, $default);
```