baagee/php-onion
Composer 安装命令:
composer require baagee/php-onion
包简介
PHP onion layer
README 文档
README
php onion layer
可以利用它来做中间件,action之前做请求拦截,验证登陆,权限,或者action之后的一些逻辑
图示例:
图片源至koa的官方图
使用示例:
首先定义要经过层
/** * Class ReturnJson */ class ReturnJson extends \BaAGee\Onion\Base\LayerAbstract { /** * @param Closure $next * @param $data * @return mixed|void */ protected function handler(Closure $next, $data) { print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL; $ret = $next($data); print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL; echo json_encode($ret, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL; } } /** * Class CatchError */ class CatchError extends \BaAGee\Onion\Base\LayerAbstract { /** * @param Closure $next * @param $data * @return array|mixed */ protected function handler(Closure $next, $data) { print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL; $ret = [ 'code' => 0, 'message' => '' ]; try { $data .= __CLASS__ . '; '; $ret['data'] = $next($data); } catch (Throwable $e) { $ret = [ 'code' => $e->getCode(), 'message' => $e->getMessage() ]; } print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL; $ret['request_id'] = time(); return $ret; } } /** * Class BLogic */ class BLogic extends \BaAGee\Onion\Base\LayerAbstract { /** * @param Closure $next * @param $data * @return mixed */ protected function handler(\Closure $next, $data) { print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL; $data .= __CLASS__ . '; '; $ret = $next($data); print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL; return $ret; } } /** * Class ALogic */ class ALogic extends \BaAGee\Onion\Base\LayerAbstract { /** * @param Closure $next * @param $data * @return mixed */ protected function handler(Closure $next, $data) { print "开始 " . __CLASS__ . " 逻辑" . PHP_EOL; $data .= __CLASS__ . "; "; // throw new Exception(__CLASS__ . ' Exception', 100); $ret = $next($data); print "结束 " . __CLASS__ . " 逻辑" . PHP_EOL; return $ret; } }
使用
// 输入的数据 $input = 'input data; '; // 经过的层 注意顺序,从前往后执行,然后从后往前一层一层返回 $through = [ ReturnJson::class, CatchError::class, ALogic::class, BLogic::class ]; // 开始 (new \BaAGee\Onion\Onion())->send($input)->through($through)->then(function ($input) { return [ 'time' => time(), 'input' => $input ]; });
运行结果
开始 ReturnJson 逻辑
开始 CatchError 逻辑
开始 ALogic 逻辑
开始 BLogic 逻辑
结束 BLogic 逻辑
结束 ALogic 逻辑
结束 CatchError 逻辑
结束 ReturnJson 逻辑
{
"code": 0,
"message": "",
"data": {
"time": 1553657337,
"input": "input data; CatchError; ALogic; BLogic; "
},
"request_id": 1553657337
}
baagee/php-onion 适用场景与选型建议
baagee/php-onion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「onion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 baagee/php-onion 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 baagee/php-onion 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 baagee/php-onion 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Guzzle middleware allows to use Tor client as a proxy
A layering mechanism for PHP applications.
Alfabank REST API integration
Onion style PSR-7 middleware stack using generators
Yii 2 Onion architecture Project Template
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
统计信息
- 总下载量: 45
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-27