Skip to content

Commit d35784a

Browse files
authored
Update example code to use helpers
1 parent bb50c09 commit d35784a

File tree

16 files changed

+129
-119
lines changed

16 files changed

+129
-119
lines changed

docs/configuration/microservices.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Update your workflow and activity classes to use the shared queue connection. As
9292
```php
9393
// App: workflow microservice
9494

95-
use Workflow\ActivityStub;
95+
use function Workflow\activity;
9696
use Workflow\Workflow;
9797

9898
class MyWorkflow extends Workflow
@@ -102,7 +102,7 @@ class MyWorkflow extends Workflow
102102

103103
public function execute($name)
104104
{
105-
$result = yield ActivityStub::make(MyActivity::class, $name);
105+
$result = yield activity(MyActivity::class, $name);
106106
return $result;
107107
}
108108
}

docs/defining-workflows/passing-data.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ $workflow->output();
1818

1919
It will be passed as arguments to the workflow's `execute()` method.
2020

21-
Similarly, you can pass data into an activity via the `ActivityStub::make()` method.
21+
Similarly, you can pass data into an activity via the `activity()` helper function.
2222

2323
```php
24-
use Workflow\ActivityStub;
24+
use function Workflow\activity;
2525
use Workflow\Workflow;
2626

2727
class MyWorkflow extends Workflow
2828
{
2929
public function execute($name)
3030
{
31-
return yield ActivityStub::make(MyActivity::class, $name);
31+
return yield activity(MyActivity::class, $name);
3232
}
3333
}
3434
```
@@ -55,14 +55,14 @@ Passing in models works similarly to `SerializesModels`.
5555

5656
```php
5757
use App\Models\User;
58-
use Workflow\ActivityStub;
58+
use function Workflow\activity;
5959
use Workflow\Workflow;
6060

6161
class MyWorkflow extends Workflow
6262
{
6363
public function execute(User $user)
6464
{
65-
return yield ActivityStub::make(MyActivity::class, $user->name);
65+
return yield activity(MyActivity::class, $user->name);
6666
}
6767
}
6868
```

docs/defining-workflows/workflows.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ php artisan make:workflow MyWorkflow
1515
It is defined by extending the `Workflow` class and implementing the `execute()` method.
1616

1717
```php
18-
use Workflow\ActivityStub;
18+
use function Workflow\activity;
1919
use Workflow\Workflow;
2020

2121
class MyWorkflow extends Workflow
2222
{
2323
public function execute()
2424
{
25-
$result = yield ActivityStub::make(MyActivity::class);
25+
$result = yield activity(MyActivity::class);
2626

2727
return $result;
2828
}

docs/failures-and-recovery.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class MyActivity extends Activity
2525

2626
```php
2727
use Exception;
28-
use Workflow\ActivityStub;
28+
use function Workflow\activity;
2929
use Workflow\Workflow;
3030

3131
class MyWorkflow extends Workflow
3232
{
3333
public function execute()
3434
{
3535
try {
36-
$result = yield ActivityStub::make(MyActivity::class);
36+
$result = yield activity(MyActivity::class);
3737
} catch (Exception) {
3838
// handle the exception here
3939
}

docs/features/child-workflows.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ sidebar_position: 7
66

77
It's often necessary to break down complex processes into smaller, more manageable units. Child workflows provide a way to encapsulate a sub-process within a parent workflow. This allows you to create hierarchical and modular structures for your workflows, making them more organized and maintainable.
88

9-
A child workflow is just like any other workflow. The only difference is how it's invoked within the parent workflow, using `ChildWorkflowStub::make()`.
9+
A child workflow is just like any other workflow. The only difference is how it's invoked within the parent workflow, using `child()`.
1010

1111
```php
12-
use Workflow\ChildWorkflowStub;
12+
use function Workflow\child;
1313
use Workflow\Workflow;
1414

1515
class ParentWorkflow extends Workflow
1616
{
1717
public function execute()
1818
{
19-
$result = yield ChildWorkflowStub::make(ChildWorkflow::class);
19+
$result = yield child(ChildWorkflow::class);
2020
}
2121
}
2222
```
@@ -30,14 +30,14 @@ Parent workflows can signal their child workflows to coordinate behavior or pass
3030
The `child()` method returns a `ChildWorkflowHandle` for the most recently created child workflow:
3131

3232
```php
33-
use Workflow\ChildWorkflowStub;
33+
use function Workflow\child;
3434
use Workflow\Workflow;
3535

3636
class ParentWorkflow extends Workflow
3737
{
3838
public function execute()
3939
{
40-
$child = ChildWorkflowStub::make(ChildWorkflow::class);
40+
$child = child(ChildWorkflow::class);
4141

4242
$childHandle = $this->child();
4343

@@ -55,24 +55,24 @@ class ParentWorkflow extends Workflow
5555
Use the `children()` method to get handles for all child workflows created by the parent:
5656

5757
```php
58-
use Workflow\ChildWorkflowStub;
58+
use function Workflow\{all, child};
5959
use Workflow\Workflow;
6060

6161
class ParentWorkflow extends Workflow
6262
{
6363
public function execute()
6464
{
65-
$child1 = ChildWorkflowStub::make(ChildWorkflow::class, 'first');
66-
$child2 = ChildWorkflowStub::make(ChildWorkflow::class, 'second');
67-
$child3 = ChildWorkflowStub::make(ChildWorkflow::class, 'third');
65+
$child1 = child(ChildWorkflow::class, 'first');
66+
$child2 = child(ChildWorkflow::class, 'second');
67+
$child3 = child(ChildWorkflow::class, 'third');
6868

6969
$childHandles = $this->children();
7070

7171
foreach ($childHandles as $childHandle) {
7272
$childHandle->process('approved');
7373
}
7474

75-
$results = yield ChildWorkflowStub::all([$child1, $child2, $child3]);
75+
$results = yield all([$child1, $child2, $child3]);
7676

7777
return $results;
7878
}
@@ -86,10 +86,9 @@ The `children()` method returns children in reverse chronological order (most re
8686
You can forward external signals to child workflows by combining signal methods with child handles:
8787

8888
```php
89-
use Workflow\ChildWorkflowStub;
89+
use function Workflow\{await, child};
9090
use Workflow\SignalMethod;
9191
use Workflow\Workflow;
92-
use Workflow\WorkflowStub;
9392

9493
class ParentWorkflow extends Workflow
9594
{
@@ -105,11 +104,11 @@ class ParentWorkflow extends Workflow
105104

106105
public function execute()
107106
{
108-
$child = ChildWorkflowStub::make(ChildWorkflow::class);
107+
$child = child(ChildWorkflow::class);
109108

110109
$childHandle = $this->child();
111110

112-
yield WorkflowStub::await(fn () => $this->processed);
111+
yield await(fn () => $this->processed);
113112

114113
$childHandle->process($this->status);
115114

@@ -127,16 +126,15 @@ class ParentWorkflow extends Workflow
127126
You can access the underlying stored workflow ID using the `id()` method. This allows you to store the ID for external systems to signal the child directly.
128127

129128
```php
130-
use Workflow\ActivityStub;
131-
use Workflow\ChildWorkflowStub;
129+
use function Workflow\{activity, child};
132130
use Workflow\Workflow;
133131

134132
class ParentWorkflow extends Workflow
135133
{
136134
public function execute()
137135
{
138-
$child = ChildWorkflowStub::make(ChildWorkflow::class);
139-
yield ActivityStub::make(StoreWorkflowIdActivity::class, $this->child()->id());
136+
$child = child(ChildWorkflow::class);
137+
yield activity(StoreWorkflowIdActivity::class, $this->child()->id());
140138
yield $child;
141139
}
142140
}
@@ -145,7 +143,7 @@ class ParentWorkflow extends Workflow
145143
or
146144

147145
```php
148-
use Workflow\ChildWorkflowStub;
146+
use function Workflow\{await, child};
149147
use Workflow\QueryMethod;
150148
use Workflow\Workflow;
151149

@@ -161,9 +159,9 @@ class ParentWorkflow extends Workflow
161159

162160
public function execute()
163161
{
164-
$child = ChildWorkflowStub::make(ChildWorkflow::class);
162+
$child = child(ChildWorkflow::class);
165163
$childHandle = $this->child();
166-
yield WorkflowStub::await(fn () => !is_null($childHandle));
164+
yield await(fn () => !is_null($childHandle));
167165
$this->childId = $childHandle->id();
168166
yield $child;
169167
}
@@ -183,19 +181,19 @@ if ($childId = $workflow->childId()) {
183181

184182
## Async Activities
185183

186-
Rather than creating a child workflow, you can pass a callback to `ActivityStub::async()` and it will be executed in the context of a separate workflow.
184+
Rather than creating a child workflow, you can pass a callback to `async()` and it will be executed in the context of a separate workflow.
187185

188186
```php
189-
use Workflow\ActivityStub;
190187
use Workflow\Workflow;
188+
use function Workflow\{activity, async};
191189

192190
class AsyncWorkflow extends Workflow
193191
{
194192
public function execute()
195193
{
196-
[$result, $otherResult] = yield ActivityStub::async(function () {
197-
$result = yield ActivityStub::make(Activity::class);
198-
$otherResult = yield ActivityStub::make(OtherActivity::class, 'other');
194+
[$result, $otherResult] = yield async(function () {
195+
$result = yield activity(Activity::class);
196+
$otherResult = yield activity(OtherActivity::class, 'other');
199197
return [$result, $otherResult];
200198
});
201199
}

docs/features/concurrency.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ sidebar_position: 8
44

55
# Concurrency
66

7-
Activities can be executed in series or in parallel. In either case, you start by using `ActivityStub::make()` to create a new instance of an activity and return a promise that represents the execution of that activity. The activity will immediately begin executing in the background. You can then `yield` this promise to pause the execution of the workflow and wait for the result of the activity, or pass the promise into the `ActivityStub::all()` method to wait for a group of activities to complete in parallel.
7+
Activities can be executed in series or in parallel. In either case, you start by using `activity()` to create a new instance of an activity and return a promise that represents the execution of that activity. The activity will immediately begin executing in the background. You can then `yield` this promise to pause the execution of the workflow and wait for the result of the activity, or pass the promise into the `all()` method to wait for a group of activities to complete in parallel.
88

99
## Series
1010

1111
This example will execute 3 activities in series, waiting for the completion of each activity before continuing to the next one.
1212

1313
```php
14-
use Workflow\ActivityStub;
14+
use function Workflow\activity;
1515
use Workflow\Workflow;
1616

1717
class MyWorkflow extends Workflow
1818
{
1919
public function execute()
2020
{
2121
return [
22-
yield ActivityStub::make(MyActivity1::class),
23-
yield ActivityStub::make(MyActivity1::class),
24-
yield ActivityStub::make(MyActivity1::class),
22+
yield activity(MyActivity1::class),
23+
yield activity(MyActivity1::class),
24+
yield activity(MyActivity1::class),
2525
];
2626
}
2727
}
@@ -32,47 +32,47 @@ class MyWorkflow extends Workflow
3232
This example will execute 3 activities in parallel, waiting for the completion of all activities and collecting the results.
3333

3434
```php
35-
use Workflow\ActivityStub;
35+
use function Workflow\{activity, all};
3636
use Workflow\Workflow;
3737

3838
class MyWorkflow extends Workflow
3939
{
4040
public function execute()
4141
{
42-
return yield ActivityStub::all([
43-
ActivityStub::make(MyActivity1::class),
44-
ActivityStub::make(MyActivity2::class),
45-
ActivityStub::make(MyActivity3::class),
42+
return yield all([
43+
activity(MyActivity1::class),
44+
activity(MyActivity2::class),
45+
activity(MyActivity3::class),
4646
]);
4747
}
4848
}
4949
```
5050

51-
The main difference between the serial example and the parallel execution example is the number of `yield` statements. In the serial example, there are 3 `yield` statements, one for each activity. This means that the workflow will pause and wait for each activity to complete before continuing to the next one. In the parallel example, there is only 1 `yield` statement, which wraps all of the activities in a call to `ActivityStub::all()`. This means that all of the activities will be executed in parallel, and the workflow will pause and wait for all of them to complete as a group before continuing.
51+
The main difference between the serial example and the parallel execution example is the number of `yield` statements. In the serial example, there are 3 `yield` statements, one for each activity. This means that the workflow will pause and wait for each activity to complete before continuing to the next one. In the parallel example, there is only 1 `yield` statement, which wraps all of the activities in a call to `all()`. This means that all of the activities will be executed in parallel, and the workflow will pause and wait for all of them to complete as a group before continuing.
5252

5353
## Mix and Match
5454

5555
You can also mix serial and parallel executions as desired.
5656

5757
```php
58-
use Workflow\ActivityStub;
58+
use function Workflow\{activity, all, async};
5959
use Workflow\Workflow;
6060

6161
class MyWorkflow extends Workflow
6262
{
6363
public function execute()
6464
{
6565
return [
66-
yield ActivityStub::make(MyActivity1::class),
67-
yield ActivityStub::all([
68-
ActivityStub::async(fn () => [
69-
yield ActivityStub::make(MyActivity2::class),
70-
yield ActivityStub::make(MyActivity3::class),
66+
yield activity(MyActivity1::class),
67+
yield all([
68+
async(fn () => [
69+
yield activity(MyActivity2::class),
70+
yield activity(MyActivity3::class),
7171
]),
72-
ActivityStub::make(MyActivity4::class),
73-
ActivityStub::make(MyActivity5::class),
72+
activity(MyActivity4::class),
73+
activity(MyActivity5::class),
7474
]),
75-
yield ActivityStub::make(MyActivity6::class),
75+
yield activity(MyActivity6::class),
7676
];
7777
}
7878
}
@@ -84,15 +84,15 @@ Activity 1 will execute and complete before any other activities start. Activiti
8484

8585
## Child Workflows in Parallel
8686

87-
You can pass child workflows to `ActivityStub::all()` along with with other activities (but we also provide `ChildWorkflowStub::all()` if you prefer). Both of these are just thin wrappers for React Promise's `all()`. It works the same way as parallel activity execution, but for child workflows. It allows you to fan out multiple child workflows and wait for all of them to complete together.
87+
You can pass child workflows to `all()` along with with other activities. Both of these are just thin wrappers for React Promise's `all()`. It works the same way as parallel activity execution, but for child workflows. It allows you to fan out multiple child workflows and wait for all of them to complete together.
8888

8989
```php
90-
use Workflow\ChildWorkflowStub;
90+
use function Workflow\{all, child};
9191

92-
$results = yield ChildWorkflowStub::all([
93-
ChildWorkflowStub::make(ChildA::class),
94-
ChildWorkflowStub::make(ChildB::class),
95-
ChildWorkflowStub::make(ChildC::class),
92+
$results = yield all([
93+
child(ChildA::class),
94+
child(ChildB::class),
95+
child(ChildC::class),
9696
]);
9797
```
9898

docs/features/continue-as-new.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ This is useful when you need to:
1313

1414
## Using `continueAsNew`
1515

16-
To restart a workflow as new, call the static method `WorkflowStub::continueAsNew(...)` from within the workflow’s `execute()` method.
16+
To restart a workflow as new, call the helper function `continueAsNew(...)` from within the workflow’s `execute()` method.
1717

1818
```php
19-
use Workflow\ActivityStub;
19+
use function Workflow\{activity, continueAsNew};
2020
use Workflow\Workflow;
21-
use Workflow\WorkflowStub;
2221

2322
class CounterWorkflow extends Workflow
2423
{
2524
public function execute(int $count = 0, int $max = 3)
2625
{
27-
$result = yield ActivityStub::make(CountActivity::class, $count);
26+
$result = yield activity(CountActivity::class, $count);
2827

2928
if ($count >= $max) {
3029
return 'workflow_' . $result;
3130
}
3231

33-
return yield WorkflowStub::continueAsNew($count + 1, $max);
32+
return yield continueAsNew($count + 1, $max);
3433
}
3534
}
3635
```

0 commit comments

Comments
 (0)