Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony Events require different event listener connection than normal #159

Open
BurningDog opened this issue Nov 11, 2020 · 1 comment
Open

Comments

@BurningDog
Copy link

BurningDog commented Nov 11, 2020

I'm using Symfony 4.4. The documentation at https://finite.readthedocs.io/en/master/usage/symfony.html#using-callbacks say:

Events dispatched with the EventDispatcher and works as the Symfony kernel events.

This would seem to imply that one can simply follow the standard Symfony ways of registering an event listener. i.e. using services.yaml or Creating an Event Subscriber.

However, neither of those work, because Finite creates its own event dispatcher! When a new StateMachine is created, it creates a new StateMachineDispatcher() which creates...a new Symfony\Component\EventDispatcher\EventDispatcher.

So any Symfony event registered in the normal way will not listen to the event.

Solution

We have to connect our event listener to the StateMachine's dispatcher.

$sm = new StateMachine();
$dispatcher = $sm->getDispatcher();
$dispatcher->addListener(FiniteEvents::POST_TRANSITION, [new MyPostTransitionListener(), 'onFinitePostTransition']);
class MyPostTransitionListener
{
    public function onFinitePostTransition(\Finite\Event\TransitionEvent $event)
    {
        // handle event here
    }
}
@amadeot
Copy link

amadeot commented Nov 17, 2020

Fixed by #158

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants