remils / event-listener
Слушатель событий
1.1.0
2023-10-29 00:53 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpstan/phpstan: ^1.10
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-12-29 04:21:46 UTC
README
Реализация простого слушателя событий.
Пример использования
<?php use Remils\EventListener\AttachDispatcherInterface; use Remils\EventListener\Dispatcher; use Remils\EventListener\EventInterface; use Remils\EventListener\ListenerInterface; use Remils\EventListener\SubscriberInterface; require './vendor/autoload.php'; class Entity { public string $name; public int $age; } class SetAgeEntityEvent implements EventInterface { public int $age; } class SetNameEntityEvent implements EventInterface { public string $name; } class SendEntityEvent implements EventInterface { } class ResponseEntityEvent implements EventInterface { public Entity $entity; } class EntitySubscriber implements SubscriberInterface, AttachDispatcherInterface { protected Dispatcher $dispatcher; protected Entity $entity; public function __construct() { $this->entity = new Entity(); } public function setDispatcher(Dispatcher $dispatcher): void { $this->dispatcher = $dispatcher; } public function getSubscribedEvents(): array { return [ SetAgeEntityEvent::class => 'setAge', SetNameEntityEvent::class => 'setName', SendEntityEvent::class => 'sendEntity', ]; } public function setAge(SetAgeEntityEvent $event): void { $this->entity->age = $event->age; } public function setName(SetNameEntityEvent $event): void { $this->entity->name = $event->name; } public function sendEntity(SendEntityEvent $event): void { $event = new ResponseEntityEvent(); $event->entity = $this->entity; $this->dispatcher->dispatch($event); } } class ResponseEntityListener implements ListenerInterface { public function getEventNamespace(): string { return ResponseEntityEvent::class; } /** * @param ResponseEntityEvent $event * @return void */ public function handle(EventInterface $event): void { var_dump($event->entity); } } $listeners = [ new EntitySubscriber(), new ResponseEntityListener(), ]; $dispatcher = new Dispatcher($listeners); $event = new SetAgeEntityEvent(); $event->age = 64; $dispatcher->dispatch($event); $event = new SetNameEntityEvent(); $event->name = 'Анатолий'; $dispatcher->dispatch($event); $event = new SendEntityEvent(); $dispatcher->dispatch($event);