Files
kiri-core/Annotation/LocalService.php
T
2021-04-16 15:05:08 +08:00

58 lines
1.0 KiB
PHP

<?php
namespace Annotation;
use Exception;
use Snowflake\Event;
use Snowflake\Snowflake;
/**
* Class LocalService
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class LocalService extends Attribute
{
/**
* LocalService constructor.
* @param string $service
* @param array $args
* @param bool $async_reload
*/
public function __construct(public string $service, public array $args = [], public bool $async_reload = true)
{
}
/**
* @param array $handler
* @return mixed
* @throws Exception
*/
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);
if ($this->async_reload === true) {
$event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_EXIT, [$this, 'clear']);
}
return parent::execute($handler); // TODO: Change the autogenerated stub
}
public function clear()
{
Snowflake::app()->remove($this->service);
}
}