Skip to content

Commit 844b47a

Browse files
authored
add array support
1 parent b0c7510 commit 844b47a

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

App/Models/Env.php

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,86 @@
22

33
namespace Monster\App\Models;
44

5+
/**
6+
* The Env class is responsible for reading and parsing environment variables from a file.
7+
*/
58
class Env
69
{
10+
/**
11+
* @var array Holds the environment data after parsing.
12+
*/
713
private $envData;
814

15+
/**
16+
* Constructor for the Env class.
17+
*
18+
* @param string $envFilePath The file path to the .env file.
19+
*/
920
public function __construct($envFilePath)
1021
{
22+
// Read and store the environment variables from the file.
1123
$this->envData = $this->readEnvFile($envFilePath);
1224
}
1325

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+
*/
1532
private function readEnvFile($envFilePath)
1633
{
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");
1938
if ($file) {
39+
// Read each line of the file.
2040
while (($line = fgets($file)) !== false) {
21-
// Ignore lines starting with # or empty lines
41+
// Skip lines that are comments or empty.
2242
if (preg_match("/^\s*(#|$)/", $line)) {
2343
continue;
2444
}
25-
// Parse lines in the format KEY=VALUE
45+
// Match lines that look like key=value pairs.
2646
if (preg_match("/^([^=]+)=(.*)$/", $line, $matches)) {
47+
// Trim whitespace around the key.
2748
$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+
}
3063
}
3164
}
32-
fclose($file); // Close the environment file
65+
// Close the file after reading.
66+
fclose($file);
3367
}
34-
return $envData; // Return the parsed environment data
68+
// Return the parsed environment data.
69+
return $envData;
3570
}
3671

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+
*/
3878
public function get($key)
3979
{
80+
// Check if the key exists in the environment data and return its value.
4081
if (isset($this->envData[$key])) {
41-
return trim($this->envData[$key], "\""); // Remove surrounding double quotes if present
82+
return $this->envData[$key];
4283
}
43-
return null; // Return null if the key is not found
84+
// Return null if the key does not exist.
85+
return null;
4486
}
45-
}
87+
}

0 commit comments

Comments
 (0)