beebmx / pipeline
Pipeline your code
Fund package maintenance!
Requires
- php: ^8.4
Requires (Dev)
- laravel/pint: ^1.16
- pestphp/pest: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Pipeline
Package inspired by Laravel Pipeline, but without Laravel Container
Installation
Install using composer:
composer require beebmx/pipeline
Usage
The basic use of pipeline is:
use Beebmx\Pipeline\Pipeline; $string = (new Pipeline) ->send('say') ->through([ function($string, $next) { $string = $string.' hello'; return $next($string); }, function($string, $next) { $string = $string.' world'; return $next($string); } ])->execute(); //$string will be 'say hello world'
Important
You should always call the $next callback with the pipe variable as argument
for the next pipe or final result.
You can use a class instead of a Closure if that better suits your needs:
use Beebmx\Pipeline\Pipeline; class MyOwnPipe { public function handle($myPipeObject, Closure $next) { $myPipeObject->process(); return $next($myPipeObject); } } $result = (new Pipeline) ->send($someObject) ->through([ MyOwnPipe::class, OtherPipe::class, ])->execute();
Note
By default Pipeline will trigger the handle method.
If you need to change the default handle method in your pipe classes, you can do it like:
use Beebmx\Pipeline\Pipeline; $result = (new Pipeline) ->send($someObject) ->via('myOwnMethod') ->through([ MyOwnPipe::class, OtherPipe::class, ])->execute();
At the end of the pipe's flow, you can finish with the processing like:
use Beebmx\Pipeline\Pipeline; $string = (new Pipeline) ->send('say') ->through(function($string, $next) { $string = $string.' hello'; return $next($string); })->then(function($string) { $string = $string.' world'; return $string; }); //$string will be 'say hello world'
You can add more pipes to the pipeline flow:
use Beebmx\Pipeline\Pipeline; $string = (new Pipeline) ->send('say') ->through(function($string, $next) { $string = $string.' hello'; return $next($string); })->pipe(function($string, $next) { $string = $string.' world'; return $next($string); })->execute(); //$string will be 'say hello world'
Both through and pipe accept an array of pipes or multiple pipes as arguments:
use Beebmx\Pipeline\Pipeline; $result = (new Pipeline) ->send($someObject) ->through(MyOwnPipe::class, OtherPipe::class) ->execute();
Pipe resolution
Each pipe is resolved in the following order:
- Callables (closures, invokable objects, and strings that are callable) are called directly with
($passable, $next). - Strings are treated as a class name (optionally with parameters, like
MyOwnPipe::class.':one,two'), instantiated without constructor arguments, and theirhandlemethod (or the one set withvia) is called. - Objects have their
handlemethod (or the one set withvia) called.
Note
Because callables are resolved first, an object with an __invoke method will always be invoked,
even if it also has the method set with via.
Warning
A string that matches the name of a PHP function (like 'trim' or 'system') is called as that function,
and any other string is instantiated as a class. Never build pipe names from user input.
Testing
composer test
Credits
- Original repository illuminate/pipeline
- Fernando Gutierrez @beebmx
- All Contributors