2017 © Pedro Peláez
 

library pipeline

A Simple PHP 7 Pipeline class

image

ecfectus/pipeline

A Simple PHP 7 Pipeline class

  • Wednesday, February 14, 2018
  • by leemason
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Pipeline

Build Status, (*1)

A Simple PHP 7 Pipeline class., (*2)

The ecfectus pipeline is an un-opinionated sequential runner which takes pipes, and runs the given value(s) through each one, returning the end result., (*3)

Unlike other pipelines which are strict on number of arguments, the ecfectus pipeline will accept and return whatever you give to it., (*4)

If you give it 1 argument, that's what you get back, and the same is true for 6 arguments or more., (*5)

$pipeline new Ecfectus\Pipeline\Pipeline();
$pipeline->push(function($arg, callable $next){
    return $next($arg + 1);
});
$result = $pipeline(1);//$result = 2

$pipeline new Ecfectus\Pipeline\Pipeline();
$pipeline->push(function($arg, $arg2, callable $next){
    return $next($arg + 1, $arg2);
});
$result = $pipeline(1,3);//$result = [2, 3]

Whats more it will also tell you if the pipeline finished, or returned early from a pipe., (*6)

$pipeline new Ecfectus\Pipeline\Pipeline();
$pipeline->push(function($arg, callable $next){
    return $next($arg);
});
$pipeline->push(function($arg, callable $next){
    return $arg;
});
$pipeline->push(function($arg, callable $next){
    return $next($arg + 1);
});
$result = $pipeline(0);//$result = 0
$pipeline->finished();// == false as pipe 2 returned a result, not $next();

By design passed pipes MUST be callable and they are invoked in sequence. However that's not set in stone, you could if needed set your own resolver to resolve each pipe, perfect when using ioc containers, or to provide app specific syntax like Class@method., (*7)

$pipeline new Ecfectus\Pipeline\Pipeline();
$pipeline->setResolver(function($pipe){
    if(is_string($pipe)){
        //parse string, or fetch from ioc container
        $pipe = $someContainer->get($pipe);
    }
    if(is_callable($pipe)){
        return $pipe;
    }
    throw new InvalidArgumentException('Whoah, we could find that pipe!');
});

First or Last Argument Pipeline

As described above what you give the pipeline, and then return from the pipes is the result given to you when completed., (*8)

However this flexibility isn't always desired, for example if using the pipeline as a middleware runner with request and response objects, you only want back the response., (*9)

This is where the FirstArgumentPipeline or LastArgumentPipeline classes come in., (*10)

$pipeline new Ecfectus\Pipeline\FirstArgumentPipeline();
$pipeline->push(function($arg, $arg2, callable $next){
    return $next($arg + 1, $arg2);
});
$result = $pipeline(0,0);//$result = 1

$pipeline new Ecfectus\Pipeline\LastArgumentPipeline();
$pipeline->push(function($arg, $arg2, callable $next){
    return $next($arg + 1, $arg2 + 2);
});
$result = $pipeline(0,0);//$result = 2

The Versions

14/02 2018

dev-master

9999999-dev https://github.com/ecfectus/pipeline

A Simple PHP 7 Pipeline class

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Avatar leemason