2021-02-09 11:13:43 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2022-01-08 18:49:08 +08:00
|
|
|
namespace Kiri\Annotation;
|
2021-02-09 11:13:43 +08:00
|
|
|
|
|
|
|
|
|
2021-02-22 17:44:24 +08:00
|
|
|
use Exception;
|
2021-08-13 14:58:58 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2021-09-08 00:26:18 +08:00
|
|
|
use Kiri\Kiri;
|
2021-02-09 11:13:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Event
|
2022-01-08 18:49:08 +08:00
|
|
|
* @package Annotation
|
2021-02-09 11:13:43 +08:00
|
|
|
*/
|
2021-03-03 18:35:04 +08:00
|
|
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Event extends Attribute
|
2021-02-09 11:13:43 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-08-29 05:53:47 +08:00
|
|
|
/**
|
|
|
|
|
* Event constructor.
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $params
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(public string $name, public array $params = [])
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $class
|
|
|
|
|
* @param mixed|null $method
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function execute(mixed $class, mixed $method = null): bool
|
|
|
|
|
{
|
2021-09-08 00:26:18 +08:00
|
|
|
$pro = Kiri::getDi()->get(EventProvider::class);
|
2021-08-29 05:53:47 +08:00
|
|
|
if (is_string($class)) {
|
2021-09-08 00:26:18 +08:00
|
|
|
$class = Kiri::getDi()->get($class);
|
2021-08-29 05:53:47 +08:00
|
|
|
}
|
|
|
|
|
$pro->on($this->name, [$class, $method]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-02-09 11:13:43 +08:00
|
|
|
|
|
|
|
|
}
|