Giter Site home page Giter Site logo

laravel-workflow's Introduction

Laravel workflow Build Status

Use the Symfony Workflow component in Laravel8,PHP7,PHP8 This repository based on @brexis,his project since 2019-09 No longer maintained. So i modify the code and publish it.

Installation

composer require cage/laravel-workflow

Configuration

Publish the config file

    php artisan vendor:publish --provider="Cage\LaravelWorkflow\WorkflowServiceProvider"

Configure your workflow in config/workflow.php

Use the WorkflowTrait inside supported classes

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Cage\LaravelWorkflow\Traits\WorkflowTrait;

class BlogPost extends Model
{
  use WorkflowTrait;
}

Usage

<?php

use App\Models\BlogPost;
use Workflow;

$post        = BlogPost::find(1);
$workflow    = $post->workflow_get();// or $workflow = Workflow::get($post);
$workflow->can($post, 'publish'); //  False
$workflow->can($post, 'to_review'); // True
$transitions = $workflow->getEnabledTransitions($post);// Get the transitions
$definition  = $workflow->getDefinition();// Get the definition
$places      = $workflow->getMarking($post)->getPlaces();// // Get the current places
$metadata    = $workflow->getMetadataStore();// Get the metadata

// Apply a transition
$workflow->apply($post, 'to_review');
$post->save(); // Don't forget to persist the state

Use the events

Register at app/Providers/EventServiceProvider.php

protected $subscribe = [
        BlogPostWorkflowSubscriber::class,
    ];

Then you can subscribe to an event Create Listener at app/Listeners/BlogPostWorkflowSubscriber.php

<?php

namespace App\Listeners;

use Cage\LaravelWorkflow\Events\GuardEvent;

class BlogPostWorkflowSubscriber
{
    public function onGuard(GuardEvent $event){}

    public function onLeave($event)
    {
        // The event can also proxy to the original event
        $subject = $event->getOriginalEvent()->getSubject();
    }

    public function onTransition($event) {}

    public function onEnter($event) {}

    public function onEntered($event) {}

    public function subscribe($events)
    {
        $events->listen(
            'Cage\LaravelWorkflow\Events\GuardEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onGuard'
        );

        $events->listen(
            'Cage\LaravelWorkflow\Events\LeaveEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onLeave'
        );

        $events->listen(
            'Cage\LaravelWorkflow\Events\TransitionEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onTransition'
        );

        $events->listen(
            'Cage\LaravelWorkflow\Events\EnterEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onEnter'
        );

        $events->listen(
            'Cage\LaravelWorkflow\Events\EnteredEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onEntered'
        );
    }

}

Dump Workflows

Symfony workflow uses GraphvizDumper to create the workflow image. You may need to install the dot command of Graphviz

php artisan workflow:dump straight --class App\\Models\\BlogPost --path workflows  --format=svg

For More Information

https://symfony.com/doc/current/workflow.html https://github.com/brexis/laravel-workflow

laravel-workflow's People

Stargazers

 avatar Kishore Chandra Sahoo avatar Patrick ER avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.