Skip to content

Commit cd0c157

Browse files
committed
Merge branch 'main' of github.com:Safemood/laravel-workflow
2 parents 900cfeb + 0513eb7 commit cd0c157

File tree

1 file changed

+76
-47
lines changed

1 file changed

+76
-47
lines changed

README.md

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Laravel Workflow Manager
1+
# Laravel Workflow
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/safemood/laravel-workflow.svg?style=flat-square)](https://packagist.org/packages/safemood/laravel-workflow)
44
[![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)
55
[![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")
66
[![Total Downloads](https://img.shields.io/packagist/dt/safemood/laravel-workflow.svg?style=flat-square)](https://packagist.org/packages/safemood/laravel-workflow)
77

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.
99

10-
- [Laravel Workflow Manager](#laravel-workflow-manager)
1110
- [Installation](#installation)
1211
- [Create a Workflow](#create-a-workflow)
1312
- [Create Actions](#create-actions)
@@ -39,6 +38,29 @@ You can create an action using the artisan command:
3938
php artisan make:action ValidateCartItems
4039
```
4140

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+
4264

4365
## Basic Example
4466

@@ -49,7 +71,7 @@ Once you have set up your workflows and actions, you can define your business lo
4971
In your `PaymentWorkflow` class, you define the sequence of actions and conditions that make up your workflow:
5072

5173
```php
52-
// app/Workflows/PaymentWorkflow.php
74+
<?php
5375

5476
namespace App\Workflows;
5577

@@ -59,7 +81,7 @@ use App\Actions\ValidateCartItems;
5981
use App\Events\PaymentProcessed;
6082
use App\Jobs\SendEmails;
6183
use App\Observers\UserObserver;
62-
use App\Models\User;
84+
use App\Models\Order;
6385
use Safemood\Workflow\WorkflowManager;
6486

6587
class PaymentWorkflow extends Workflow
@@ -75,62 +97,69 @@ class PaymentWorkflow extends Workflow
7597
// The main action of the workflow
7698
$this->addMainAction(new MakePayment());
7799

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
80102

81103
// Observers to register for specific entities
82104
$this->registerObservers([
83-
User::class => UserObserver::class,
105+
Order::class => OrderObserver::class,
84106
]);
85107

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+
94119
}
95120
}
96121
```
97122

98123
### Execute Workflow
99124

100125
```php
126+
<?php
127+
128+
namespace App\Http\Controllers;
129+
101130
use App\Workflows\PaymentWorkflow;
131+
use Illuminate\Http\Request;
102132

103-
public function checkout()
133+
class PaymentController extends Controller
104134
{
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-
'email' => '[email protected]'
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+
}
134162
}
135163

136-
```
164+
165+
```

0 commit comments

Comments
 (0)