PHP code example of b2pweb / bdf-pipeline

1. Go to this page and download the library: Download b2pweb/bdf-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/ */

    

b2pweb / bdf-pipeline example snippets


use Bdf\Pipeline\Pipeline;
use Bdf\Pipeline\CallableFactory\LinkedCallableFactory;

$pipeline = new Pipeline(new LinkedCallableFactory());
$pipeline->pipe(function($value) {
    return $value + 2;
});

// Returns 12
$pipeline->send(10);

use Bdf\Pipeline\Pipeline;
use Bdf\Pipeline\CallableFactory\StackCallableFactory;

$pipeline = new Pipeline(new StackCallableFactory());
$pipeline->pipe(function($next, $foo, $bar) {
    // Do something
    ...
    
    $result = $next($foo, $bar);
    
    // Do something else
    ...
    
    return $result;
});
$pipeline->outlet(function($foo, $bar) {
    return "${foo}.${bar}";
});
// Manage multiple parameters
echo $pipeline->send('foo', 'bar'); // Print foo.bar

$pipeline->pipe(new LogCommand());
$pipeline->outlet(new CreateUserHandler());

...

$pipeline->prepend(new TransactionnalCommand());
$pipeline->send(new CreateUserCommand());

class TransactionnalCommand
{
    public function __invoke($next, $command)
    {
        try {
            $result = $next($command);
            
            // Commit and return the result
            ...
            return $result;
        } catch (\Throwable $exception) {
            // Rollback and propagate exception
            throw $exception;
        }
    }
}

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

$new = clone $pipeline;

$new->pipe(new Double());

echo $pipeline->send(2); // 4
echo $new->send(2); // 8