Files
kiri-core/Annotation/LocalService.php
T

57 lines
1.2 KiB
PHP
Raw Normal View History

2021-03-31 00:58:18 +08:00
<?php
namespace Annotation;
2021-04-01 10:11:20 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Event;
2021-08-13 14:58:58 +08:00
use Kiri\Events\EventProvider;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-08-13 14:58:58 +08:00
use Server\Events\OnWorkerExit;
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-08-13 14:58:58 +08:00
$pro = di(EventProvider::class);
$pro->on(OnWorkerExit::class, function () {
di(\Kiri\Di\LocalService::class)->remove($this->service);
},0);
2021-03-31 00:58:18 +08:00
}
2021-05-03 03:48:52 +08:00
/**
* @param object $class
* @param string $method
2021-07-12 17:17:36 +08:00
* @return bool
* @throws Exception
2021-05-03 03:48:52 +08:00
*/
2021-07-12 17:17:36 +08:00
public function execute(mixed $class, mixed $method = null): bool
2021-05-03 03:48:52 +08:00
{
2021-08-13 15:19:07 +08:00
$class = ['class' => $class];
2021-05-03 03:48:52 +08:00
if (!empty($this->args)) {
$class = array_merge($class, $this->args);
}
2021-08-11 01:04:57 +08:00
Kiri::set($this->service, $class);
2021-05-03 03:48:52 +08:00
return true; // TODO: Change the autogenerated stub
}
2021-04-16 15:05:08 +08:00
2021-03-31 00:58:18 +08:00
}