waterloomatt / pipeline
A simple tool for step-by-step processing.
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-17 00:27:39 UTC
README
Based on Laravel's pipeline, this little titan performs step-by-step processing over an object - any object.
Installation
This package is currently in alpha and you should take care running it in anything beyond toy applications. That said, I have been using this pipeline class in production for well over a year without any issues.
composer require waterloomatt/pipeline:v0.0.1-alpha
Overview
Imagine an event happens in your system, like a CSV file is uploaded, and it triggers some actions,
- the file is validated (ex. size, extension, mime-type, etc.)
- file is processed (ex. parsed and extracted into a relational database)
- file is archived (ex. moved in a remote share)
- notifications need to be sent (ex. emails are sent to business users and the end-user)
You probably, have some objects that are responsible for each of these steps,
- a
$validator
- a
$processor
- an
$archiver
- a
$notifier
Good. And in a traditional OOP application, the calling code would look something like,
$validator->validate($file); $processor->process($file); $mover->move($file); $notifier->notify($file);
Good. Nothing wrong with that. That logic could live anywhere but in a traditional MVC application it'd probably live in a controller, model, or some auxiliary of those.
Now, how does the pipeline do it?
$pipes = [ $validator, $processor, $archiver, $notifier ]; (new Pipeline()) ->send($file) ->through($pipes) ->thenReturn();
Yip. It is that simple. Let's look at some examples. We'll start easy and work our way up.
Pipes can be closures
$pipes = [ // Multiply by 10 function ($input, $next) { $input = $input * 10; return $next($input); }, // Divide by 5 function ($input, $next) { $input = $input / 5; return $next($input); }, // Add 1 function ($input, $next) { $input = $input + 1; return $next($input); }, ]; $output = (new Pipeline()) ->send(10) // Start with 10 ->through($pipes) // Multiply by 10, divide by 5, add 1 ->thenReturn(); // Output: 21
Pipes can be classes
class Validate { public function handle(File $file, Closure $next) { // ... business logic ... return $next($file); } } class Process { public function handle(File $file, Closure $next) { // ... business logic ... return $next($file); } } class Archive { public function handle(File $file, Closure $next) { // ... business logic ... return $next($file); } } class Notify { public function handle(File $file, Closure $next) { // ... business logic ... return $next($file); } } $pipes = [ new Validate(), new Process(), new Archive(), new Notify() ]; (new Pipeline()) ->send(new File()) // Start with a file ->through($pipes) // validate, process, archive, notify ->thenReturn();