diff options
author | Александр Кирюхин <alexander@kiryukhin.su> | 2018-01-19 03:13:16 +0300 |
---|---|---|
committer | Александр Кирюхин <alexander@kiryukhin.su> | 2018-01-19 03:13:16 +0300 |
commit | 6f452ea995c25a30f214a530fddc6e9d236bdfce (patch) | |
tree | b6538c4e7bd600f3dd4bcaf0d1340155d95da373 /src/Loader/FileLoader.php | |
parent | 6b07f5f4ebb3043efc4991debebb14529cb77aa5 (diff) |
Completed reference implementation, added tests
Diffstat (limited to 'src/Loader/FileLoader.php')
-rw-r--r-- | src/Loader/FileLoader.php | 41 |
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 |