This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A simple pipeline implementation for PHP.

0.1.1 2016-07-07 13:18 UTC

This package is auto-updated.

Last update: 2021-11-29 02:11:34 UTC


Build Status Code Climate Test Coverage SensioLabsInsight

A simple Pipeline implementation for PHP.


This package enables you to send a value through a sequence of steps. Each step operates on the return value of the previous step. Without a pipeline you would end up with deeply nested function calls like this.


Using a pipeline you can transform the above example to this:


This makes the order of steps executed a lot clearer and gets rid of the annoying and ugly nesting of function calls.


use Sassnowski\Pipeline\Pipeline;

    ->through(function ($i) { return $i + 10; })
// 11

Building reusable pipelines

The through($fn) method does not change the existing Pipeline, but instead returns a new Pipeline instance that includes the next step. This enables you to reuse parts of a pipeline.

Since seeding the pipeline with an initial value goes against the idea of reusability, an additional method build() is defined. The build method does not initialize the pipeline with value. In this case the value has to be provided when calling the run method.

$pipeline1 = Pipeline::build()->through(function ($i) { return $i + 1 });

// Use the existing pipeline and simply add on additional steps.
$pipeline2 = $pipeline1->through(function ($i) { return $i * 10; });

// The initial pipeline remains unchanged.
$pipeline1->run(10); // 11

$pipeline2->run(10); // 110

Class-based steps

It is possible to use classes as steps instead of function. All the class has to do is implement the magic __invoke method.

class Add10
    function __invoke($i)
        return $i + 10;

// Somewhere else
    ->through(new Add10)
// 20

This is useful if the execution of a step requires a lot of additional business logic that does not belong inside the object containing the Pipeline.


If a step is added to the pipeline that fails the is_callable check an InvalidArgumentException will be thrown.

Pipeline::pipe(10)->through(10); // InvalidArgumentException

If the build method was used to create a pipeline, the initial value has to be passed to then run method. Failing to do so will result in a RuntimeException.

Pipeline::build()->through(function ($i) { return $i + 10; })->run() // RuntimeException

Method aliases

In order to enable a more easily readable syntax when building a pipeline several method aliases for the through method exist.

  • firstThrough()
  • andThen()