File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Dotenv ;
4
4
5
+ use Dotenv \Exception \InvalidPathException ;
6
+
5
7
/**
6
8
* This is the dotenv class.
7
9
*
@@ -48,6 +50,21 @@ public function load()
48
50
return $ this ->loadData ();
49
51
}
50
52
53
+ /**
54
+ * Load environment file in given directory, suppress InvalidPathException.
55
+ *
56
+ * @return array
57
+ */
58
+ public function safeLoad ()
59
+ {
60
+ try {
61
+ return $ this ->loadData ();
62
+ } catch (InvalidPathException $ e ) {
63
+ // suppressing exception
64
+ return array ();
65
+ }
66
+ }
67
+
51
68
/**
52
69
* Load environment file in given directory.
53
70
*
Original file line number Diff line number Diff line change @@ -24,6 +24,12 @@ public function testDotenvThrowsExceptionIfUnableToLoadFile()
24
24
$ dotenv ->load ();
25
25
}
26
26
27
+ public function testDotenvSkipsLoadingIfFileIsMissing ()
28
+ {
29
+ $ dotenv = new Dotenv (__DIR__ );
30
+ $ this ->assertEmpty ($ dotenv ->safeLoad ());
31
+ }
32
+
27
33
public function testDotenvLoadsEnvironmentVars ()
28
34
{
29
35
$ dotenv = new Dotenv ($ this ->fixturesFolder );
You can’t perform that action at this time.
0 commit comments