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