Wallogit.com
2017 © Pedro Peláez
Simple library to send data through a chain of tasks
Pipeline allows to easily chain operations / tasks on the fly or create a reusable chain of commands. Complete documentation is available., (*2)
composer require gnumast/pipeline
Here's a trivial example., (*3)
class MakeAllCaps implements TaskInterface {
public function run($data) {
return mb_strtoupper($data);
}
}
class RemoveAllSpaces implements TaskInterface {
public function run($data) {
return str_replace(' ', '', $data);
}
}
$pipeline = new Pipeline(
new MakeAllCaps(),
new RemoveAllSpaces()
);
$pipeline->execute("Hello, my name is Steve"); // HELLO,MYNAMEISSTEVE
For simple chains where defining a brand new class isn't really worth it, or if you quickly want to chain things
together, the CallablePipe class wraps anonymous functions to be passed as pipes., (*4)
$pipeline = new Pipeline(
new CallablePipe(function($data) {
return $data * 10;
}),
new CallablePipe(function($data) {
return $data + 50;
})
);
$result = $pipeline->execute(10); // 150
You don't have to pass all of your tasks at initialisation time. Pipeline provides an add method to add steps
to an existing object:, (*5)
$pipeline = new Pipeline(new MakeAllCaps());
$pipeline->add(new RemoveAllSpaces());
$pipeline->execute("Hello, world!"); // HELLO,WORLD!