2017 © Pedro Peláez
 

library pipeline

Provides a pipeline pattern. Based on middleware approach.

image

andydune/pipeline

Provides a pipeline pattern. Based on middleware approach.

  • Tuesday, June 19, 2018
  • by AndyDune
  • Repository
  • 1 Watchers
  • 3 Stars
  • 51 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 6 % Grown

The README.md

AndyDune\Pipeline

Build Status Software License Packagist Version Total Downloads, (*1)

This package provides a pipeline pattern implementation. It is based on middleware approach., (*2)

Requirements

PHP version >= 7.1, (*3)

For php 5.6 use version 1:, (*4)

"require" : {
     "andydune/pipeline": "1.*"
}

Installation

Installation using composer:, (*5)

composer require andydune/pipeline 

Or if composer was not installed globally:, (*6)

php composer.phar require andydune/pipeline

Or edit your composer.json:, (*7)

"require" : {
     "andydune/pipeline": "^2"
}

And execute command:, (*8)

php composer.phar update

Usage

Operations in pipeline stages, can be anything that satisfies the callable type-hint. So closures and anything that's invokable is good., (*9)

use AndyDune\Pipeline\Pipeline;

$pipeline = new Pipeline();

$stages = [
    function($contest, $next) {
        $contest += 100;
        return $next($contest);    
    },
    function($contest, $next) {
        $contest += 10;
        return $next($contest);    
    }
];
$result = $pipeline->through($stages)->send(1)
->then(function ($context) {
            $context += 1000;
            return $context;});
$result // 1111

Types of stages

Basically each stage can be Closure:, (*10)

$pipeline = new Pipeline();
$pipeline->pipe(function ($context, $next) {
    $context['closure'] = 'was here';
    return $next($context); 
});

It can be an instance of class with callable interface:, (*11)

$instance = new class() {
    public function __invoke($context, $next) 
    {
        $context['invoke'] = 'was here';
        return $next($context); 
    }
}

$pipeline = new Pipeline();
$pipeline->pipe($instance);

It can be an instance of class with any method:, (*12)

$instance = new class() {
    public function doIt($context, $next) 
    {
        $context['invoke'] = 'was here';
        return $next($context); 
    }
}

$pipeline = new Pipeline();
$pipeline->pipe($instance, 'doIt');

It can be a class name with method __invoke or any method you describe:, (*13)

class Trim
{
    public function __invoke($context, $next) 
    {
        $context['class_invoke'] = 'was here';
        return $next($context); 
    }
}

$pipeline = new Pipeline();
$pipeline->pipe(Trim::class);

It can be a class name with any method you describe:, (*14)

class Trim
{
    public function handle($context, $next) 
    {
        $context['class_invoke'] = 'was here';
        return $next($context); 
    }
}

$pipeline = new Pipeline();
$pipeline->pipe(Trim::class,  'handle');

Use object for stage without middleware interface

You can use methods which don't execute $next function. It gets some data and return results. There is special method: 'pipeForContainer', (*15)

Example class., (*16)

namespace AndyDune\Pipeline\Example;

class Methods
{
    // return result - no calling next() 
    public function addBraceLeft($string)
    {
        return '(' . $string;
    }

    // It has middleware interface
    public function addBraceRight($string, callable $next)
    {
        $string =  $string . ')';
        return $next($string);
    }
}

$instance = new Methods(); $pipeline = new Pipeline(); $pipeline->send('puh'); $pipeline->pipeForContainer($instance, 'addBraceLeft'); $pipeline->pipe(Methods::class, 'addBraceRight'); $result = $pipeline->execute(); $result == '(puh)';

Container provider (service provider)

String can de passed as pipeline stage. By default it is a class name. It's make possible by using default provider: AndyDune\Pipeline\PipeIsClassName
You can set your own provider, with injections you need. Your class must implements interface: Interop\Container\ContainerInterface, (*17)

If was not found stage with your provider system will try to use default provider. There is stack of providers., (*18)

Use your provider as parameter for pipeline constructor:, (*19)

use Interop\Container\ContainerInterface;

class SpecialPipelineContainer implements ContainerInterface
{
    /**
     * @var  \Rzn\ObjectBuilder
     */
    protected $objectBuilder;

    public function get($name)
    {
        /*
        do anything to build object using $name
        */
        return $object;
    }

    public function has($name)
    {
        /*
        do anything to check required object can be build  
        */
        return $exist;
    }
}

$container = new SpecialPipelineContainer();
$pipeLine = new Pipeline($container);

$pipeLine->pipe('any string to get stage from container');

Additional parameters for stage

Sometimes you need to pass any number of additional parameters to pipe stage during description. It needs for test and it very good for more flexibility of stage. It increases reuse of class., (*20)

Lets look at the example. It is very simple example., (*21)

Here is example class for stage:, (*22)

namespace AndyDune\Pipeline\Example;
class PowerOfNumber
{
    public function __invoke($data, callable $next, $power = 2)
    {
        if (is_array($power)) {
            array_walk($power, function (&$value, $key) use ($data) {
                $value = pow($data, $value);
            });
            return $next($power);
        }
        $data = $this->handle($data, $power);
        return $next($data);
    }
    protected function handle($number, $power)
    {
        return pow($number, $power);
    }
}

