opxcore / pipeline
OpxCore 管道。
1.0.0
2021-03-03 14:34 UTC
Requires
- php: ^7.4
- opxcore/container-interface: ^1.0.5
Requires (Dev)
- opxcore/container: ^1.0.3
- phpunit/phpunit: ^9.5.2
This package is auto-updated.
Last update: 2024-09-29 05:56:22 UTC
README
管道是一个设计用于执行链式修改的类。
管道
"管道"是一个对传入值执行某些转换的类。
class Pipe { public function handle($passable, callable $next) { // here you can perform modifications of passable // and pass it to next pipe $modified = $next($passable); // and finally you can modify returned value here return $modified; } }
调用 $next()
函数将运行下一个修改。你可以通过不调用此函数而返回所需的结果来中断修改链。
用法
use OpxCore\Container\Container; use OpxCore\Pipeline\Pipeline; $container = new Container; $pipeline = new Pipeline($container); $result = $pipeline ->send('some value') ->through(['pipe_1', 'pipe_2']) ->via('handle') ->then(function ($passable){ return $passable; }) ->run();
这将通过 'pipe_1', 'pipe_2' 传递值,然后修改后的值将被传递到回调函数并以相同的方式返回。
管道可以是类名,因此容器必须设置为解析并创建它们(以及所有必需的依赖项)。另一种方式是传递已创建的类,这样你就不需要容器。