59 lines
1.1 KiB
PHP
59 lines
1.1 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);
|
|
}
|
|
|
|
var_dump($this->service, $class);
|
|
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);
|
|
}
|
|
|
|
}
|