Hi Marc,

you can use the shared event manager like this (reduced to one form):

--------------------------------------------------------------------------
/module/User/Module.php

class Module
{
    protected $serviceLocator = null;

    public function onBootstrap(EventInterface $e)
    {
        [...]

        $this->serviceLocator = $e->getApplication()
                                  ->getServiceManager();

        $sharedEventManager = $this->serviceLocator
                                   ->get('SharedEventManager');

        $sharedEventManager->attach(
            'User\Service\User',
            'set-user-register-form',
            array($this, 'onRegisterForm')
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'User\Form\UserRegister' => 'User\Form\UserRegister',
                'User\Service\User'      => 'User\Service\UserFactory',
            ),
        );
    }

    public function onRegisterForm(EventInterface $e)
    {
        $service = $this->serviceLocator->get('User\Service\User');
        $form    = $this->serviceLocator->get('User\Form\UserRegister');
        $service->setRegisterForm($form);
    }
}
--------------------------------------------------------------------------

In the onBootstrap() method I attach the event 'set-user-register-form'
to my service 'User\Service\User'. When this event is triggered, then
the onRegisterForm() method of the Module object should be called. The
onRegisterForm() method just takes the user service and the form and
calls the setRegisterForm() method of my user service.

--------------------------------------------------------------------------
/module/User/src/User/Service/User.php

class User implements EventManagerAwareInterface
{
    protected $eventManager = null;

    protected $registerForm;

    public function setEventManager(EventManagerInterface $eventManager)
    {
        $eventManager->setIdentifiers(array(__CLASS__));
        $this->eventManager = $eventManager;
    }

    public function getEventManager()
    {
        return $this->eventManager;
    }

    public function getRegisterForm()
    {
        if (null === $this->registerForm) {
            $this->getEventManager()->trigger('set-user-register-form');

            $this->setMessage('user_message_info_user_add');
        }

        return $this->registerForm;
    }

    public function setRegisterForm(UserSaveForm $form)
    {
        $this->registerForm = $form;
    }
}

--------------------------------------------------------------------------

With the  implementation of EventManagerAwareInterface the Event Manager
is injected to my user service automatically. When I call the
getRegisterForm() method of my user service for the first time, the
event 'set-user-register-form' is triggered. Now the event manager calls
Module::onRegisterForm() which creates my register form and passes it to
my user service.

You can build this for all forms you have to just create form instances
when you really need it.

Regards,

Ralf

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to