From 6f5073f8a7e7cb4095dcbc47149731f7b294a7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 31 Mar 2021 17:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/LocalService.php | 11 ++++++++++- System/Di/Service.php | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) 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); } }