PHP code example of jardissupport / workflow

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

    

jardissupport / workflow example snippets


use JardisSupport\Workflow\Builder\WorkflowBuilder;
use JardisSupport\Workflow\Workflow;
use JardisSupport\Workflow\WorkflowResult;

// Build a two-step graph
$config = (new WorkflowBuilder())
    ->node(ValidateOrderHandler::class)
        ->onSuccess(ChargePaymentHandler::class)
        ->onFail(RejectOrderHandler::class)
    ->node(ChargePaymentHandler::class)
        ->onSuccess(ConfirmOrderHandler::class)
    ->build();

// Workflow is stateless and single-shot. Per-run input is passed as $data and forwarded
// to the handler factory — handlers themselves are invoked with the WorkflowContext only.
$workflow = new Workflow(
    handlerFactory: fn(string $cls, mixed $data): object => new $cls($data),
);
$context  = $workflow($config, $order);

// Inspect the final result and the full chain
$lastResult   = $context->getPrevious();                          // WorkflowResult of last executed handler
$chargeResult = $context->getLatest(ChargePaymentHandler::class); // most recent invocation of that handler
$allCharges   = $context->getAll(ChargePaymentHandler::class);    // every invocation in execution order
$executed     = count($context->getChain());                      // total number of handler invocations

use JardisSupport\Contract\Workflow\WorkflowContextInterface;
use JardisSupport\Workflow\Builder\WorkflowBuilder;
use JardisSupport\Workflow\Workflow;
use JardisSupport\Workflow\WorkflowResult;

// Handler using named transitions (retry loop). All handlers share the same signature:
// __invoke(WorkflowContextInterface): WorkflowResultInterface — per-run input is wired in
// by the handler factory (e.g. injected via constructor or set as the BoundedContext payload).
class ChargePaymentHandler
{
    public function __construct(private readonly Order $order) {}

    public function __invoke(WorkflowContextInterface $context): WorkflowResult
    {
        // Count prior invocations from the chain — every retry has a fresh entry
        $attempt = count($context->getAll(self::class)) + 1;

        $gatewayResult = $this->gateway->charge($this->order->total);

        if ($gatewayResult->isTemporaryFailure()) {
            // Service-side timeout translated into a domain transition — loops back via ON_TIMEOUT
            return new WorkflowResult(WorkflowResult::ON_TIMEOUT, ['attempt' => $attempt]);
        }

        if (!$gatewayResult->isSuccess()) {
            return new WorkflowResult(WorkflowResult::ON_FAIL, ['error' => $gatewayResult->message]);
        }

        return new WorkflowResult(WorkflowResult::ON_SUCCESS, ['chargeId' => $gatewayResult->id]);
    }
}

// Wire the timeout retry back to the same handler
$config = (new WorkflowBuilder())
    ->node(ChargePaymentHandler::class)
        ->onSuccess(FulfillOrderHandler::class)
        ->onFail(NotifyFailureHandler::class)
        ->onTimeout(ChargePaymentHandler::class)   // self-loop for retry-like behaviour
    ->build();

// Inject handlers from a DI container; the factory receives both the FQCN and the
// per-run $data passed to $workflow($config, $data).
$workflow = new Workflow(
    fn(string $class, mixed $data): object => $container->get($class)->withOrder($data),
);

$context = $workflow($config, $order);

// Inspect the chain — flat ordered execution log; every entry is a stamped WorkflowResult
foreach ($context->getChain() as $result) {
    echo "{$result->getHandlerFqcn()}: {$result->getStatus()}\n";
}

use JardisSupport\Workflow\WorkflowConfig;
use JardisSupport\Workflow\WorkflowResult;

$config = new WorkflowConfig(strictRouting: true);
$config->addNode(ChargePaymentHandler::class, [
    WorkflowResult::ON_SUCCESS => FulfillOrderHandler::class,
    WorkflowResult::ON_FAIL    => null,   // declared terminal — legitimate, silent end
]);