PHP code example of temporal-php / support

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

    

temporal-php / support example snippets


use \Temporal\Support\Factory\ActivityStub;

#[\Temporal\Workflow\WorkflowInterface]
class HelloWorkflow {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $user) {
        yield ActivityStub::activity(
            class: UserService::class,
            startToCloseTimeout: 60,
            retryAttempts: 5,
        )->getContactEmail($user)->then(
            fn (string $email) => ActivityStub::activity(
                class: HelloService::class,
                startToCloseTimeout: '10 minutes',
                retryAttempts: 5,
            )->sendHelloEmail($user, $email),
        );
    }
}

use \Temporal\Support\Factory\WorkflowStub;
/**
 * @var \Temporal\Client\WorkflowClient $client
 */
$stub = WorkflowStub::workflow($client, HelloWorkflow::class, executionTimeout: '1 day');
$run = $client->start($stub, 'User');
// ...

use \Temporal\Support\Factory\WorkflowStub;

#[\Temporal\Workflow\WorkflowInterface]
class RegisterWorkflow {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $user) {
        yield \Temporal\Promise::all([
            WorkflowStub::childWorkflow(GreetingWorkflow::class, executionTimeout: '1 hour')->greet($user),
            WorkflowStub::childWorkflow(SubscribeNewsWorkflow::class, executionTimeout: '10 minutes')->subscribe($user),
            WorkflowStub::childWorkflow(PrepareUserSpaceWorkflow::class, executionTimeout: '1 hour')->handle($user),
        ])->then(
            // Suppress failures
            onRejected: static fn () => null,
        );

        // ...
    }
}

#[\Temporal\Support\Attribute\TaskQueue('my-task-queue')]
#[\Temporal\Support\Attribute\RetryPolicy(attempts: 5)]
#[WorkflowInterface]
interface HelloWorkflow {
    #[WorkflowMethod]
    public function greet(string $name);
}

$stub = \Temporal\Support\Factory\WorkflowStub::workflow($client, HelloWorkflow::class);

// TaskQueue is now set to 'my-task-queue' and RetryPolicy to 5 attempts
$stub->greet('User');

// You can override the default options
$stub = \Temporal\Support\Factory\WorkflowStub::workflow(
    $client,
    HelloWorkflow::class,
    taskQueue: 'another-task-queue',
    retryAttempts: 1,
)->greet('User');

use Temporal\Support\VirtualPromise;

#[\Temporal\Activity\ActivityInterface]
class HelloService {
    /**
     * @param non-empty-string $name
     *
     * @return VirtualPromise<non-empty-string>
     */
    public function greet(string $name) {
        // ...
    }
}

#[\Temporal\Workflow\WorkflowInterface]
class WorkflowClass {
    #[\Temporal\Workflow\WorkflowMethod]
    public function run(string $name) {
        $activity = \Temporal\Support\Factory\ActivityStub::activity(HelloService::class);

        // IDE will know that $name is a non-empty-string
        $name = yield $activity->greet($name);
        // ...
    }
}
bash
composer