Files
kiri-event/EventListener.php
T
2026-01-09 01:10:20 +08:00

40 lines
801 B
PHP

<?php
namespace Kiri\Events;
use Exception;
use Kiri\Di\Interface\InjectTargetInterface;
#[\Attribute]
readonly class EventListener implements InjectTargetInterface
{
/**
* @param string $event
* @throws Exception
*/
public function __construct(public string $event)
{
}
/**
* @param string $class
* @return void
* @throws Exception
*/
public function dispatch(string $class): void
{
// TODO: Implement dispatch() method.
$target = \Kiri::getDi()->getReflectionClass($class)->newInstanceWithoutConstructor();
if (!($target instanceof EventInterface)) {
throw new Exception("Event listen must implement " . EventInterface::class);
}
on($this->event, [$target, "process"]);
}
}