55 lines
1.1 KiB
PHP
55 lines
1.1 KiB
PHP
<?php
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
use Exception;
|
|
use Kiri\Event;
|
|
use Kiri\Kiri;
|
|
|
|
/**
|
|
* Class LocalService
|
|
* @package Annotation
|
|
*/
|
|
#[\Attribute(\Attribute::TARGET_CLASS)] class LocalService extends Attribute
|
|
{
|
|
|
|
|
|
/**
|
|
* LocalService constructor.
|
|
* @param string $service
|
|
* @param array|null $args
|
|
* @param bool $async_reload
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(public string $service, public ?array $args = [], public bool $async_reload = true)
|
|
{
|
|
if ($this->async_reload !== true) {
|
|
return;
|
|
}
|
|
Event::on(Event::SERVER_WORKER_EXIT, function () {
|
|
Kiri::app()->remove($this->service);
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* @param object $class
|
|
* @param string $method
|
|
* @return bool
|
|
* @throws Exception
|
|
*/
|
|
public function execute(mixed $class, mixed $method = null): bool
|
|
{
|
|
$class = ['class' => $class::class];
|
|
if (!empty($this->args)) {
|
|
$class = array_merge($class, $this->args);
|
|
}
|
|
|
|
Kiri::set($this->service, $class);
|
|
return true; // TODO: Change the autogenerated stub
|
|
}
|
|
|
|
}
|