PHP code example of zheltikov / php-pipeline

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

    

zheltikov / php-pipeline example snippets




unction Zheltikov\Pipeline\pipeline;

$work = pipeline(function ($resolve, $reject) {
        // do some interesting work...
        // for illustration purposes, just randomize the result
        rand(0, 1)
            ? $resolve(123) // pass the result value of the work
            : $reject(new \Exception('Some error'));
    })
    // on each step of the pipeline, do something with the result of the
    // previous step and pass it forward
    ->then(fn($x) => $x ** 2) 
    ->then(fn($x) => $x + 1)
    ->then(fn($x) => $x / 123)
    ->then(fn($x) => sqrt($x))
    ->catch(function ($reason) {
        // do something when an error occurs in the pipeline
        // for example, you can log it, for later analysis
        \Logger::log($reason);
    })
    ->finally(function () {
        // do something regardless if the work succeeded of failed
        // for example you can close some file or connection
        fclose($some_file);
        mysqli_close($connection);
    });

// You are given some methods to gather information about the pipeline
if ($work->isResolved()) {
    // The pipeline succeeded
    $result = $work->getValue();
}

if ($work->isRejected()) {
    // The pipeline failed
    $reason = $work->getReason();
}

// We can even add pipeline steps afterwards...!
$work->then(fn($x) => $x * $x)
    // ...
    ->catch('var_dump')
    ->finally(function () { /* ... */ });

shell
$ composer