diff --git a/Annotation/LocalService.php b/Annotation/LocalService.php index 3714c244..e55bb653 100644 --- a/Annotation/LocalService.php +++ b/Annotation/LocalService.php @@ -8,6 +8,7 @@ use ReflectionException; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; +use Snowflake\Event; /** * Class LocalService @@ -21,8 +22,9 @@ use Snowflake\Snowflake; * LocalService constructor. * @param string $service * @param array $args + * @param bool $async_reload */ - public function __construct(public string $service, public array $args = []) + public function __construct(public string $service, public array $args = [], public bool $async_reload = true) { } @@ -33,6 +35,7 @@ use Snowflake\Snowflake; * @throws ReflectionException * @throws ComponentException * @throws NotFindClassException + * @throws \Exception */ public function execute(array $handler): mixed { @@ -42,6 +45,12 @@ use Snowflake\Snowflake; } Snowflake::set($this->service, $class); + if ($this->async_reload === true) { + $event = Snowflake::app()->getEvent(); + $event->on(Event::SERVER_BEFORE_RELOAD, function () { + Snowflake::app()->remove($this->service); + }); + } return parent::execute($handler); // TODO: Change the autogenerated stub } diff --git a/System/Di/Service.php b/System/Di/Service.php index 7a75d8e7..fccfe15b 100644 --- a/System/Di/Service.php +++ b/System/Di/Service.php @@ -139,13 +139,19 @@ class Service extends Component */ public function remove($id): bool { - unset($this->_components[$id]); + $component = $this->_components[$id]; + $className = get_class($component); + + unset($component, $this->_components[$id]); unset($this->_definition[$id]); if (isset($this->_alias[$id])) { unset($this->_components[$this->_alias[$id]]); unset($this->_definition[$this->_alias[$id]]); unset($this->_alias[$id]); } + + Snowflake::getDi()->unset($className); + return $this->has($id); } }