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

Event Subscriber not being fired #725

Closed
hsb1007 opened this issue Feb 19, 2019 · 1 comment
Closed

Event Subscriber not being fired #725

hsb1007 opened this issue Feb 19, 2019 · 1 comment

Comments

@hsb1007
Copy link

hsb1007 commented Feb 19, 2019

Hi I don't seem to be able to get the event subscriber working...

I am using Symfony 4.1 and JMS Serializer 3.1

Below is what I have in /config/services.yaml

App\Event\Subscriber\SerializeSubscriber:
        tags:
            - { name: jms_serializer.event_subscriber }`

And below is my subscriber.

namespace App\Event\Subscriber;
use JMS\Serializer\EventDispatcher\Event;
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
class SerializerSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            array(
                'event' => Events::PRE_SERIALIZE,
                'method' => 'onPreSerialize',
                'format' => 'json', // optional format
                'priority' => 0, // optional priority
            ),
        );
    }

    public function onPreSerialize(PreSerializeEvent $event)
    {
        $test = 1;
    }
}

It does seem to get registered as subscriber in "RegisterEventListenersAndSubscribersPass"

When it actually gets built via 'SerializerBuilder::build' $this->listenersConfigured is set to false (But I am not sure if I am looking at the right place)..

Does anyone have any idea please?

@hsb1007
Copy link
Author

hsb1007 commented Feb 19, 2019

I have resolved this myself..

Please note that I had to also do 'addDefaultListeners()' to include Doctrine Proxy Event Subscribers..

$builder->addDefaultListeners();
        $builder
            ->configureListeners(function(EventDispatcher $dispatcher) {
                $dispatcher->addSubscriber(new SerializationSubscriber($this->tokenStorage));
            })
        ;

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

1 participant