Skip to content

Commit 322bb82

Browse files
authored
Update example code (#7)
1 parent 0f8ef27 commit 322bb82

12 files changed

+68
-62
lines changed

blog/2022-10-29-email-verifications.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ The `verify-email` route receives a workflow id, loads it and then calls the `ve
6161
Now let’s take a look at the actual workflow.
6262

6363
```php
64-
use Workflow\ActivityStub;
6564
use Workflow\SignalMethod;
6665
use Workflow\Workflow;
67-
use Workflow\WorkflowStub;
66+
use function Workflow\{activity, await};
6867

6968
class VerifyEmailWorkflow extends Workflow
7069
{
@@ -78,11 +77,11 @@ class VerifyEmailWorkflow extends Workflow
7877

7978
public function execute($email = '', $password = '')
8079
{
81-
yield ActivityStub::make(SendEmailVerificationEmailActivity::class, $email);
80+
yield activity(SendEmailVerificationEmailActivity::class, $email);
8281

83-
yield WorkflowStub::await(fn () => $this->verified);
82+
yield await(fn () => $this->verified);
8483

85-
yield ActivityStub::make(VerifyEmailActivity::class, $email, $password);
84+
yield activity(VerifyEmailActivity::class, $email, $password);
8685
}
8786
}
8887
```
@@ -100,9 +99,9 @@ Step By Step
10099

101100
The first time the workflow executes, it will reach the call to `SendEmailVerificationEmailActivity` , start that activity, and then exit. Workflows suspend execution while an activity is running. After the `SendEmailVerificationEmailActivity` finishes, it will resume execution of the workflow. This brings us to…
102101

103-
The second time the workflow is executed, it will reach the call to `SendEmailVerificationEmailActivity` and skip it because it will already have the result of that activity. Then it will reach the call to `WorkflowStub::await()` which allows the workflow to wait for an external signal. In this case, it will come from the user clicking on the verification link they receive in their email. Once the workflow is signaled then it will execute for…
102+
The second time the workflow is executed, it will reach the call to `SendEmailVerificationEmailActivity` and skip it because it will already have the result of that activity. Then it will reach the call to `await()` which allows the workflow to wait for an external signal. In this case, it will come from the user clicking on the verification link they receive in their email. Once the workflow is signaled then it will execute for…
104103

105-
The third time, both the calls to `SendEmailVerificationEmailActivity` and `WorkflowStub::await()` are skipped. This means that the `VerifyEmailActivity` will be started. After the final activity has executed we still have…
104+
The third time, both the calls to `SendEmailVerificationEmailActivity` and `await()` are skipped. This means that the `VerifyEmailActivity` will be started. After the final activity has executed we still have…
106105

107106
The final time the workflow is called, there is nothing left to do so the workflow completes.
108107

blog/2022-10-31-converting-videos-with-ffmpeg.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ For simplicity, the workflow we are making today will only contain the most inte
3030
```php
3131
namespace App\Workflows\ConvertVideo;
3232

33-
use Workflow\ActivityStub;
33+
use function Workflow\activity;
3434
use Workflow\Workflow;
3535

3636
class ConvertVideoWorkflow extends Workflow
3737
{
3838
public function execute()
3939
{
40-
yield ActivityStub::make(
40+
yield activity(
4141
ConvertVideoWebmActivity::class,
4242
storage_path('app/oceans.mp4'),
4343
storage_path('app/oceans.webm'),

blog/2022-11-15-invalidating-cloud-images.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,22 @@ The workflow looks as follows and is the same process as outlined before.
6969
```php
7070
namespace App\Workflows\InvalidateCache;
7171

72-
use Workflow\ActivityStub;
7372
use Workflow\Workflow;
74-
use Workflow\WorkflowStub;
73+
use function Workflow\{activity, timer};
7574

7675
class InvalidateCacheWorkflow extends Workflow
7776
{
7877
public function execute($url)
7978
{
80-
$oldDate = yield ActivityStub::make(CheckImageDateActivity::class, $url);
79+
$oldDate = yield activity(CheckImageDateActivity::class, $url);
8180

8281
while (true) {
83-
yield ActivityStub::make(InvalidateCacheActivity::class, $url);
82+
yield activity(InvalidateCacheActivity::class, $url);
8483

8584
for ($i = 0; $i < 3; ++$i) {
86-
yield WorkflowStub::timer(30);
85+
yield timer(30);
8786

88-
$newDate = yield ActivityStub::make(CheckImageDateActivity::class, $url);
87+
$newDate = yield activity(CheckImageDateActivity::class, $url);
8988

9089
if ($oldDate !== $newDate) return;
9190
}

blog/2022-12-06-job-chaining-vs-fan-out-fan-in.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ The workflow divides the task of creating a PDF into activities, with each activ
2828
```php
2929
namespace App\Workflows\BuildPDF;
3030

31-
use Workflow\ActivityStub;
3231
use Workflow\Workflow;
32+
use function Workflow\{activity, all};
3333

3434
class BuildPDFWorkflow extends Workflow
3535
{
3636
public function execute()
3737
{
38-
$page1 = ActivityStub::make(ConvertURLActivity::class, 'https://example.com/');
39-
$page2 = ActivityStub::make(ConvertURLActivity::class, 'https://example.com/');
38+
$page1 = activity(ConvertURLActivity::class, 'https://example.com/');
39+
$page2 = activity(ConvertURLActivity::class, 'https://example.com/');
4040

41-
$pages = yield ActivityStub::all([$page1, $page2]);
41+
$pages = yield all([$page1, $page2]);
4242

43-
$result = yield ActivityStub::make(MergePDFActivity::class, $pages);
43+
$result = yield activity(MergePDFActivity::class, $pages);
4444

4545
return $result;
4646
}
@@ -76,7 +76,7 @@ class ConvertURLActivity extends Activity
7676
}
7777
```
7878

79-
Next, the `BuildPDFWorkflow` uses `ActivityStub::all` to wait for both `ConvertURLActivity` instances to complete. This is an example of the fan-in part of the fan-out/fan-in pattern, as it collects the results of the parallel activities and combines them into a single array of PDF files.
79+
Next, the `BuildPDFWorkflow` uses `all()` to wait for both `ConvertURLActivity` instances to complete. This is an example of the fan-in part of the fan-out/fan-in pattern, as it collects the results of the parallel activities and combines them into a single array of PDF files.
8080

8181
Finally, the `BuildPDFWorkflow` executes the`MergePDFActivity`, which is passed the array of PDFs that were generated by the `ConvertURLActivity` instances, and merges them into a single PDF document.
8282

blog/2022-12-22-new-laravel-workflow-feature-side-effects.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@ Recently, Laravel Workflow added support for [side effects](https://laravel-work
2222
Here is an example workflow that demonstrates side effects.
2323

2424
```php
25+
use Workflow\Workflow;
26+
use function Workflow\{activity, sideEffect};
27+
2528
class SideEffectWorkflow extends Workflow
2629
{
2730
public function execute()
2831
{
29-
$sideEffect = yield WorkflowStub::sideEffect(
32+
$sideEffect = yield sideEffect(
3033
fn () => random\_int(PHP\_INT\_MIN, PHP\_INT\_MAX)
3134
);
3235

3336
$badSideEffect = random\_int(PHP\_INT\_MIN, PHP\_INT\_MAX);
3437

35-
$result1 = yield ActivityStub::make(SimpleActivity::class, $sideEffect);
38+
$result1 = yield activity(SimpleActivity::class, $sideEffect);
3639

37-
$result2 = yield ActivityStub::make(SimpleActivity::class, $badSideEffect);
40+
$result2 = yield activity(SimpleActivity::class, $badSideEffect);
3841

3942
if ($sideEffect !== $result1) {
4043
throw new Exception(
41-
'These side effects should match because it was properly wrapped in WorkflowStub::sideEffect().'
44+
'These side effects should match because it was properly wrapped in sideEffect().'
4245
);
4346
}
4447

4548
if ($badSideEffect === $result2) {
4649
throw new Exception(
47-
'These side effects should not match because it was not wrapped in WorkflowStub::sideEffect().'
50+
'These side effects should not match because it was not wrapped in sideEffect().'
4851
);
4952
}
5053
}

blog/2023-04-05-introducing-child-workflows-in-laravel-workflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Laravel Workflow has introduced an exciting new feature called “Child Workflow
1414
What are Child Workflows?
1515
=========================
1616

17-
In Laravel Workflow, child workflows are a way to manage complex processes by breaking them down into smaller, more manageable units. They enable developers to create hierarchical and modular structures for their workflows, making them more organized and easier to maintain. A child workflow is essentially a separate workflow that is invoked within a parent workflow using the `ChildWorkflowStub::make()` method.
17+
In Laravel Workflow, child workflows are a way to manage complex processes by breaking them down into smaller, more manageable units. They enable developers to create hierarchical and modular structures for their workflows, making them more organized and easier to maintain. A child workflow is essentially a separate workflow that is invoked within a parent workflow using the `child()` helper function.
1818

1919
Benefits of Using Child Workflows
2020
=================================

blog/2023-04-25-combining-laravel-workflow-and-state-machines.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ use Finite\StatefulInterface;
4949
use Finite\StateMachine\StateMachine;
5050
use Finite\State\State;
5151
use Finite\State\StateInterface;
52+
use function Workflow\await;
5253
use Workflow\Models\StoredWorkflow;
5354
use Workflow\SignalMethod;
54-
use Workflow\WorkflowStub;
5555
use Workflow\Workflow;
5656

5757
class LoanApplicationWorkflow extends Workflow implements StatefulInterface
@@ -127,11 +127,11 @@ class LoanApplicationWorkflow extends Workflow implements StatefulInterface
127127
{
128128
// loan created
129129

130-
yield WorkflowStub::await(fn () => $this->isSubmitted());
130+
yield await(fn () => $this->isSubmitted());
131131

132132
// loan submitted
133133

134-
yield WorkflowStub::await(fn () => $this->isApproved() || $this->isDenied());
134+
yield await(fn () => $this->isApproved() || $this->isDenied());
135135

136136
// loan approved/denied
137137

blog/2023-05-21-saga-pattern-and-laravel-workflow.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ class BookingSagaWorkflow extends Workflow
6161
Next, we’ll imbue our saga with logic, by adding booking steps:
6262

6363
```php
64+
use function Workflow\activity;
65+
use Workflow\Workflow;
66+
6467
class BookingSagaWorkflow extends Workflow
6568
{
6669
public function execute()
6770
{
6871
try {
69-
$flightId = yield ActivityStub::make(BookFlightActivity::class);
70-
$hotelId = yield ActivityStub::make(BookHotelActivity::class);
71-
$carId = yield ActivityStub::make(BookRentalCarActivity::class);
72+
$flightId = yield activity(BookFlightActivity::class);
73+
$hotelId = yield activity(BookHotelActivity::class);
74+
$carId = yield activity(BookRentalCarActivity::class);
7275
} catch (Throwable $th) {
7376
}
7477
}
@@ -81,19 +84,22 @@ Adding Compensations
8184
--------------------
8285

8386
```php
87+
use function Workflow\activity;
88+
use Workflow\Workflow;
89+
8490
class BookingSagaWorkflow extends Workflow
8591
{
8692
public function execute()
8793
{
8894
try {
89-
$flightId = yield ActivityStub::make(BookFlightActivity::class);
90-
$this->addCompensation(fn () => ActivityStub::make(CancelFlightActivity::class, $flightId));
95+
$flightId = yield activity(BookFlightActivity::class);
96+
$this->addCompensation(fn () => activity(CancelFlightActivity::class, $flightId));
9197

92-
$hotelId = yield ActivityStub::make(BookHotelActivity::class);
93-
$this->addCompensation(fn () => ActivityStub::make(CancelHotelActivity::class, $hotelId));
98+
$hotelId = yield activity(BookHotelActivity::class);
99+
$this->addCompensation(fn () => activity(CancelHotelActivity::class, $hotelId));
94100

95-
$carId = yield ActivityStub::make(BookRentalCarActivity::class);
96-
$this->addCompensation(fn () => ActivityStub::make(CancelRentalCarActivity::class, $carId));
101+
$carId = yield activity(BookRentalCarActivity::class);
102+
$this->addCompensation(fn () => activity(CancelRentalCarActivity::class, $carId));
97103
} catch (Throwable $th) {
98104
}
99105
}
@@ -113,14 +119,14 @@ class BookingSagaWorkflow extends Workflow
113119
public function execute()
114120
{
115121
try {
116-
$flightId = yield ActivityStub::make(BookFlightActivity::class);
117-
$this->addCompensation(fn () => ActivityStub::make(CancelFlightActivity::class, $flightId));
122+
$flightId = yield activity(BookFlightActivity::class);
123+
$this->addCompensation(fn () => activity(CancelFlightActivity::class, $flightId));
118124

119-
$hotelId = yield ActivityStub::make(BookHotelActivity::class);
120-
$this->addCompensation(fn () => ActivityStub::make(CancelHotelActivity::class, $hotelId));
125+
$hotelId = yield activity(BookHotelActivity::class);
126+
$this->addCompensation(fn () => activity(CancelHotelActivity::class, $hotelId));
121127

122-
$carId = yield ActivityStub::make(BookRentalCarActivity::class);
123-
$this->addCompensation(fn () => ActivityStub::make(CancelRentalCarActivity::class, $carId));
128+
$carId = yield activity(BookRentalCarActivity::class);
129+
$this->addCompensation(fn () => activity(CancelRentalCarActivity::class, $carId));
124130
} catch (Throwable $th) {
125131
yield from $this->compensate();
126132
throw $th;

blog/2023-08-18-microservice-communication-with-laravel-workflow.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ In a microservices architecture, decoupling is the name of the game. You want ea
2525

2626
#### 1. Create a workflow.
2727
```php
28-
use Workflow\ActivityStub;
28+
use function Workflow\activity;
2929
use Workflow\Workflow;
3030

3131
class MyWorkflow extends Workflow
3232
{
3333
public function execute($name)
3434
{
35-
$result = yield ActivityStub::make(MyActivity::class, $name);
35+
$result = yield activity(MyActivity::class, $name);
3636
return $result;
3737
}
3838
}
@@ -159,7 +159,7 @@ class MyWorkflow extends Workflow
159159

160160
public function execute($name)
161161
{
162-
yield ActivityStub::make(MyActivity::class, $name);
162+
yield activity(MyActivity::class, $name);
163163
}
164164
}
165165
class MyActivity extends Activity

blog/2023-08-20-ai-image-moderation-with-laravel-workflow.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ class ClarifAI
8686
```php
8787
namespace App\Workflows;
8888

89-
use Workflow\ActivityStub;
9089
use Workflow\SignalMethod;
91-
use Workflow\WorkflowStub;
9290
use Workflow\Workflow;
91+
use function Workflow\{activity, all, awaitWithTimeout};
9392

9493
class ImageModerationWorkflow extends Workflow
9594
{
@@ -124,23 +123,23 @@ class ImageModerationWorkflow extends Workflow
124123

125124
private function check($imagePath)
126125
{
127-
return yield ActivityStub::make(AutomatedImageCheckActivity::class, $imagePath);
126+
return yield activity(AutomatedImageCheckActivity::class, $imagePath);
128127
}
129128

130129
private function unsafe($imagePath)
131130
{
132-
yield ActivityStub::all([
133-
ActivityStub::make(LogUnsafeImageActivity::class, $imagePath),
134-
ActivityStub::make(DeleteImageActivity::class, $imagePath),
131+
yield all([
132+
activity(LogUnsafeImageActivity::class, $imagePath),
133+
activity(DeleteImageActivity::class, $imagePath),
135134
]);
136135
}
137136

138137
private function moderate($imagePath)
139138
{
140139
while (true) {
141-
yield ActivityStub::make(NotifyImageModeratorActivity::class, $imagePath);
140+
yield activity(NotifyImageModeratorActivity::class, $imagePath);
142141

143-
$signaled = yield WorkflowStub::awaitWithTimeout('24 hours', fn () => $this->approved || $this->rejected);
142+
$signaled = yield awaitWithTimeout('24 hours', fn () => $this->approved || $this->rejected);
144143

145144
if ($signaled) break;
146145
}

0 commit comments

Comments
 (0)