Zend 2 EventManager in legacy code

64 Views Asked by At

We are in middle from retrofitting our 10 year old system (php 3.0 code) to Zend 2

The progress is messy but we are finally on the step where we want to use events in development. We are already using them but for some strange reason dous the event manager not register our event calls when we use him outside of a class like in:

$eventManager = $serviceManager->get('EventManager');
$cartId = getvar('showcartid');
$eventManager->trigger('MainEvent.PreRender');

But when we use him with the

$eventManager->addIdentifiers('MainEvent');

Function call so we add the identifiers manually he then accepts the calls.

$eventManager = $serviceManager->get('EventManager');
$eventManager->addIdentifiers('MainEvent');
$cartId = getvar('showcartid');
$eventManager->trigger('MainEvent.PreRender');

The problem is when we use to many of them... it gets messy in the identifiers...

Anyone has any idea how we could make the event trigger so he fins the global instance?

Here is our base listener class.

namespace DIG\Listener;

use Zend\EventManager\EventManagerInterface;

class ListenerConfigAware implements ListenerConfigAwareInterface
{

    protected $eventsConfig;
    protected $identifier;

    public function setEventConfig(array $eventConfigs)
    {
        $this->eventsConfig = $eventConfigs;
    }

    public function setIdentifier($identifier)
    {
        $this->identifier = $identifier;
    }

    public function attach(EventManagerInterface $events)
    {
        foreach ($this->eventsConfig as $eventConfig) {
            if (is_array($this->identifier)) {
                foreach ($this->identifier as $identifier) {
                    $this->listeners[] = $events->attach("$identifier." . $eventConfig['name'], array($this, $eventConfig['method']), $eventConfig['priority']);
                }
            } else {
                $this->listeners[] = $events->attach($this->identifier . "." . $eventConfig['name'], array($this, $eventConfig['method']), $eventConfig['priority']);
            }
        }

        return $this;
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $key => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$key]);
            }
        }
    }

}

I think the problem is with the namespace, but we have to have one for the event to be found from the service manager....

0

There are 0 best solutions below