Final Class Yiisoft\EventDispatcher\Provider\ListenerCollection
| Inheritance | Yiisoft\EventDispatcher\Provider\ListenerCollection |
|---|
Listener collection stores listeners and is used to configure provider.
Public 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
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;
}
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];
}
}
}
Signup or Login in order to comment.