PHP code example of andydune / pipeline

1. Go to this page and download the library: Download andydune/pipeline library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andydune / pipeline example snippets


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

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

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

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

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

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

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

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

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

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

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)';

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 

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);
    }
}

    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

    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();

    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


 
    $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


// 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();

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;
    }
}

php composer.phar 

php composer.phar update