Lets use it:, (*23)

    use use AndyDune\Pipeline\Pipeline;
    use AndyDune\Pipeline\Example;

    $pipeline = new Pipeline();
    $pipeline->send(2);
    $pipeline->pipe(PowerOfNumber::class);
    $result = $pipeline->execute(); // == 4

    $pipeline = new Pipeline();
    $pipeline->send(2);
    $pipeline->pipe(PowerOfNumber::class, null, 3);
    $result = $pipeline->execute(); // == 8

    $pipeline = new Pipeline();
    $pipeline->send(2);
    $pipeline->pipe(PowerOfNumber::class, null, 4);
    $result = $pipeline->execute(); // == 16

Dependency injection

You can use default pipeline stage creator with injection into stage objects services or any objects from outside., (*24)

There is injection with interfaces., (*25)

    use use AndyDune\Pipeline\Pipeline;

    $pipeline = new Pipeline();

    $pipeline->addInitializer(function($stageObject) use ($someService) {
        if ($stageObject instanceof SomeServiceAwareInterface) {
            $stageObject->setSomeService($someService)
        }     
    });

    $pipeline->pipe(ClassHaveInterface::class);
    $result = $pipeline->execute();

This use method addInitializer which receive callable parameter., (*26)

Exceptions

Package has not integrated exception catch support. It is simple for you to include exception try-catch block into one of pipeline stages., (*27)

    use AndyDune\Pipeline\Pipeline;
    $pipeline = new Pipeline();
    $pipeline->send(['zub' => 'kovoy']);
    $pipeline->pipe(function ($context, $next) {
        try {
            return $next($context);
        } catch (Exception $e) {
            $context['exception'] = 'caught';
        }
        return $context;
    });

    $pipeline->pipe(function ($context, $next) {
        $context['action'] = 'before_exception';
        throw new Exception();
        return $next($context); // it will be never execute
    });

    // This stage will never be executed
    $pipeline->pipe(function ($context, $next) {
        $context['after_exception'] = 'ignored';
        return $next($context);
    });

    $result = $pipeline->execute();
    array_key_exists('zub', $result);       // true
    array_key_exists('exception', $result); // true
    array_key_exists('action', $result);    // false
    array_key_exists('after_exception', $result); // false

There is a class you may use as stage for your pipeline for catch exception in this package., (*28)


$pipeline = new Pipeline(); $pipeline->send(['zub' => 'kovoy']); $pipeline->pipe(AndyDune\Pipeline\Stage\ExceptionCatch::class); $pipeline->pipe(function ($context, $next) { $context['action'] = 'before_exception'; throw new Exception('jump'); }); $result = $pipeline->execute(); $result instancheof \Exception // true

Examples

Caching

You have service for retrieve data. And you don't need to change its code. Just use it as a stage in the pipeline., (*29)


// Description $pipeline = new Pipeline(); $pipeline->pipe(function($key, $next) { /* * Cache adapter with (PSR-16) interface * Ones a have used Symfony Cache Component [https://github.com/symfony/cache] * It's for example */ $cache = new FilesystemCache(); if ($cache->has($key)) { return $cache->get($key); } $data = $next($catId); $cache->set($key, $data); return $data; }); $pipeline->pipe(DataRetrieveClass::class, 'getImportantData'); // Execute $results = $pipeline->send($key)->execute();

Usage within Zend FW 3

By default string as stage for pipeline means name of class. If you create pipeline object without parameters service container implements AndyDune\Pipeline\PipeIsClassName. It only creates instance of given class and return it. If this package is used as part of of Zend FW 3 you can use Zend's services for retrieve instances., (*30)

First you must install package with composer. Then copy file vendor/andydune/pipeline/config/pipeline.global.php to config/autoload/pipeline.global.php, (*31)

Description fot your pipeline in factory:, (*32)

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class DunhillFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $pipeline = $container->get('pipeline');
        $pipeline->pipe('service name within Zend3');
        $pipeline->pipe(function($data, $next) {
            return $next($data); 
        });
        return $pipeline;
    }
}

If you don't work with Zend services use pipeline directly., (*33)

Analogues

Laravel pipeline - it do very the same I wanted, but I need more tools for testing purpose. And I don't like laraver service manager. it has not common interface. My pipeline may use service managers based on container-interop with PSR-11 interfaces., (*34)

League\Pipeline - it has good documentation. But has bad feature - poor workflow control. To stop execution it need to use exception., (*35)

The Versions

19/06 2018

dev-master

9999999-dev https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

06/04 2018

v1.5.0

1.5.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

05/03 2018

v1.4.0

1.4.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

24/01 2018

v1.3.1

1.3.1.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

11/12 2017

v1.3.0

1.3.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

16/10 2017

v1.2.0

1.2.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

10/10 2017

v1.1.1

1.1.1.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

09/10 2017

v1.1.0

1.1.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

05/10 2017

v1.0.0

1.0.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

02/10 2017

v0.8.1

0.8.1.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

28/09 2017

v0.8.0

0.8.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

28/09 2017

v0.7.1

0.7.1.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

28/09 2017

v0.7.0

0.7.0.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline

26/09 2017

v0.6.1

0.6.1.0 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline pattern

22/09 2017

dev-add-code-of-conduct-1

dev-add-code-of-conduct-1 https://github.com/AndyDune/Pipeline

Provides a pipeline pattern. Based on middleware approach.

  Sources   Download

MIT

The Requires

 

The Development Requires

middleware php pipeline pattern

21/09 2017

v0.6.0

0.6.0.0 https://github.com/AndyDune/Pipeline

provides a pipeline patter

  Sources   Download

MIT

The Requires

 

The Development Requires

php pipeline pattern

21/09 2017

v0.5

0.5.0.0 https://github.com/AndyDune/Pipeline

provides a pipeline patter

  Sources   Download

MIT

The Requires

 

The Development Requires

php pipeline pattern