Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| EnvParser | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| parse | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Config Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Config\Parsers; |
| 11 | |
| 12 | /** |
| 13 | * Class EnvParser. |
| 14 | * |
| 15 | * @package config |
| 16 | */ |
| 17 | class EnvParser extends Parser |
| 18 | { |
| 19 | /** |
| 20 | * Parses an .ENV file. |
| 21 | * |
| 22 | * @param mixed $config Path to the .ENV file. |
| 23 | * |
| 24 | * @throws ParserException |
| 25 | * |
| 26 | * @return array<mixed> The .ENV parsed data |
| 27 | */ |
| 28 | public static function parse(mixed $config) : array |
| 29 | { |
| 30 | static::checkConfig($config); |
| 31 | return static::parseOrThrow(static function () use ($config) : array { |
| 32 | $contents = \file_get_contents($config); |
| 33 | $contents = \explode(\PHP_EOL, $contents); // @phpstan-ignore-line |
| 34 | $data = []; |
| 35 | foreach ($contents as $line) { |
| 36 | $line = \trim($line); |
| 37 | if ($line === '' || \str_starts_with($line, '#')) { |
| 38 | continue; |
| 39 | } |
| 40 | [$key, $value] = \explode('=', $line, 2); |
| 41 | $key = \trim($key); |
| 42 | $key = \explode('.', $key); |
| 43 | $value = static::getValue($value); |
| 44 | $parent = []; |
| 45 | static::addChild($parent, $key, $value); |
| 46 | $data = \array_replace_recursive($data, $parent); |
| 47 | } |
| 48 | return static::ksortRecursive($data); |
| 49 | }); |
| 50 | } |
| 51 | } |