diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 4206be1..fb24cf6 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -13,7 +13,7 @@ namespace Hyperf\Watcher; use Hyperf\Watcher\Command\WatchCommand; -use Hyperf\Watcher\Listener\ReloadDotenvAndConfigListener; +use Hyperf\Watcher\Listener\ReloadDotenvListener; class ConfigProvider { @@ -26,7 +26,7 @@ public function __invoke(): array WatchCommand::class, ], 'listeners' => [ - ReloadDotenvAndConfigListener::class, + ReloadDotenvListener::class, ], 'publish' => [ [ diff --git a/src/Listener/ReloadDotenvAndConfigListener.php b/src/Listener/ReloadDotenvListener.php similarity index 51% rename from src/Listener/ReloadDotenvAndConfigListener.php rename to src/Listener/ReloadDotenvListener.php index f27e50e..b9acd5c 100644 --- a/src/Listener/ReloadDotenvAndConfigListener.php +++ b/src/Listener/ReloadDotenvListener.php @@ -12,54 +12,27 @@ namespace Hyperf\Watcher\Listener; -use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; -use Hyperf\Framework\Event\BeforeWorkerStart; use Hyperf\Support\DotenvManager; use Hyperf\Watcher\Event\BeforeServerRestart; use Psr\Container\ContainerInterface; -use Swoole\Atomic; -class ReloadDotenvAndConfigListener implements ListenerInterface +final class ReloadDotenvListener implements ListenerInterface { - protected static Atomic $restartCounter; - public function __construct(protected ContainerInterface $container) { - static::$restartCounter = new Atomic(0); } public function listen(): array { return [ - BeforeWorkerStart::class, BeforeServerRestart::class, ]; } public function process(object $event): void { - if ($event instanceof BeforeWorkerStart - && $event->workerId === 0 - && static::$restartCounter->get() === 0 - ) { - static::$restartCounter->add(); - return; - } - - static::$restartCounter->add(); - $this->reloadDotenv(); - $this->reloadConfig(); - } - - protected function reloadConfig(): void - { - if (! method_exists($this->container, 'unbind')) { - return; - } - - $this->container->unbind(ConfigInterface::class); } protected function reloadDotenv(): void