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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
{
|
2021-04-25 11:22:23 +08:00
|
|
|
$class = ['class' => $handler[0]::class];
|
2021-03-31 00:58:18 +08:00
|
|
|
if (!empty($this->args)) {
|
|
|
|
|
$class = array_merge($class, $this->args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Snowflake::set($this->service, $class);
|
|
|
|
|
|
|
|
|
|
return parent::execute($handler); // TODO: Change the autogenerated stub
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 15:05:08 +08:00
|
|
|
|
2021-03-31 00:58:18 +08:00
|
|
|
}
|