Skip to content

Commit 4eac760

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

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/DotenvManager.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Support;
14+
15+
use Dotenv\Dotenv;
16+
use Dotenv\Repository\Adapter\AdapterInterface;
17+
use Dotenv\Repository\Adapter\PutenvAdapter;
18+
use Dotenv\Repository\RepositoryBuilder;
19+
20+
class DotenvManager
21+
{
22+
protected static AdapterInterface $adapter;
23+
24+
protected static Dotenv $dotenv;
25+
26+
protected static array $cachedValues;
27+
28+
public static function load(array $paths): void
29+
{
30+
if (isset(static::$cachedValues)) {
31+
return;
32+
}
33+
34+
static::$cachedValues = static::getDotenv($paths)->load();
35+
}
36+
37+
public static function reload(array $paths, bool $force = false): void
38+
{
39+
if (! isset(static::$cachedValues)) {
40+
static::load($paths);
41+
42+
return;
43+
}
44+
45+
foreach (static::$cachedValues as $deletedEntry => $value) {
46+
static::getAdapter()->delete($deletedEntry);
47+
}
48+
49+
static::$cachedValues = static::getDotenv($paths, $force)->load();
50+
}
51+
52+
protected static function getDotenv(array $paths, bool $force = false): Dotenv
53+
{
54+
if (isset(static::$dotenv) && ! $force) {
55+
return static::$dotenv;
56+
}
57+
58+
return static::$dotenv = Dotenv::create(
59+
RepositoryBuilder::createWithNoAdapters()
60+
->addAdapter(static::getAdapter($force))
61+
->immutable()
62+
->make(),
63+
$paths
64+
);
65+
}
66+
67+
protected static function getAdapter(bool $force = false): AdapterInterface
68+
{
69+
if (isset(static::$adapter) && ! $force) {
70+
return static::$adapter;
71+
}
72+
73+
return static::$adapter = PutenvAdapter::create()->get();
74+
}
75+
}

tests/DotenvManagerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 HyperfTest\Support;
14+
15+
use Hyperf\Support\DotenvManager;
16+
use PHPUnit\Framework\Attributes\CoversNothing;
17+
use PHPUnit\Framework\TestCase;
18+
19+
use function Hyperf\Support\env;
20+
21+
/**
22+
* @internal
23+
* @coversNothing
24+
*/
25+
#[CoversNothing]
26+
class DotenvManagerTest extends TestCase
27+
{
28+
public function testLoad()
29+
{
30+
DotenvManager::load([__DIR__ . '/envs/oldEnv']);
31+
32+
$this->assertEquals('1.0', env('TEST_VERSION'));
33+
$this->assertTrue(env('OLD_FLAG'));
34+
}
35+
36+
public function testReload()
37+
{
38+
DotenvManager::load([__DIR__ . '/envs/oldEnv']);
39+
$this->assertEquals('1.0', env('TEST_VERSION'));
40+
$this->assertTrue(env('OLD_FLAG'));
41+
42+
DotenvManager::reload([__DIR__ . '/envs/newEnv'], true);
43+
$this->assertEquals('2.0', env('TEST_VERSION'));
44+
$this->assertNull(env('OLD_FLAG'));
45+
$this->assertTrue(env('NEW_FLAG'));
46+
}
47+
48+
public function testGetEnvFromEnvironment()
49+
{
50+
DotenvManager::load([__DIR__ . '/envs/oldEnv']);
51+
52+
$this->assertNotEquals('0.0.0', env('SW_VERSION'));
53+
}
54+
}

tests/envs/newEnv/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TEST_VERSION=2.0
2+
NEW_FLAG=true
3+
SW_VERSION="0.0.0"

tests/envs/oldEnv/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TEST_VERSION=1.0
2+
OLD_FLAG=true
3+
SW_VERSION="0.0.0"

0 commit comments

Comments
 (0)