windwalker/middleware
Composer 安装命令:
composer require windwalker/middleware
包简介
Windwalker Middleware package
README 文档
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);
windwalker/middleware 适用场景与选型建议
windwalker/middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 506 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「middleware」 「windwalker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 windwalker/middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 windwalker/middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 windwalker/middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Shoot aims to make providing data to your templates more manageable
Windwalker Queue package
Windwalker Promise package
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 506
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-2.0-or-later
- 更新时间: 2014-02-19