PHP code example of projectsaturnstudios / pocketflow-php

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

    

projectsaturnstudios / pocketflow-php example snippets




use ProjectSaturnStudios\PocketFlow\Flow;
use ProjectSaturnStudios\PocketFlow\Node;

class GreetNode extends Node
{
    public function prep(mixed &$shared): mixed
    {
        return $shared['name'] ?? 'World';
    }

    public function exec(mixed $prep_res): mixed
    {
        return "Hello, {$prep_res}!";
    }

    public function post(mixed &$shared, mixed $prep_res, mixed $exec_res): mixed
    {
        $shared['greeting'] = $exec_res;

        return 'default'; // next action
    }
}

class PrintNode extends Node
{
    public function post(mixed &$shared, mixed $prep_res, mixed $exec_res): mixed
    {
        echo $shared['greeting'], PHP_EOL;

        return null; // end flow
    }
}

$greet = new GreetNode;
$print = new PrintNode;
$greet->next($print);

$shared = ['name' => 'PocketFlow'];
(new Flow($greet))->run($shared);

$nodeA->on('ok')->next($nodeB);
$nodeA->next($nodeC, 'retry');

$tick->next($tick, 'continue');
// return 'continue' to loop, or null to stop

use ProjectSaturnStudios\PocketFlow\AsyncFlow;
use ProjectSaturnStudios\PocketFlow\AsyncNode;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;

class FetchNode extends AsyncNode
{
    public function exec_async(mixed $prep_res): PromiseInterface
    {
        return resolve(['ok' => true]);
    }

    public function post_async(mixed $shared, mixed $prep_res, mixed $exec_res): PromiseInterface
    {
        $shared['result'] = $exec_res;

        return resolve('default');
    }
}

$shared = [];
$promise = (new AsyncFlow(new FetchNode))->run_async($shared);
// $promise is a React\Promise\PromiseInterface
bash
composer