zerodahero / laravel-workflow

Use the Symfony Workflow component in Laravel
MIT License
197 stars 37 forks source link

Events not working for me #94

Closed juliangums closed 6 months ago

juliangums commented 6 months ago

I have a state machine workflow and I defined a listener with some events for it but I can't get them to work. I found this in the readme: // Specifies events to dispatch (only in 'workflow', not 'state_machine')

Does this mean there are no events for state machines? How can I make them work? I do need additional guard logic etc on them.

juliangums commented 6 months ago

I didn't register the subscriber class in a service provider. This sorted it:

    public function boot(): void
    {
        Event::subscribe(WorkflowSubscriber::class);
    }

Don't quite understand what the above sentence means, then. Does it mean state machines always fire all events?

zerodahero commented 6 months ago

This is a specific Symfony Workflow feature. The full comment should offer some more context:

// Specifies events to dispatch (only in 'workflow', not 'state_machine')
// - set `null` to dispatch all events (default, if omitted)
// - set to empty array (`[]`) to dispatch no events
// - set to array of events to dispatch only specific events
// Note that announce will dispatch a guard event on the next transition
// (if announce isn't dispatched the next transition won't guard until checked/applied)
'events_to_dispatch' => [ ... ]

The feature in the Symfony workflow is discussed more in-depth here: https://symfony.com/doc/current/workflow.html#choosing-which-events-to-dispatch

And, for state machines, it looks like that feature wasn't supported properly in the component itself when that was originally written to this package, but has since been updated/fixed (see upstream commit, here). If you need to specify which events are dispatched for a state machine, we'll need to add that in to this package (should be only a couple line change, if that).

So, yes, you are correct in that state machines currently fire all events.

Did that help clarify things?

juliangums commented 6 months ago

Yes, that clarifies it. I did get it to work but for some reason after some time it froze the whole application or filled up memory. I ended up removing the event listener adding all the guard logic into policies instead