1
- # Laravel Workflow Manager
1
+ # Laravel Workflow
2
2
3
3
[ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/safemood/laravel-workflow.svg?style=flat-square )] ( https://packagist.org/packages/safemood/laravel-workflow )
4
4
[ ![ GitHub Tests Action Status] ( https://img.shields.io/github/workflow/status/safemood/laravel-workflow/Tests?label=tests&style=flat-square )] ( https://github.com/safemood/laravel-workflow/actions?query=workflow%3ATests )
5
5
[ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/safemood/laravel-workflow/Check%20&%20fix%20styling?label=code%20style&style=flat-square )] ( https://github.com/safemood/laravel-workflow/actions?query=workflow%3A"Check+%26+fix+styling" )
6
6
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/safemood/laravel-workflow.svg?style=flat-square )] ( https://packagist.org/packages/safemood/laravel-workflow )
7
7
8
- Laravel Workflow Manager simplifies the creation and management of workflows in Laravel applications, providing features for defining actions, tracking events, and handling responses .
8
+ Laravel Workflow combines feature details into one class, allowing for action definition and event tracking, simplifying understanding and revealing hidden logic .
9
9
10
- - [ Laravel Workflow Manager] ( #laravel-workflow-manager )
11
10
- [ Installation] ( #installation )
12
11
- [ Create a Workflow] ( #create-a-workflow )
13
12
- [ Create Actions] ( #create-actions )
@@ -39,6 +38,29 @@ You can create an action using the artisan command:
39
38
php artisan make:action ValidateCartItems
40
39
```
41
40
41
+ ``` php
42
+ <?php
43
+
44
+ namespace App\Actions;
45
+
46
+ use Safemood\Workflow\Action;
47
+
48
+ class ValidateCartItems extends Action
49
+ {
50
+ public function handle(array & $context)
51
+ {
52
+ // Simulate validation logic
53
+ if (empty($context['cart'])) {
54
+ throw new \Exception('Cart is empty');
55
+ }
56
+
57
+ // you can pass data to the next action if you want
58
+ $context['validated'] = true;
59
+
60
+ }
61
+ }
62
+ ```
63
+
42
64
43
65
## Basic Example
44
66
@@ -49,7 +71,7 @@ Once you have set up your workflows and actions, you can define your business lo
49
71
In your ` PaymentWorkflow ` class, you define the sequence of actions and conditions that make up your workflow:
50
72
51
73
``` php
52
- // app/Workflows/PaymentWorkflow. php
74
+ <? php
53
75
54
76
namespace App\Workflows;
55
77
@@ -59,7 +81,7 @@ use App\Actions\ValidateCartItems;
59
81
use App\Events\PaymentProcessed;
60
82
use App\Jobs\SendEmails;
61
83
use App\Observers\UserObserver;
62
- use App\Models\User ;
84
+ use App\Models\Order ;
63
85
use Safemood\Workflow\WorkflowManager;
64
86
65
87
class PaymentWorkflow extends Workflow
@@ -75,62 +97,69 @@ class PaymentWorkflow extends Workflow
75
97
// The main action of the workflow
76
98
$this->addMainAction(new MakePayment());
77
99
78
- // Actions to be executed after the main action (job)
79
- $this->addAfterAction(new SendEmails());
100
+ // Actions to be executed after the main action
101
+ $this->addAfterAction(new SendEmails()); // Normal laravel Job in this example
80
102
81
103
// Observers to register for specific entities
82
104
$this->registerObservers([
83
- User ::class => UserObserver ::class,
105
+ Order ::class => OrderObserver ::class,
84
106
]);
85
107
86
- // Track events for debugging or insight during workflow execution
87
- $this->trackAllEvents(); // Track all events
88
- // or
89
- // $this->trackEventsIn('App\Events\\'); // Track events in specific namespace
90
- // or
91
- // $this->trackEvents([
92
- // PaymentProcessed::class
93
- // ]); // Track specific events
108
+ // Good Debugging or if you want to understand what is happining during the workflow execution:
109
+
110
+ $this->trackEvents([
111
+ PaymentProcessed::class
112
+ ]);
113
+
114
+ // $this->trackAllEvents(); // or
115
+
116
+ // $this->trackEventsIn('App\Events\\');
117
+
118
+
94
119
}
95
120
}
96
121
```
97
122
98
123
### Execute Workflow
99
124
100
125
``` php
126
+ <?php
127
+
128
+ namespace App\Http\Controllers;
129
+
101
130
use App\Workflows\PaymentWorkflow;
131
+ use Illuminate\Http\Request;
102
132
103
- public function checkout()
133
+ class PaymentController extends Controller
104
134
{
105
- // Example context data representing a user's cart and user information
106
- $context = [
107
- 'cart' => [
108
- ['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2],
109
- ['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1]
110
- ],
111
- 'user' => [
112
- 'id' => 123,
113
- 'name' => 'John Doe',
114
-
115
- ]
116
- ];
117
-
118
- // Execute the PaymentWorkflow with the provided context
119
- $paymentWorkflow = (new PaymentWorkflow)->run($context);
120
-
121
- // Check if the workflow execution was successful
122
- $success = $paymentWorkflow->passes();
123
-
124
- // Check if the workflow execution failed
125
- $failure = $paymentWorkflow->failed();
126
-
127
- // Handle the response based on the workflow outcome
128
- if ($success) {
129
- return $paymentWorkflow->successResponse()
130
- }
131
-
132
- return $paymentWorkflow->failureResponse()
133
-
135
+ public function payment(Request $request)
136
+ {
137
+ // Example context data representing a user's cart and user information
138
+ $context = [
139
+ 'cart' => [
140
+ ['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2],
141
+ ['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1]
142
+ ],
143
+ 'user_id' => 123
144
+ ];
145
+
146
+ // Execute the PaymentWorkflow with the provided context
147
+ $paymentWorkflow = (new PaymentWorkflow)->run($context);
148
+
149
+ // Check if the workflow execution was successful
150
+ $success = $paymentWorkflow->passes();
151
+
152
+ // Check if the workflow execution failed
153
+ $failure = $paymentWorkflow->failed();
154
+
155
+ // Handle the response based on the workflow outcome
156
+ if ($success) {
157
+ return $paymentWorkflow->successResponse();
158
+ }
159
+
160
+ return $paymentWorkflow->failureResponse();
161
+ }
134
162
}
135
163
136
- ```
164
+
165
+ ```
0 commit comments