Skip to content

Commit 35c3568

Browse files
committed
Add docs about factories; move buggregator/trap to dev section
1 parent 22dd573 commit 35c3568

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,69 @@ composer require temporal-php/support
3030

3131
## Usage
3232

33-
### Activity and Worker factories
33+
### Factories
3434

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

38+
Use the `\Temporal\Support\Factory\ActivityStub` factory to create an Activity stub:
39+
40+
```php
41+
use \Temporal\Support\Factory\ActivityStub;
42+
43+
#[\Temporal\Workflow\WorkflowInterface]
44+
class HelloWorkflow {
45+
#[\Temporal\Workflow\WorkflowMethod]
46+
public function run(string $user) {
47+
yield ActivityStub::activity(
48+
class: UserService::class,
49+
startToCloseTimeout: 60,
50+
retryAttempts: 5,
51+
)->getContactEmail($user)->then(
52+
fn (string $email) => ActivityStub::activity(
53+
class: HelloService::class,
54+
startToCloseTimeout: '10 minutes',
55+
retryAttempts: 5,
56+
)->sendHelloEmail($user, $email),
57+
);
58+
}
59+
}
60+
```
61+
62+
Use the `\Temporal\Support\Factory\WorkflowStub` factory to create a Workflow stub in a client scope:
63+
64+
```php
65+
use \Temporal\Support\Factory\WorkflowStub;
66+
/**
67+
* @var \Temporal\Client\WorkflowClient $client
68+
*/
69+
$stub = WorkflowStub::workflow($client, HelloWorkflow::class, executionTimeout: '1 day');
70+
$run = $client->start($stub, 'User');
71+
// ...
72+
```
73+
74+
Or create a Child Workflow stub in a workflow scope:
75+
76+
```php
77+
use \Temporal\Support\Factory\WorkflowStub;
78+
79+
#[\Temporal\Workflow\WorkflowInterface]
80+
class RegisterWorkflow {
81+
#[\Temporal\Workflow\WorkflowMethod]
82+
public function run(string $user) {
83+
yield \Temporal\Promise::all([
84+
WorkflowStub::childWorkflow(GreetingWorkflow::class, executionTimeout: '1 hour'),
85+
WorkflowStub::childWorkflow(SubscribeNewsWorkflow::class, executionTimeout: '10 minutes'),
86+
WorkflowStub::childWorkflow(PrepareUserEnvironmentWorkflow::class, executionTimeout: '1 hour'),
87+
])->then(
88+
// Suppress failures
89+
onRejected: static fn () => null,
90+
);
91+
92+
// ...
93+
}
94+
}
95+
```
3696

3797
### Attributes
3898

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

64124
> [!NOTE]
65-
> Attributes will work only if you use the Activity and Worker factories from this package.
125+
> Attributes will work only if you use the Activity and Worker factories from this package.
126+
127+
> [!WARNING]
128+
> Use attributes on the definitions that you use in factories.
129+
> So, if you separate interfaces and implementation, apply attributes to the interfaces.
130+
66131

67132
### VirtualPromise interface
68133

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@
3838
"prefer-stable": true,
3939
"require": {
4040
"php": ">=8.1",
41-
"temporal/sdk": "^2.7",
42-
"buggregator/trap": "^1.3"
41+
"temporal/sdk": "^2.7"
42+
},
43+
"suggest": {
44+
"buggregator/trap": "For better debugging and protobuf messages dumping"
4345
},
4446
"require-dev": {
47+
"buggregator/trap": "^1.3",
4548
"dereuromark/composer-prefer-lowest": "^0.1.10",
4649
"phpunit/phpunit": "^10.4",
4750
"vimeo/psalm": "^5.18"

0 commit comments

Comments
 (0)