Skip to content

Commit

Permalink
Add docs about factories; move buggregator/trap to dev section
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 22, 2024
1 parent 22dd573 commit 35c3568
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,69 @@ composer require temporal-php/support

## Usage

### Activity and Worker factories
### Factories

The package provides factories to create Activity and Worker stubs in a more convenient way.
With these factories, there is less code because all nested options are moved to the parameters of one method.

Use the `\Temporal\Support\Factory\ActivityStub` factory to create an Activity stub:

```php
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 the `\Temporal\Support\Factory\WorkflowStub` factory to create a Workflow stub in a client scope:

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

Or create a Child Workflow stub in a workflow scope:

```php
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'),
WorkflowStub::childWorkflow(SubscribeNewsWorkflow::class, executionTimeout: '10 minutes'),
WorkflowStub::childWorkflow(PrepareUserEnvironmentWorkflow::class, executionTimeout: '1 hour'),
])->then(
// Suppress failures
onRejected: static fn () => null,
);

// ...
}
}
```

### Attributes

Expand Down Expand Up @@ -62,7 +122,12 @@ $stub = \Temporal\Support\Factory\WorkflowStub::workflow(
```

> [!NOTE]
> Attributes will work only if you use the Activity and Worker factories from this package.
> Attributes will work only if you use the Activity and Worker factories from this package.
> [!WARNING]
> Use attributes on the definitions that you use in factories.
> So, if you separate interfaces and implementation, apply attributes to the interfaces.

### VirtualPromise interface

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
"prefer-stable": true,
"require": {
"php": ">=8.1",
"temporal/sdk": "^2.7",
"buggregator/trap": "^1.3"
"temporal/sdk": "^2.7"
},
"suggest": {
"buggregator/trap": "For better debugging and protobuf messages dumping"
},
"require-dev": {
"buggregator/trap": "^1.3",
"dereuromark/composer-prefer-lowest": "^0.1.10",
"phpunit/phpunit": "^10.4",
"vimeo/psalm": "^5.18"
Expand Down

0 comments on commit 35c3568

Please sign in to comment.