brexis / laravel-workflow

Use the Symfony Workflow component in Laravel
MIT License
281 stars 105 forks source link

Unable to listen transition guard event, and How guard is working here? #63

Open pavan-cs opened 5 years ago

pavan-cs commented 5 years ago

i follow the read me instruction, #53 still not able to connect how will it listen the events. here is my listner,

<?php 
namespace App\Listeners;

use Brexis\LaravelWorkflow\Events\GuardEvent;

class ApprovalWorkflowListeners
{
    /**
     * Handle workflow guard events.
     */
    public function onGuard(GuardEvent $event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
        /** @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) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow transition event.
     */
    public function onTransition($event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow enter event.
     */
    public function onEnter($event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow entered event.
     */
    public function onEntered($event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

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

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

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

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

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

};

here my workflow config

<?php

return [
    'approval_workflow'   => [
        'type'          => 'workflow', //'state_machine',
        'marking_store' => [
            'type' => 'single_state', // 'multiple_state',
            'arguments' => ['status'],
        ],
        'supports'      => ['App\Plan'],
        'places'        => ['new', 'submitted', 'in_review_work', 'pending_for_review_l1', 'approved_l1', 'review_comments', 'rejected', 'pending_for_review_l2', 'approved_l2'],
        'transitions'   => [
            'submit' => [
                // ''
                'from' => 'new',
                'to'   => 'submitted',
            ],

            'review-pending-l1' =>[
                // 'guard'=> todo
                'from' => 'submitted',
                'to'   => 'pending_for_review_l1',
            ],

            'approve-l1' => [
                // 'guard'=> todo
                'from' => ['pending_for_review_l1'],
                'to'   => 'approved_l1',
            ],

            'review-pending-l2' => [
                'from' => ['approved_l1'],
                'to'   => 'pending_for_review_l2',
            ],

            'approve-l2' => [
                'from' => ['pending_for_review_l2'],
                'to'   => 'approved_l2',
            ],

            'review-comment' => [
                // 'guard'=>
                'from' => ['pending_for_review_l1','pending_for_review_l2'],
                'to'   => 'review_comments',
            ],

            'review-work' => [
                'from' => ['submitted','pending_for_review_l1','pending_for_review_l2','review_comments','rejected'],
                'to' => 'in_review_work',
            ],

            'reject' => [
                'from' => ['submitted','pending_for_review_l1','pending_for_review_l2'],
                'to' => 'rejected',
            ]
        ],
    ]
];

does event trigger automatically ? In ApprovalWorkflowListeners onGuard should be called, i could validate and block the txn but no luck.

or we have to define in config like (symphony) 'review-pending-l1' =>[ 'guard'=> "has_role('customer')", 'from' => 'submitted', 'to' => 'pending_for_review_l1', ], second, how can i implement guard in my workflow based on user role. ex. if user has customer role he will see only submit only, and if user has managed role he will see approve, review, reject etc

fabien44300 commented 4 years ago

Hello,

In App\Providers\EventServiceProvider file, you have to add your listener class into $subscribe : protected $subscribe = [ 'App\Listeners\ApprovalWorkflowListeners', ];

Fabien