2021-03-31 00:58:18 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
|
|
|
|
|
2021-04-01 10:11:20 +08:00
|
|
|
use Exception;
|
2021-03-31 00:58:18 +08:00
|
|
|
use Snowflake\Snowflake;
|
2021-03-31 17:57:42 +08:00
|
|
|
use Snowflake\Event;
|
2021-03-31 00:58:18 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class LocalService
|
|
|
|
|
* @package Annotation
|
|
|
|
|
*/
|
|
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)] class LocalService extends Attribute
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* LocalService constructor.
|
|
|
|
|
* @param string $service
|
|
|
|
|
* @param array $args
|
2021-03-31 17:57:42 +08:00
|
|
|
* @param bool $async_reload
|
2021-03-31 00:58:18 +08:00
|
|
|
*/
|
2021-03-31 17:57:42 +08:00
|
|
|
public function __construct(public string $service, public array $args = [], public bool $async_reload = true)
|
2021-03-31 00:58:18 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $handler
|
|
|
|
|
* @return mixed
|
2021-04-01 10:11:20 +08:00
|
|
|
* @throws Exception
|
2021-03-31 00:58:18 +08:00
|
|
|
*/
|
|
|
|
|
public function execute(array $handler): mixed
|
|
|
|
|
{
|
|
|
|
|
$class = ['class' => get_class($handler[0])];
|
|
|
|
|
if (!empty($this->args)) {
|
|
|
|
|
$class = array_merge($class, $this->args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Snowflake::set($this->service, $class);
|
2021-03-31 17:57:42 +08:00
|
|
|
if ($this->async_reload === true) {
|
|
|
|
|
$event = Snowflake::app()->getEvent();
|
2021-04-16 15:04:25 +08:00
|
|
|
$event->on(Event::SERVER_WORKER_EXIT, function () {
|
2021-03-31 17:57:42 +08:00
|
|
|
Snowflake::app()->remove($this->service);
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-03-31 00:58:18 +08:00
|
|
|
|
|
|
|
|
return parent::execute($handler); // TODO: Change the autogenerated stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|