2017 © Pedro Peláez
 

library laravel-workflow

Integerate Symfony Workflow component into Laravel.

image

brexis/laravel-workflow

Integerate Symfony Workflow component into Laravel.

  • Wednesday, July 25, 2018
  • by brexis
  • Repository
  • 10 Watchers
  • 66 Stars
  • 7,854 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 31 Forks
  • 6 Open issues
  • 12 Versions
  • 48 % Grown

The README.md

Laravel workflow Build Status

Use the Symfony Workflow component in Laravel, (*1)

Installation

composer require brexis/laravel-workflow

For laravel <= 5.4

Add a ServiceProvider to your providers array in config/app.php:, (*2)

<?php

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

]

Add the Workflow facade to your facades array:, (*3)

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

Configuration

Publish the config file, (*4)

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

Configure your workflow in config/workflow.php, (*5)

<?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, (*6)

<?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, (*7)

    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, (*8)

<?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, (*9)

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., (*10)

php artisan workflow:dump workflow_name --format=jpg

The Versions

01/12 2017

1.1.2

1.1.2.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

17/11 2017

1.1.1

1.1.1.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

17/11 2017

1.1.0

1.1.0.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

06/06 2017

1.0.4

1.0.4.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

17/02 2017

1.0.3

1.0.3.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

15/02 2017

1.0.2

1.0.2.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

13/02 2017

1.0.1

1.0.1.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow

13/02 2017

1.0.0

1.0.0.0

Integerate Symfony Workflow component into Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel symfony laravel5 workflow