vitoreis / extend-caller
此包已被放弃,不再维护。未建议替代包。
PHP 中间件调用者
1.0.1
2023-08-24 11:15 UTC
Requires
- php: >=5.6
Requires (Dev)
This package is auto-updated.
Last update: 2024-07-31 00:18:19 UTC
README
灵活强大的 PHP 中间件调用者,支持队列中的多个中间件,具有上下文和持久数据。单元测试已在以下版本通过:5.6
、7.4
、8.1
、8.2
和 8.3
安装
composer require vitorsreis/extend-caller
用法
简单用法
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
多个中间件
您可以在队列中使用多个中间件,每个中间件的结果将作为参数传递给下一个中间件。
$caller = new Caller('ccc', 'ddd', ...); $caller->prepend('aaa', 'bbb'); $caller->append('eee', 'fff', ...); // queue: aaa -> bbb -> ccc -> ddd -> eee -> fff -> ...
上下文参数
上下文包含有关当前执行的完整信息。使用名称为 "$context" 的参数,省略类型或类型为 "mixed",或使用类型为 "\VSR\Extend\Caller\Context" 的任何名称在中间件或类的构造函数中,如果中间件是非静态类方法在任何位置以接收参数信息。
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
您可以在上下文中持久化数据,以便在未来的回调中持久化。
$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"
支持的中间件回调类型
- 原生函数名
$caller = new \VSR\Extend\Caller("\\stripos");
- 函数名
function callback($a, $b, $c = 3) { ... } $caller = new \VSR\Extend\Caller("\\callback");
- 匿名函数
$caller = new \VSR\Extend\Caller(function ($a, $b, $c = 3) { ... }); $caller = new \VSR\Extend\Caller(static function ($a, $b, $c = 3) { ... });
- 箭头函数,PHP 7.4+
$caller = new \VSR\Extend\Caller(fn($a, $b, $c = 3) => ...); $caller = new \VSR\Extend\Caller(static fn($a, $b, $c = 3) => ...);
- 变量函数
$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 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 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 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 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
- 匿名类,PHP 7+
$caller = new \VSR\Extend\Caller(new class { public function __invoke($a, $b, $c = 3) { ... } }); // Call __invoke