windwalker / middleware
Windwalker Middleware package
Installs: 505
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Type:windwalker-package
Requires
- php: >=7.1.3
Requires (Dev)
- windwalker/http: ~3.0
- windwalker/test: ~3.0
Suggests
- psr/http-message: Install 1.* to support Psr7MiddlewareInterface.
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.23
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-test
This package is auto-updated.
Last update: 2024-12-18 15:21:48 UTC
README
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
.
{ "require": { "windwalker/middleware": "~3.0" } }
Getting Started
Basic Example
This is a simple way using middleware to wrap your logic.
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
$a = new TestA; $b = new TestB; $a->setNext($b); $b->setNext(new CallbackMiddleware( function($next) { echo ">>>CCCC\n"; echo "<<<CCCC\n"; } )); $a->call();
The result should be:
>>> AAAA
>>> BBBB
>>> CCCC
<<< CCCC
<<< BBBB
<<< AAAA
The CallbackMiddleware
support second argument as next in constructor:
$ware = new CallbackMiddleware( function($next) { echo ">>>CCCC\n"; $next->call(); echo "<<<CCCC\n"; }, new NextMiddleware )
End The Chaining
If a middleware call next, we have to make sure there are a next middleware exists, or we will return error.
class TestB extends Middleware { /** * call * * @return mixed */ public function 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
:
$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.
use Windwalker\Middleware\Chain\ChainBuilder; $chain = new ChainBuilder; $chain ->add('TestA') ->add(new TestB) ->add(function($next) { echo ">>>CCCC\n"; echo "<<<CCCC\n"; }); $chain->call();
The result still:
>>> AAAA
>>> BBBB
>>> CCCC
<<< CCCC
<<< BBBB
<<< AAAA
Psr7 Middleware
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);