PHP code example of durable-workflow / sdk

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

    

durable-workflow / sdk example snippets




use DurableWorkflow\Attribute\Activity;
use DurableWorkflow\Attribute\Workflow;
use DurableWorkflow\Worker\ActivityContext;
use DurableWorkflow\Worker\WorkflowContext;

final class GreetingWorkflow
{
    #[Workflow('example.greeting')]
    public function run(WorkflowContext $context, string $name): array
    {
        $greeting = $context->activity('example.greet', [$name]);

        return ['greeting' => $greeting];
    }
}

final class GreetingActivities
{
    #[Activity('example.greet')]
    public function greet(ActivityContext $context, string $name): string
    {
        return "Hello, {$name}";
    }
}

use DurableWorkflow\Client;
use DurableWorkflow\Worker;

$client = new Client(
    getenv('DURABLE_WORKFLOW_RUNTIME_URL'),
    namespace: getenv('DURABLE_WORKFLOW_NAMESPACE'),
    workerToken: getenv('DURABLE_WORKFLOW_WORKER_TOKEN'),
);

Worker::create($client, 'greetings')
    ->register(GreetingWorkflow::class, GreetingActivities::class)
    ->run();

$client = new Client(
    getenv('DURABLE_WORKFLOW_RUNTIME_URL'),
    namespace: getenv('DURABLE_WORKFLOW_NAMESPACE'),
    controlToken: getenv('DURABLE_WORKFLOW_CLIENT_TOKEN'),
);

$handle = $client->startWorkflow(
    workflowType: 'example.greeting',
    workflowId: 'greeting-'.bin2hex(random_bytes(12)),
    taskQueue: 'greetings',
    input: ['PHP'],
);

var_dump($handle->result());