aboutsummaryrefslogtreecommitdiff
path: root/src/Loader/FileLoader.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Loader/FileLoader.php')
-rw-r--r--src/Loader/FileLoader.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Loader/FileLoader.php b/src/Loader/FileLoader.php
new file mode 100644
index 0000000..0702a53
--- /dev/null
+++ b/src/Loader/FileLoader.php
@@ -0,0 +1,41 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * @author: Alexander Kiryukhin <alexander@kiryukhin.su>
+ * @license: MIT
+ */
+
+namespace NeonXP\Dotenv\Loader;
+
+use NeonXP\Dotenv\Exception\RuntimeException;
+
+/**
+ * Class FileLoader
+ * @package NeonXP\Dotenv\Loader
+ */
+class FileLoader implements LoaderInterface
+{
+ const COMMENT_LINE_REGEX = '/^\s*#/';
+
+ /**
+ * @inheritdoc
+ * @param string $filePath
+ * @return array
+ * @throws RuntimeException
+ */
+ public function load(string $filePath = '.env'): array
+ {
+ if (!file_exists($filePath)) {
+ throw new RuntimeException("There is no {$filePath} file!");
+ }
+ $lines = file($filePath);
+ $lines = array_map('trim', $lines);
+ $lines = array_filter($lines, function (string $line) {
+ return trim($line) && !preg_match(self::COMMENT_LINE_REGEX, $line);
+ });
+ $lines = array_values($lines);
+
+ return $lines;
+ }
+} \ No newline at end of file