Giter Site home page Giter Site logo

pipeline's Introduction

PHP Pipeline

Build Status Code Climate Test Coverage SensioLabsInsight

A simple Pipeline implementation for PHP.

Summary

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.

fn5(fn4(fn3(fn2(fn1($initialValue)))));

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

Pipeline::pipe($initialValue)
    ->through($fn1)
    ->through($fn2)
    ->through($fn3)
    ->through($fn4)
    ->through($fn5)
    ->run();

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

Usage

use Sassnowski\Pipeline\Pipeline;

Pipeline::pipe(10)
    ->through(function ($i) { return $i + 10; })
    ->run();
// 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
Pipeline::pipe(10)
    ->through(new Add10)
    ->run();
// 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.

Exceptions

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()
Pipeline::pipe(10)
    ->firstThrough($fn1)
    ->andThen($fn2)
    ->andThen($fn3)
    ->run();

License

MIT

pipeline's People

Contributors

ksassnowski avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.