vitorsreis/extend-caller
最新稳定版本:1.0.1
Composer 安装命令:
composer require vitorsreis/extend-caller
包简介
Middleware caller for PHP
README 文档
README
Flexible and powerful middleware caller for PHP, supporting multiple middlewares in a queue with contexts and persistent data
Unit tests have passed on versions: 5.6, 7.4, 8.1, 8.2 and 8.3
Install
composer require vitorsreis/extend-caller
Usage
Simple usage
use VSR\Extend\Caller; $caller = new Caller(static function ($aaa, $bbb, $ccc = 3) { return "$aaa:$bbb:$ccc"; }); echo $caller->execute([1, 2, /* 3 */]); // output: 1:2:3 echo $caller->execute(['aaa' => 1, 'bbb' => 2, /* 'ccc' => 3 */]); // output: 1:2:3
Multiple middlewares
You can use multiple middlewares in queue, the result of each middleware is passed as argument to next middleware.
$caller = new Caller('ccc', 'ddd', ...); $caller->prepend('aaa', 'bbb'); $caller->append('eee', 'fff', ...); // queue: aaa -> bbb -> ccc -> ddd -> eee -> fff -> ...
Context param
Context contains all information about the current execution. Use an argument with the name "$context" with an omitted type or of type "mixed," or use any name with the type "\VSR\Extend\Caller\Context" in middlewares or in the constructor of a class if the middleware is a non-static class method at any position to receive the argument information.
use VSR\Extend\Caller\Context; new Caller(function ($context) { ... }); new Caller(function (mixed $context) { ... }); # Explicit mixed type only PHP 8+ new Caller(function (Context $context) { ... }); new Caller(function (Context $custom_name_context) { ... }); new Caller(new class { public function __construct($context) { ... } public function __invoke($context) { ... } }); // Call __invoke
You can persist data in context so that it is persisted in future callbacks.
$caller = new Caller(); $caller->append(static function (Context $context) { $context->set('xxx', $context->get('xxx', 0) + 10); # 2. Increment value: 5 + 10 = 15 }); $caller->append(static function ($context) { $context->set('xxx', $context->get('xxx', 0) + 15); # 3. Increment value: 15 + 15 = 30 }); $caller->context()->set('xxx', 5); # 1. Initial value: 5 $context = $caller->execute(); echo $context->get('xxx'); // output: "30"
Supported middleware callback types
- Native function name
$caller = new \VSR\Extend\Caller("\\stripos");
- Function name
function callback($a, $b, $c = 3) { ... } $caller = new \VSR\Extend\Caller("\\callback");
- Anonymous function
$caller = new \VSR\Extend\Caller(function ($a, $b, $c = 3) { ... }); $caller = new \VSR\Extend\Caller(static function ($a, $b, $c = 3) { ... });
- Arrow function, PHP 7.4+
$caller = new \VSR\Extend\Caller(fn($a, $b, $c = 3) => ...); $caller = new \VSR\Extend\Caller(static fn($a, $b, $c = 3) => ...);
- Variable function
$callback = function ($a, $b, $c = 3) { ... }; $caller = new \VSR\Extend\Caller($callback); $callback = static function ($a, $b, $c = 3) { ... }; $caller = new \VSR\Extend\Caller($callback);
- Class method
class AAA { public function method($a, $b, $c = 3) { ... } } $aaa = new AAA(); $caller = new \VSR\Extend\Caller("AAA::method"); // Call first constructor if exists and then method $caller = new \VSR\Extend\Caller([ AAA::class, 'method' ]); // Call first constructor if exists and then method $caller = new \VSR\Extend\Caller([ new AAA(), 'method' ]); // Call method $caller = new \VSR\Extend\Caller([ $aaa, 'method' ]); // Call method
- Class static method
class BBB { public static function method($a, $b, $c = 3) { ... } } $bbb = new BBB(); $caller = new \VSR\Extend\Caller("BBB::method"); // Call static method $caller = new \VSR\Extend\Caller([ BBB::class, 'method' ]); // Call static method $caller = new \VSR\Extend\Caller([ new BBB(), 'method' ]); // Call static method $caller = new \VSR\Extend\Caller([ $bbb, 'method' ]); // Call static method
- Class method with constructor
class CCC { public function __construct($d, $e, $f = 6) { ... } public function method($a, $b, $c = 3) { ... } } $caller = new \VSR\Extend\Caller("CCC::method"); // Call first constructor and then method $caller = new \VSR\Extend\Caller([ CCC::class, "method" ]); // Call first constructor and then method
- Class name/object
class DDD { public function __invoke($a, $b, $c = 3) { ... } } $ddd = new DDD(); $caller = new \VSR\Extend\Caller("DDD"); // Call first constructor if exists and then __invoke $caller = new \VSR\Extend\Caller(DDD::class); // Call first constructor if exists and then __invoke $caller = new \VSR\Extend\Caller(new DDD()); // Call __invoke $caller = new \VSR\Extend\Caller($ddd); // Call __invoke
- Anonymous class, PHP 7+
$caller = new \VSR\Extend\Caller(new class { public function __invoke($a, $b, $c = 3) { ... } }); // Call __invoke
vitorsreis/extend-caller 适用场景与选型建议
vitorsreis/extend-caller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「middleware」 「callback」 「caller」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vitorsreis/extend-caller 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vitorsreis/extend-caller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vitorsreis/extend-caller 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
Show a cli spinner while executing a callback
Pleasantly work with asynchronous code.
Slim Framework 3 CSRF protection middleware utilities
统计信息
- 总下载量: 53
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-15