ztsu/pipe
Provides a simple implemenation of a pipeline pattern., (*1)
Requirements
Supports PHP starting with version 5.4., (*2)
Installation
composer require ztsu/pipe
Usage
Here is a basic usage example:, (*3)
use Ztsu\Pipe\Pipeline;
$a = function ($payload, $next) {
return $next($payload . "a");
};
$b = function ($payload, $next) {
return $next($payload . "b");
};
$pipeline = new Pipeline;
$pipeline->add($a);
$pipeline->add($b);
echo $pipeline->run(""); // "ab"
Here $a and $b are callables with two arguments. First is for accumulating a payload from previous stages.
Second is for continuing next stages in a pipeline., (*4)
For break pipeline just return $payload instead of call $next:, (*5)
$pipeline = new Pipeline;
$break = function ($payload, $next) {
return $payload;
};
$pipeline->add($a);
$pipeline->add($break);
$pipeline->add($b);
echo $pipeline(""); // "a"
A pipeline is callable too. And it's able to use as a stage., (*6)
For this just add it to another pipeline:, (*7)
$pipeline = new Pipeline;
$pipeline->add($a);
$pipeline->add($bc);
echo $pipeline(""); // "abc"
If use pipeline with a break as a stage it breaks entire pipeline., (*8)
License
MIT., (*9)