beebmx/pipeline

Pipeline your code

1.0.0 2024-06-19 17:29 UTC

This package is auto-updated.

Last update: 2024-08-19 17:54:21 UTC


README

Build Status Total Downloads Latest Stable Version License

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 insted of a Closure if that more your need:

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 triger 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 and add more pipes to the pipeline flow:

use Beebmx\Pipeline\Pipeline;

use Beebmx\Pipeline\Pipeline;

$string = (new Pipeline)
    ->send('say')
    ->through(function($string, $next) {
        $string = $string.' hello';

        return $next($string);
    })->pipe(function($string) {
        $string = $string.' world';

        return $string;
    })->execute();

//$string will be 'say hello world'

Testing

composer test

Credits