This commit is contained in:
2021-03-31 17:57:42 +08:00
parent 05edfdd247
commit 6f5073f8a7
2 changed files with 17 additions and 2 deletions
+10 -1
View File
@@ -8,6 +8,7 @@ use ReflectionException;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Snowflake\Event;
/** /**
* Class LocalService * Class LocalService
@@ -21,8 +22,9 @@ use Snowflake\Snowflake;
* LocalService constructor. * LocalService constructor.
* @param string $service * @param string $service
* @param array $args * @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 ReflectionException
* @throws ComponentException * @throws ComponentException
* @throws NotFindClassException * @throws NotFindClassException
* @throws \Exception
*/ */
public function execute(array $handler): mixed public function execute(array $handler): mixed
{ {
@@ -42,6 +45,12 @@ use Snowflake\Snowflake;
} }
Snowflake::set($this->service, $class); 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 return parent::execute($handler); // TODO: Change the autogenerated stub
} }
+7 -1
View File
@@ -139,13 +139,19 @@ class Service extends Component
*/ */
public function remove($id): bool 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]); unset($this->_definition[$id]);
if (isset($this->_alias[$id])) { if (isset($this->_alias[$id])) {
unset($this->_components[$this->_alias[$id]]); unset($this->_components[$this->_alias[$id]]);
unset($this->_definition[$this->_alias[$id]]); unset($this->_definition[$this->_alias[$id]]);
unset($this->_alias[$id]); unset($this->_alias[$id]);
} }
Snowflake::getDi()->unset($className);
return $this->has($id); return $this->has($id);
} }
} }