PHP code example of syberisle / pipeline

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

    

syberisle / pipeline example snippets


$result = $payload;

foreach ($stages as $stage) {
    $result = $stage($result);
}

return $result;

$result = $stage3($stage2($stage1($payload)));

$pipeline = (new Pipeline\Simple)->pipe(function ($payload) {
    return $payload * 10;
});

use SyberIsle\Pipeline\Pipeline;
use SyberIsle\Pipeline\Processor;
use SyberIsle\Pipeline\Stage;

class TimesTwoStage implements Stage
{
    public function process($payload)
    {
        return $payload * 2;
    }
}

class AddOneStage implements Stage
{
    public function process($payload)
    {
        return $payload + 1;
    }
}

$pipeline = (new Pipeline\Simple)
    ->pipe(new TimesTwoStage)
    ->pipe(new AddOneStage);

// Returns 21
(new Processor\FingersCrossed())->process($pipeline, 10);


$processApiRequest = (new Pipeline)
    ->pipe(new ExecuteHttpRequest) // 2
    ->pipe(new ParseJsonResponse); // 3
    
$pipeline = (new Pipeline)
    ->pipe(new ConvertToPsr7Request) // 1
    ->pipe($processApiRequest) // (2,3)
    ->pipe(new ConvertToResponseDto); // 4 
    
(new Processor\FingersCrossed())->process($pipeline, new DeleteBlogPost($postId));

use SyberIsle\Pipeline\Pipeline\SimpleBuilder;

// Prepare the builder
$pipelineBuilder = (new SimpleBuilder)
    ->add(new LogicalStage)
    ->add(new AnotherStage)
    ->add(new LastStage);

// Build the pipeline
$pipeline = $pipelineBuilder->build();

$pipeline = (new Pipeline)->pipe(function () {
    throw new LogicException();
});
    
try {
    (new Processor\FingersCrossed())->process($pipeline, $payload);
} catch(LogicException $e) {
    // Handle the exception.
}