Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv\Store\File;
6
 
7
use Dotenv\Exception\InvalidEncodingException;
8
use Dotenv\Util\Str;
9
use PhpOption\Option;
10
 
11
/**
12
 * @internal
13
 */
14
final class Reader
15
{
16
    /**
17
     * This class is a singleton.
18
     *
19
     * @codeCoverageIgnore
20
     *
21
     * @return void
22
     */
23
    private function __construct()
24
    {
25
        //
26
    }
27
 
28
    /**
29
     * Read the file(s), and return their raw content.
30
     *
31
     * We provide the file path as the key, and its content as the value. If
32
     * short circuit mode is enabled, then the returned array with have length
33
     * at most one. File paths that couldn't be read are omitted entirely.
34
     *
35
     * @param string[]    $filePaths
36
     * @param bool        $shortCircuit
37
     * @param string|null $fileEncoding
38
     *
39
     * @throws \Dotenv\Exception\InvalidEncodingException
40
     *
41
     * @return array<string,string>
42
     */
43
    public static function read(array $filePaths, bool $shortCircuit = true, string $fileEncoding = null)
44
    {
45
        $output = [];
46
 
47
        foreach ($filePaths as $filePath) {
48
            $content = self::readFromFile($filePath, $fileEncoding);
49
            if ($content->isDefined()) {
50
                $output[$filePath] = $content->get();
51
                if ($shortCircuit) {
52
                    break;
53
                }
54
            }
55
        }
56
 
57
        return $output;
58
    }
59
 
60
    /**
61
     * Read the given file.
62
     *
63
     * @param string      $path
64
     * @param string|null $encoding
65
     *
66
     * @throws \Dotenv\Exception\InvalidEncodingException
67
     *
68
     * @return \PhpOption\Option<string>
69
     */
70
    private static function readFromFile(string $path, string $encoding = null)
71
    {
72
        /** @var Option<string> */
73
        $content = Option::fromValue(@\file_get_contents($path), false);
74
 
75
        return $content->flatMap(static function (string $content) use ($encoding) {
76
            return Str::utf8($content, $encoding)->mapError(static function (string $error) {
77
                throw new InvalidEncodingException($error);
78
            })->success();
79
        });
80
    }
81
}