-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadsLocalEnv.php
More file actions
62 lines (45 loc) · 1.37 KB
/
LoadsLocalEnv.php
File metadata and controls
62 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreCodeCoop\NfsePHP\Tests\Support;
trait LoadsLocalEnv
{
private static bool $envLoaded = false;
protected static function loadLocalEnv(): void
{
if (self::$envLoaded) {
return;
}
self::$envLoaded = true;
$root = dirname(__DIR__, 2);
self::loadFile($root . '/.env.local');
self::loadFile($root . '/.env');
}
private static function loadFile(string $path): void
{
if (!is_file($path)) {
return;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
return;
}
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) {
continue;
}
[$key, $value] = explode('=', $line, 2);
$key = trim($key);
if ($key === '' || getenv($key) !== false) {
continue;
}
$value = trim($value);
$value = trim($value, "\"'");
putenv($key . '=' . $value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
}
}