soap/laravel-workflow-process

Provides Laravel workflow guards using Symfony expression and related objects

v0.1.2 2024-09-26 17:40 UTC

This package is auto-updated.

Last update: 2024-12-19 07:34:30 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Using Zerodahero's Laravel Workflow (based on Symfony Workflow) to handle state-transition workflow is great. However, coding transition guards in events is hard. This package provides a simple way; you can add Symfony Expression Language as a transition guard for each transition. This configuration must be provided in transition metadata using the 'guard' key. The package subscribes for all workflows' transition guard events and uses the provided Symfony Expression Language to allow or block the transition.

Support me

Any recommendation or pull request is welcome. It is great if you sponsor me if you find my work valuable.

Installation

You can install the package via composer:

composer require soap/laravel-workflow-process

You can publish the config file with:

php artisan vendor:publish --tag="laravel-workflow-process-config"

This is the contents of the published config file:

return [
];

Usage

Task that you have to do is providing guard configuration like the following example. This is in laravel-workflow 's configuration file (config/workflow.php). If you want to store workflow configuration in database, please visis my Laravel Workflow Loader package.

// file config/workflow.php
use ZeroDaHero\LaravelWorkflow\MarkingStores\EloquentMarkingStore;

return [
    'blogPost' => [
        'type' => 'workflow',
        'supports' => [App\Models\BlogPost::class],
        'marking_store' => [
            'property' => 'state',
            'type' => 'single_state',
            'class' => EloquentMarkingStore::class,
        ],
        'places' => ['draft', 'pending_for_review', 'approved', 'rejected', 'published', 'archived'],
        'transitions' => [
            'submit' => [
                'from' => 'draft',
                'to' => 'pending_for_review',
                'metadata' => [
                    'guard' => 'authenticated and subject.isOwnedBy(user)',
                ],
            ],
            'approve' => [
                'from' => 'pending_for_review',
                'to' => 'approved',
            ],
            'reject' => [
                'from' => 'pending_for_review',
                'to' => 'rejected',
            ],
            'publish' => [
                'from' => 'approved',
                'to' => 'published',
            ],
            'archive' => [
                'from' => ['draft', 'rejected'],
                'to' => 'archived',
            ],
        ],
    ]
    
];

Currenty these variables/objects were injected into Symfony Expression Language.

  • "subject" is the Eloquent model which is subject of a workflow
  • "user" is authenticated user.
  • "authenticated" boolean, true if user was authenticated.

So you can call any method on the injected object.

Todo

I have a plan to provide document role for user. For example, some users may be assign as "reviewer" or "approver" for Eloquent model. So we can use something like subject.hasActorRole('reviewer') or subject.canBeReviewedBy(user). Any suggestion is welcome.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.