https://github.com/windwalker-io/middleware
[READ ONLY] PSR-7 Middleware base classes.
https://github.com/windwalker-io/middleware
middleware psr7-http psr7-middleware
Last synced: 3 months ago
JSON representation
[READ ONLY] PSR-7 Middleware base classes.
- Host: GitHub
- URL: https://github.com/windwalker-io/middleware
- Owner: windwalker-io
- Created: 2014-02-18T06:13:09.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-02-18T07:02:32.000Z (over 5 years ago)
- Last Synced: 2025-01-02T06:44:53.203Z (over 1 year ago)
- Topics: middleware, psr7-http, psr7-middleware
- Language: PHP
- Homepage: https://github.com/ventoviro/windwalker
- Size: 67.4 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Windwalker Middleware
Windwalker Middleware is a simple & elegant PHP Middleware library help you integrating middleware pattern in your project.
## Installation via Composer
Add this to the require block in your `composer.json`.
``` json
{
"require": {
"windwalker/middleware": "~3.0"
}
}
```
## Getting Started
### Basic Example
This is a simple way using middleware to wrap your logic.
``` php
use Windwalker\Middleware\CallbackMiddleware;
use Windwalker\Middleware\AbstractMiddleware;
class TestA extends AbstractMiddleware
{
/**
* call
*
* @return mixed
*/
public function call()
{
echo ">>> AAAA\n";
$this->next->call();
echo "<<< AAAA\n";
}
}
class TestB extends AbstractMiddleware
{
/**
* call
*
* @return mixed
*/
public function call()
{
echo ">>> BBBB\n";
$this->next->call();
echo "<<< BBBB\n";
}
}
$a = new TestA;
$a->setNext(new TestB);
$a->call();
```
The result should be:
```
>>> AAAA
>>> BBBB
<<< BBBB
<<< AAAA
```
### Callback Middleware
If you don't want to create a class, you want to set a middleware in runtime, using `CallbackMiddleware`
``` php
$a = new TestA;
$b = new TestB;
$a->setNext($b);
$b->setNext(new CallbackMiddleware(
function($next)
{
echo ">>>CCCC\n";
echo "<<call();
```
The result should be:
```
>>> AAAA
>>> BBBB
>>> CCCC
<<< CCCC
<<< BBBB
<<< AAAA
```
The `CallbackMiddleware` support second argument as next in constructor:
``` php
$ware = new CallbackMiddleware(
function($next)
{
echo ">>>CCCC\n";
$next->call();
echo "<<>> BBBB\n";
$this->next->call();
echo "<<< BBBB\n";
}
}
$b = new TestB;
$b->call();
// Error, next not exists.
```
But yes we can set a blackhole middleware in the last element, that will do nothing when previous class call it, using `EndMiddleware`:
``` php
$b = new TestB;
$b->setNext(new EndMiddleware);
$b->call();
```
The result still like below:
```
>>> BBBB
<<< BBBB
```
## Chaining Builder
We can using `ChainBuilder` to chaining multiple middlewares.
``` php
use Windwalker\Middleware\Chain\ChainBuilder;
$chain = new ChainBuilder;
$chain
->add('TestA')
->add(new TestB)
->add(function($next)
{
echo ">>>CCCC\n";
echo "<<call();
```
The result still:
```
>>> AAAA
>>> BBBB
>>> CCCC
<<< CCCC
<<< BBBB
<<< AAAA
```
## Psr7 Middleware
``` php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Windwalker\Middleware\Chain\Psr7ChainBuilder;
use Windwalker\Middleware\Psr7Middleware;
class MyPsr7Middleware implements \Windwalker\Middleware\Psr7InvokableInterface
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
{
// Do something
$result = $next($request, $response);
// Do something
return $result;
}
}
$mid = new Psr7Middleware(function (ServerRequestInterface $request, ResponseInterface $response, $next = null)
{
// Do something
});
$chain = new Psr7ChainBuilder;
$chain->add(new MyPsr7Middleware)
->add($mid);
$chain->execute(new ServerRequest, new Response);
```