Final Class Yiisoft\Yii\Event\ListenerCollectionFactory
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| create() | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $injector | \ |
|
| $callableFactory | Yiisoft\ |
|
public function __construct(
private readonly Injector $injector,
private readonly CallableFactory $callableFactory,
) {}
| public \ | ||
| $eventListeners | array |
Event listener list in format ['eventName1' => [$listener1, $listener2, ...]] |
public function create(array $eventListeners): ListenerCollection
{
$listenerCollection = new ListenerCollection();
foreach ($eventListeners as $eventName => $listeners) {
if (!is_string($eventName)) {
throw new InvalidEventConfigurationFormatException(
'Incorrect event listener format. Format with event name must be used.',
);
}
if (!is_iterable($listeners)) {
$type = get_debug_type($listeners);
throw new InvalidEventConfigurationFormatException(
"Event listeners for $eventName must be an iterable, $type given.",
);
}
foreach ($listeners as $callable) {
$listener
= fn(object $event): mixed => $this->injector->invoke(
$this->callableFactory->create($callable),
[$event],
);
$listenerCollection = $listenerCollection->add($listener, $eventName);
}
}
return $listenerCollection;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.