https://github.com/phoole/middleware
Slim and simple PSR-15 compliant middleware runner library for PHP
https://github.com/phoole/middleware
middleware phoole php psr-15 psr-7 swoole
Last synced: 5 months ago
JSON representation
Slim and simple PSR-15 compliant middleware runner library for PHP
- Host: GitHub
- URL: https://github.com/phoole/middleware
- Owner: phoole
- License: apache-2.0
- Created: 2019-10-17T08:19:16.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-19T06:47:59.000Z (over 6 years ago)
- Last Synced: 2025-09-22T22:41:40.962Z (9 months ago)
- Topics: middleware, phoole, php, psr-15, psr-7, swoole
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# middleware
[](https://travis-ci.com/phoole/middleware)
[](https://scrutinizer-ci.com/g/phoole/middleware/?branch=master)
[](https://codeclimate.com/github/phoole/middleware)
[](https://packagist.org/packages/phoole/middleware)
[](https://packagist.org/packages/phoole/middleware)
[]()
Slim and simple PSR-15 compliant middleware runner library for PHP.
Installation
---
Install via the `composer` utility.
```bash
composer require "phoole/middleware"
```
or add the following lines to your `composer.json`
```json
{
"require": {
"phoole/middleware": "1.*"
}
}
```
- Able to use PSR-15 compliant middlewares out there.
- Able to use a middleware [queue](#queue) (a group of middlewares) as a
generic middleware in another(or the main) queue.
Usage
---
Create the middleware queue, then process all the middlewares.
```php
use Phoole\Middleware\Queue;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
// create middleware queue with 404 response as default(last)
$mws = new Queue(new Response(404));
// add middlewares
$mws->add(
new LoggerMiddleware(),
new DispatcherMiddleware()
);
// process the request with middlewares
$response = $mws->handle(new ServerRequest('GET', 'http://bingo.com/get'));
```
Advanced
---
`Phoole\Middleware\Queue` implements the `Psr\Http\Server\MiddlewareInterface`,
so the queue itself can be used as a generic middleware.
```php
// subqueue may need no default response if not the last in the main queue
$subQueue = (new Queue())->add(
new ResponseTimeMiddleware(),
new LoggingMiddleware()
);
// main middleware queue
$mws = (new Queue(new Response(404)))->add(
$subQueue,
new DispatcherMiddleware()
);
```
Testing
---
```bash
$ composer test
```
Dependencies
---
- PHP >= 7.2.0
- phoole/base 1.*
- A PSR-7 HTTP message implementation, such as [guzzle/psr7](https://github.com/guzzle/psr7).
License
---
- [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)