Search code examples
symfonysymfony2-easyadmin

EasyAdminBundle event POST_INITIALIZE is Undefined class constant


The EasyAdminBundle uses FOSUserBundle user manipulator to create & manage users. I want to capture the password used when a user is created to update a postfix database. When a user is created the dev profiler does not show that any FOSUserBundle event was listened to. The only EasyAdminBundle event was easy_admin.post_initialize. The EasyAdminEvents class shows

/** @Event("Symfony\Component\EventDispatcher\GenericEvent") */
const POST_INITIALIZE = 'easy_admin.post_initialize';

But an event listener with:

use Symfony\Component\EventDispatcher\GenericEvent;
...
    public static function getSubscribedEvents()
    {
        return array(
            GenericEvent::POST_INITIALIZE => 'onPostInitialize',
        );
    }

throws:

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Undefined class constant 'POST_INITIALIZE' ...

I thought the answer was to use a JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents event rather than a Symfony GenericEvent. As in

use JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
...
    public static function getSubscribedEvents()
    {
        return array(
            EasyAdminEvents::POST_INITIALIZE => 'onPostInitialize',
        );
    }
...
//error occurs on the following line:
    public function onPostInitialize(EasyAdminEvents $event)
    {
        ...
    }

However, doing so gave me this Catch 22:

must be an instance of JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents, instance of Symfony\Component\EventDispatcher\GenericEvent given"


Solution

  • So the simplest answer was to change the type hint as in:

        public function onPostInitialize(GenericEvent $event)
        {
            ...
        }