-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to reload
.env
when using hyperf/watcher
. (#6936)
Co-authored-by: 李铭昕 <715557344@qq.com>
- Loading branch information
1 parent
804dc0a
commit 3ad2e1d
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Watcher\Event; | ||
|
||
class BeforeServerRestart | ||
{ | ||
public function __construct(public string $pid) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
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 | ||
{ | ||
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 | ||
{ | ||
DotenvManager::reload([BASE_PATH]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters