All Projects → brexis → laravel-workflow

brexis / laravel-workflow

Licence: MIT license
Use the Symfony Workflow component in Laravel

Projects that are alternatives of or similar to laravel-workflow

laravel-fiverrclone
Full functional fiverr clone
Stars: ✭ 23 (-91.61%)
Mutual labels:  laravel5
laravel-make-me
Extendable Interactive Make Command for Laravel
Stars: ✭ 36 (-86.86%)
Mutual labels:  laravel5
arisan
Sistem pengelolaan grup arisan berbasis web yang dibangun dengan Laravel 5.
Stars: ✭ 24 (-91.24%)
Mutual labels:  laravel5
laravel-hijri-dates
Hijri dates for Laravel
Stars: ✭ 25 (-90.88%)
Mutual labels:  laravel5
generator
A Laravel Module Generator Package for Laravel AdminPanel <https://github.com/viralsolani/laravel-adminpanel>
Stars: ✭ 19 (-93.07%)
Mutual labels:  laravel5
OfficeManagementSystem
A system that helps to perform day to day activities of an office including attendance management, task management, leave management, complaint management, etc.
Stars: ✭ 32 (-88.32%)
Mutual labels:  laravel5
laravel-5.3-app
🗺️ Get started with Laravel 5.3, Vue.js and Google Maps API
Stars: ✭ 28 (-89.78%)
Mutual labels:  laravel5
solder
Supercharge Your Modpack with Solder
Stars: ✭ 27 (-90.15%)
Mutual labels:  laravel5
captcha
😁一个Laravel5使用的简单图形验证码组件包
Stars: ✭ 13 (-95.26%)
Mutual labels:  laravel5
laravel5Angular4
Laravel 5.4 & Angular 4.3.4
Stars: ✭ 37 (-86.5%)
Mutual labels:  laravel5
laravel-adminlte-boilerplate
Laravel 5.6+ AdminLTE
Stars: ✭ 45 (-83.58%)
Mutual labels:  laravel5
Segnalibro
Save and comment your favorite links from the web. It's just a bookmarking application.
Stars: ✭ 14 (-94.89%)
Mutual labels:  laravel5
php-framework-benchmark
php framework benchmark (include laravel、symfony、silex、lumen、slim、yii2、tastphp etc)
Stars: ✭ 17 (-93.8%)
Mutual labels:  laravel5
generator-laravel-5
Scaffold Laravel 5.7 applications with ease.
Stars: ✭ 19 (-93.07%)
Mutual labels:  laravel5
laravel-vue-starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 80 (-70.8%)
Mutual labels:  laravel5
Laravel-5.3-Repository
Simple repository setup for Laravel 5.3
Stars: ✭ 17 (-93.8%)
Mutual labels:  laravel5
MyVideoManager
A Laravel and Vuejs application for managing Youtube videos
Stars: ✭ 15 (-94.53%)
Mutual labels:  laravel5
opencore
OpenCore - Laravel for OpenCart
Stars: ✭ 42 (-84.67%)
Mutual labels:  laravel5
nginx-virtual-host-bash-script
Nginx Virtual Host Bash Script
Stars: ✭ 35 (-87.23%)
Mutual labels:  laravel5
december-2018-meetup
🤖 Build an API with Laravel 5.7
Stars: ✭ 27 (-90.15%)
Mutual labels:  laravel5

Laravel workflow Build Status

Use the Symfony Workflow component in Laravel

Installation

composer require brexis/laravel-workflow

For laravel <= 5.4

Add a ServiceProvider to your providers array in config/app.php:

<?php

'providers' => [
    ...
    Brexis\LaravelWorkflow\WorkflowServiceProvider::class,

]

Add the Workflow facade to your facades array:

<?php
    ...
    'Workflow' => Brexis\LaravelWorkflow\Facades\WorkflowFacade::class,

Configuration

Publish the config file

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

Configure your workflow in config/workflow.php

<?php

return [
    'straight'   => [
        'type'          => 'workflow', // or 'state_machine'
        'marking_store' => [
            'type'      => 'multiple_state',
            'arguments' => ['currentPlace']
        ],
        'supports'      => ['App\BlogPost'],
        'places'        => ['draft', 'review', 'rejected', 'published'],
        'transitions'   => [
            'to_review' => [
                'from' => 'draft',
                'to'   => 'review'
            ],
            'publish' => [
                'from' => 'review',
                'to'   => 'published'
            ],
            'reject' => [
                'from' => 'review',
                'to'   => 'rejected'
            ]
        ],
    ]
];

Use the WorkflowTrait inside supported classes

<?php

namespace App;

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

class BlogPost extends Model
{
  use WorkflowTrait;

}

Usage

<?php

use App\BlogPost;
use Workflow;

$post = BlogPost::find(1);
$workflow = Workflow::get($post);
// if more than one workflow is defined for the BlogPost class
$workflow = Workflow::get($post, $workflowName);

$workflow->can($post, 'publish'); // False
$workflow->can($post, 'to_review'); // True
$transitions = $workflow->getEnabledTransitions($post);

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

// Using the WorkflowTrait
$post->workflow_can('publish'); // True
$post->workflow_can('to_review'); // False

// Get the post transitions
foreach ($post->workflow_transitions() as $transition) {
    echo $transition->getName();
}
// if more than one workflow is defined for the BlogPost class
foreach ($post->workflow_transitions($workflowName) as $transition) {
    echo $transition->getName();
}

// Apply a transition
$post->workflow_apply('publish');
$post->save();

Use the events

This package provides a list of events fired during a transition

    Brexis\LaravelWorkflow\Events\Guard
    Brexis\LaravelWorkflow\Events\Leave
    Brexis\LaravelWorkflow\Events\Transition
    Brexis\LaravelWorkflow\Events\Enter
    Brexis\LaravelWorkflow\Events\Entered

You can subscribe to an event

<?php

namespace App\Listeners;

use Brexis\LaravelWorkflow\Events\GuardEvent;

class BlogPostWorkflowSubscriber
{
    /**
     * Handle workflow guard events.
     */
    public function onGuard(GuardEvent $event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();

        /** @var App\BlogPost $post */
        $post = $originalEvent->getSubject();
        $title = $post->title;

        if (empty($title)) {
            // Posts with no title should not be allowed
            $originalEvent->setBlocked(true);
        }
    }

    /**
     * Handle workflow leave event.
     */
    public function onLeave($event) {}

    /**
     * Handle workflow transition event.
     */
    public function onTransition($event) {}

    /**
     * Handle workflow enter event.
     */
    public function onEnter($event) {}

    /**
     * Handle workflow entered event.
     */
    public function onEntered($event) {}

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'Brexis\LaravelWorkflow\Events\GuardEvent',
            'App\Listeners\BlogPostWorkflowSubscriber@onGuard'
        );

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

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

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

        $events->listen(
            'Brexis\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 workflow_name --class App\\BlogPost

You can change the image format with the --format option. By default the format is png.

php artisan workflow:dump workflow_name --format=jpg
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].