Files
kiri-core/note/LocalService.php
T

55 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-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-08-29 04:06:49 +08:00
public function __construct(string $service, ?array $args = [], bool $async_reload = true)
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-08-29 04:06:49 +08:00
public static function execute(mixed $params, 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-08-29 04:06:49 +08:00
if (!empty($params->args)) {
$class = array_merge($class, $params->args);
2021-05-03 03:48:52 +08:00
}
2021-08-29 04:06:49 +08:00
if ($params->async_reload !== true) {
$pro = di(EventProvider::class);
$pro->on(OnWorkerExit::class, function () use ($params) {
di(\Kiri\Di\LocalService::class)->remove($params->service);
},0);
}
Kiri::set($params->service, $class);
return true;
2021-05-03 03:48:52 +08:00
}
2021-04-16 15:05:08 +08:00
2021-03-31 00:58:18 +08:00
}