2
2
3
3
namespace Monster \App \Models ;
4
4
5
+ /**
6
+ * The Env class is responsible for reading and parsing environment variables from a file.
7
+ */
5
8
class Env
6
9
{
10
+ /**
11
+ * @var array Holds the environment data after parsing.
12
+ */
7
13
private $ envData ;
8
14
15
+ /**
16
+ * Constructor for the Env class.
17
+ *
18
+ * @param string $envFilePath The file path to the .env file.
19
+ */
9
20
public function __construct ($ envFilePath )
10
21
{
22
+ // Read and store the environment variables from the file.
11
23
$ this ->envData = $ this ->readEnvFile ($ envFilePath );
12
24
}
13
25
14
- // Read the environment file and parse its contents into an associative array
26
+ /**
27
+ * Reads the environment file and parses the variables into an array.
28
+ *
29
+ * @param string $envFilePath The file path to the .env file.
30
+ * @return array The parsed environment variables.
31
+ */
15
32
private function readEnvFile ($ envFilePath )
16
33
{
17
- $ envData = array (); // Initialize an empty array to store the environment data
18
- $ file = fopen ($ envFilePath , "r " ); // Open the environment file for reading
34
+ // Initialize an empty array to hold the environment data.
35
+ $ envData = array ();
36
+ // Open the file for reading.
37
+ $ file = fopen ($ envFilePath , "r " );
19
38
if ($ file ) {
39
+ // Read each line of the file.
20
40
while (($ line = fgets ($ file )) !== false ) {
21
- // Ignore lines starting with # or empty lines
41
+ // Skip lines that are comments or empty.
22
42
if (preg_match ("/^\s*(#|$)/ " , $ line )) {
23
43
continue ;
24
44
}
25
- // Parse lines in the format KEY=VALUE
45
+ // Match lines that look like key=value pairs.
26
46
if (preg_match ("/^([^=]+)=(.*)$/ " , $ line , $ matches )) {
47
+ // Trim whitespace around the key.
27
48
$ key = trim ($ matches [1 ]);
28
- $ value = trim ($ matches [2 ], "\" \n\r\t" );
29
- $ envData [$ key ] = $ value ; // Store the key-value pair in the $envData array
49
+ // Trim whitespace and quotes around the value.
50
+ $ value = trim ($ matches [2 ], " \n\r\t" );
51
+ // Check if the value is an array (starts with [ and ends with ]).
52
+ if (preg_match ("/^\[(.*)\]$/ " , $ value , $ arrayMatches )) {
53
+ // Split the value by comma and trim each element.
54
+ $ arrayValues = array_map (function ($ item ) {
55
+ return trim ($ item , "\" \t\n\r" );
56
+ }, explode (', ' , trim ($ arrayMatches [1 ])));
57
+ // Assign the array to the key in the environment data.
58
+ $ envData [$ key ] = $ arrayValues ;
59
+ } else {
60
+ // Assign the value to the key in the environment data.
61
+ $ envData [$ key ] = trim ($ value , "\"" );
62
+ }
30
63
}
31
64
}
32
- fclose ($ file ); // Close the environment file
65
+ // Close the file after reading.
66
+ fclose ($ file );
33
67
}
34
- return $ envData ; // Return the parsed environment data
68
+ // Return the parsed environment data.
69
+ return $ envData ;
35
70
}
36
71
37
- // Retrieve the value of a specific key from the environment data
72
+ /**
73
+ * Retrieves the value of an environment variable by key.
74
+ *
75
+ * @param string $key The key of the environment variable to retrieve.
76
+ * @return mixed|null The value of the environment variable, or null if not found.
77
+ */
38
78
public function get ($ key )
39
79
{
80
+ // Check if the key exists in the environment data and return its value.
40
81
if (isset ($ this ->envData [$ key ])) {
41
- return trim ( $ this ->envData [$ key ], "\"" ); // Remove surrounding double quotes if present
82
+ return $ this ->envData [$ key ];
42
83
}
43
- return null ; // Return null if the key is not found
84
+ // Return null if the key does not exist.
85
+ return null ;
44
86
}
45
- }
87
+ }
0 commit comments