Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Eventmanager Laravel Package

zendframework/zend-eventmanager

Zend\EventManager provides a lightweight, flexible event and listener system for PHP apps. Attach and detach listeners, trigger events with priorities, and use shared managers for cross-cutting events—ideal for decoupling components in Zend Framework and beyond.

View on GitHub
Deep Wiki
Context7

Listener Aggregates

Listener aggregates exist to facilitate two operations:

  • Attaching many listeners at once.
  • Attaching stateful listeners.

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:

  • Zend\EventManager\AbstractListenerAggregate is an abstract class that defines the $listeners property and the detach() method. You may extend it in order to create an implementation.
  • Zend\EventManager\ListenerAggregateTrait is a trait that defines the $listeners property and the detach() method. You may implement Zend\EventManager\ListenerAggregateInterface and use this trait to implement the detach() logic.

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

  • We recommend using listener aggregates when you have several listeners that are related and/or share common dependencies and/or business logic. This helps keep the logic in the same location, and helps reduce dependencies.

  • We recommend using the verbiage on<Event Name> to name your listener methods. This helps hint that they will be triggered on an event, and semantically ties them to the specific event name.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope