0 follower

Final Class Yiisoft\EventDispatcher\Provider\ListenerCollection

InheritanceYiisoft\EventDispatcher\Provider\ListenerCollection

Listener collection stores listeners and is used to configure provider.

See also Yiisoft\EventDispatcher\Provider\Provider.

Public Methods

Hide inherited methods

Method Description Defined By
add() Attaches listener to corresponding event based on the type-hint used for the event argument. Yiisoft\EventDispatcher\Provider\ListenerCollection
getForEvents() Get listeners for event class names specified. Yiisoft\EventDispatcher\Provider\ListenerCollection

Method Details

Hide inherited methods

add() public method

Attaches listener to corresponding event based on the type-hint used for the event argument.

Method signature should be the following:

 function (MyEvent $event): void

Any callable could be used be it a closure, invokable object or array referencing a class or object.

public add( callable $listener, string $eventClassNames ): self
$listener callable
$eventClassNames string
throws InvalidArgumentException

If callable is invalid.

                public function add(callable $listener, string ...$eventClassNames): self
{
    $new = clone $this;
    if ($eventClassNames === []) {
        $eventClassNames = $this->getParameterType($listener);
    }
    foreach ($eventClassNames as $eventClassName) {
        $new->listeners[$eventClassName][] = $listener;
    }
    return $new;
}

            
getForEvents() public method

Get listeners for event class names specified.

public getForEvents( string $eventClassNames ): iterable<callable>
$eventClassNames string

Event class names.

return iterable<callable>

Listeners.

                public function getForEvents(string ...$eventClassNames): iterable
{
    foreach ($eventClassNames as $eventClassName) {
        if (isset($this->listeners[$eventClassName])) {
            yield from $this->listeners[$eventClassName];
        }
    }
}