2024-10-09 16:01:22 +08:00
|
|
|
<?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();
|
2026-01-09 01:10:20 +08:00
|
|
|
|
|
|
|
|
if (!($target instanceof EventInterface)) {
|
|
|
|
|
throw new Exception("Event listen must implement " . EventInterface::class);
|
|
|
|
|
}
|
|
|
|
|
on($this->event, [$target, "process"]);
|
2024-10-09 16:01:22 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-09 01:10:20 +08:00
|
|
|
}
|