Listener Aggregates

Listener aggregates exist to facilitate two operations:

A listener aggregate is a class implementing Zend\EventManager\ListenerAggregateInterface, which defines two methods:

attach(EventManagerInterface $events, $priority = 1);
detach(EventManagerInterface $events);

To attach an aggregate to an event manager, you pass the event manager to the aggregate's attach() method; in that method, you will then attach listeners to the events you are interested in.

Implementation

To implement ListenerAggregateInterface, you need to define the attach() and detach() methods. A typical implementation will look something like this:

use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;

class Aggregate implements ListenerAggregateInterface
{
    private $listeners = [];

    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach('something', [$this, 'onSomething'], $priority);
        $this->listeners[] = $events->attach('else', [$this, 'onElse'], $priority);
        $this->listeners[] = $events->attach('again', [$this, 'onAgain'], $priority);
    }

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

    public function onSomething(EventInterface $event)
    {
        // handle event
    }

    public function onElse(EventInterface $event)
    {
        // handle event
    }

    public function onAgain(EventInterface $event)
    {
        // handle event
    }
}

Because the logic for detaching is essentially the same in all implementations, we provide two facilities for implementing this:

Usage

To use an aggregate listener, you need to attach it to the event manager. As noted in the intro to this section, you do so by passing the event manager to the aggregate's attach() method:

// Assume $events is an EventManager instance, and $aggregate is an instance of
// the Aggregate class defined earlier.
$aggregate->attach($events);

Recommendations