Skip to content

Commit 3ad2e1d

Browse files
Support to reload .env when using hyperf/watcher. (#6936)
Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent 804dc0a commit 3ad2e1d

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

src/ConfigProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Hyperf\Watcher;
1414

1515
use Hyperf\Watcher\Command\WatchCommand;
16+
use Hyperf\Watcher\Listener\ReloadDotenvAndConfigListener;
1617

1718
class ConfigProvider
1819
{
@@ -24,6 +25,9 @@ public function __invoke(): array
2425
'commands' => [
2526
WatchCommand::class,
2627
],
28+
'listeners' => [
29+
ReloadDotenvAndConfigListener::class,
30+
],
2731
'publish' => [
2832
[
2933
'id' => 'config',

src/Event/BeforeServerRestart.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Watcher\Event;
14+
15+
class BeforeServerRestart
16+
{
17+
public function __construct(public string $pid)
18+
{
19+
}
20+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Watcher\Listener;
14+
15+
use Hyperf\Contract\ConfigInterface;
16+
use Hyperf\Event\Contract\ListenerInterface;
17+
use Hyperf\Framework\Event\BeforeWorkerStart;
18+
use Hyperf\Support\DotenvManager;
19+
use Hyperf\Watcher\Event\BeforeServerRestart;
20+
use Psr\Container\ContainerInterface;
21+
use Swoole\Atomic;
22+
23+
class ReloadDotenvAndConfigListener implements ListenerInterface
24+
{
25+
protected static Atomic $restartCounter;
26+
27+
public function __construct(protected ContainerInterface $container)
28+
{
29+
static::$restartCounter = new Atomic(0);
30+
}
31+
32+
public function listen(): array
33+
{
34+
return [
35+
BeforeWorkerStart::class,
36+
BeforeServerRestart::class,
37+
];
38+
}
39+
40+
public function process(object $event): void
41+
{
42+
if ($event instanceof BeforeWorkerStart
43+
&& $event->workerId === 0
44+
&& static::$restartCounter->get() === 0
45+
) {
46+
static::$restartCounter->add();
47+
return;
48+
}
49+
50+
static::$restartCounter->add();
51+
52+
$this->reloadDotenv();
53+
$this->reloadConfig();
54+
}
55+
56+
protected function reloadConfig(): void
57+
{
58+
if (! method_exists($this->container, 'unbind')) {
59+
return;
60+
}
61+
62+
$this->container->unbind(ConfigInterface::class);
63+
}
64+
65+
protected function reloadDotenv(): void
66+
{
67+
DotenvManager::reload([BASE_PATH]);
68+
}
69+
}

src/Watcher.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
use Hyperf\Support\Filesystem\FileNotFoundException;
2121
use Hyperf\Support\Filesystem\Filesystem;
2222
use Hyperf\Watcher\Driver\DriverInterface;
23+
use Hyperf\Watcher\Event\BeforeServerRestart;
2324
use PhpParser\PrettyPrinter\Standard;
2425
use Psr\Container\ContainerInterface;
26+
use Psr\EventDispatcher\EventDispatcherInterface;
2527
use Symfony\Component\Console\Output\OutputInterface;
2628
use Throwable;
2729

@@ -109,6 +111,8 @@ public function restart($isStart = true)
109111
$pid = $this->filesystem->get($file);
110112
try {
111113
$this->output->writeln('Stop server...');
114+
$this->container->get(EventDispatcherInterface::class)
115+
->dispatch(new BeforeServerRestart($pid));
112116
if (posix_kill((int) $pid, 0)) {
113117
posix_kill((int) $pid, SIGTERM);
114118
}

0 commit comments

Comments
 (0)