From 6f452ea995c25a30f214a530fddc6e9d236bdfce Mon Sep 17 00:00:00 2001 From: Александр Кирюхин Date: Fri, 19 Jan 2018 03:13:16 +0300 Subject: Completed reference implementation, added tests --- src/Loader/FileLoader.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Loader/FileLoader.php (limited to 'src/Loader/FileLoader.php') 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 @@ + + * @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 -- cgit v1.2.3