2021-03-31 00:58:18 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
|
|
|
|
|
2021-04-01 10:11:20 +08:00
|
|
|
use Exception;
|
2021-03-31 17:57:42 +08:00
|
|
|
use Snowflake\Event;
|
2021-04-16 15:05:08 +08:00
|
|
|
use Snowflake\Snowflake;
|
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
|
2021-04-19 11:52:23 +08:00
|
|
|
* @param array|null $args
|
2021-03-31 17:57:42 +08:00
|
|
|
* @param bool $async_reload
|
2021-04-19 11:52:23 +08:00
|
|
|
* @throws Exception
|
2021-03-31 00:58:18 +08:00
|
|
|
*/
|
2021-04-19 11:52:23 +08:00
|
|
|
public function __construct(public string $service, public ?array $args = [], public bool $async_reload = true)
|
2021-03-31 00:58:18 +08:00
|
|
|
{
|
2021-04-19 11:52:23 +08:00
|
|
|
if ($this->async_reload !== true) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-27 15:57:50 +08:00
|
|
|
Event::on(Event::SERVER_WORKER_EXIT, function () {
|
2021-04-19 11:52:23 +08:00
|
|
|
Snowflake::app()->remove($this->service);
|
|
|
|
|
});
|
2021-03-31 00:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-03 03:48:52 +08:00
|
|
|
/**
|
|
|
|
|
* @param object $class
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function execute(mixed $class, mixed $method = null): mixed
|
|
|
|
|
{
|
|
|
|
|
$class = ['class' => $class::class];
|
|
|
|
|
if (!empty($this->args)) {
|
|
|
|
|
$class = array_merge($class, $this->args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Snowflake::set($this->service, $class);
|
|
|
|
|
return true; // TODO: Change the autogenerated stub
|
|
|
|
|
}
|
2021-04-16 15:05:08 +08:00
|
|
|
|
2021-03-31 00:58:18 +08:00
|
|
|
}
|