Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/safemood/laravel-workflow
Laravel package that simplifies workflows with clear actions definition and event tracking.
https://github.com/safemood/laravel-workflow
laravel laravel-framework laravel-package
Last synced: about 1 month ago
JSON representation
Laravel package that simplifies workflows with clear actions definition and event tracking.
- Host: GitHub
- URL: https://github.com/safemood/laravel-workflow
- Owner: Safemood
- License: mit
- Created: 2024-07-04T10:53:33.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-09-27T07:05:09.000Z (about 2 months ago)
- Last Synced: 2024-10-11T11:03:09.300Z (about 1 month ago)
- Topics: laravel, laravel-framework, laravel-package
- Language: PHP
- Homepage: https://kbouzidi.com/payment-is-more-than-just-payment-laravel-workflow
- Size: 75.2 KB
- Stars: 149
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Workflow
[![Latest Version on Packagist](https://img.shields.io/packagist/v/safemood/laravel-workflow?style=flat-square&color=blue
)](https://packagist.org/packages/safemood/laravel-workflow)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/safemood/laravel-workflow/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/safemood/laravel-workflow/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/safemood/laravel-workflow/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/safemood/laravel-workflow/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/safemood/laravel-workflow.svg?style=flat-square)](https://packagist.org/packages/safemood/laravel-workflow)Laravel Workflow combines feature details into one class, allowing for action definition and event tracking, simplifying understanding and revealing hidden logic.
- [Laravel Workflow](#laravel-workflow)
- [Installation](#installation)
- [Create a Workflow](#create-a-workflow)
- [Create Actions](#create-actions)
- [Basic Example](#basic-example)
- [Define Workflow Logic](#define-workflow-logic)
- [Execute Workflow](#execute-workflow)
- [Conditional Action Execution](#conditional-action-execution)## Installation
You can install the package via Composer:
```bash
composer require safemood/laravel-workflow
```## Create a Workflow
You can create a workflow using the artisan command:
```bash
php artisan make:workflow PaymentWorkflow
```## Create Actions
You can create an action using the artisan command:
```bash
php artisan make:workflow-action ValidateCartItems
``````php
addBeforeActions([
new ValidateCartItems(),
new CalculateTotal()
]);// The main action of the workflow
$this->addMainAction(new MakePayment());// Actions to be executed after the main action
$this->addAfterAction(new SendEmails()); // Normal laravel Job in this example// Observers to register for specific entities
$this->registerObservers([
Order::class => OrderObserver::class,
]);// Good Debugging or if you want to understand what is happining during the workflow execution:
$this->trackEvents([
PaymentProcessed::class
]);
// $this->trackAllEvents(); // or// $this->trackEventsIn('App\Events\\');
}
}
```### Execute Workflow
```php
[
['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2],
['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1]
],
'user_id' => 123
];// Execute the PaymentWorkflow with the provided context
$paymentWorkflow = (new PaymentWorkflow)->run($context);// Check if the workflow execution was successful
$success = $paymentWorkflow->passes();// Check if the workflow execution failed
$failure = $paymentWorkflow->failed();// Dump the workflow for debugging
// $paymentWorkflow->dd();// Handle the response based on the workflow outcome
if ($success) {
return $paymentWorkflow->successResponse();
}return $paymentWorkflow->failureResponse();
}
}```
## Conditional Action Execution
You can use the when method to conditionally execute an action.
```php
when(false, function () {
$this->trackAllEvents();
});$this->when(true, function () {
$this->registerObservers([
DummyModel::class => DummyModelObserver::class,
]);
});$this->when(true, function () {
$this->addBeforeActions([
new DummyAction(),
new DummyAction(),
]);
});$this->unless(
value: false,
callback: fn () => $this->trackAllEvents(),
default: fn () => $this->doSomething()
);
}
}
```