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\Parser;
6
 
7
use Dotenv\Util\Regex;
8
use Dotenv\Util\Str;
9
 
10
final class Lines
11
{
12
    /**
13
     * This class is a singleton.
14
     *
15
     * @codeCoverageIgnore
16
     *
17
     * @return void
18
     */
19
    private function __construct()
20
    {
21
        //
22
    }
23
 
24
    /**
25
     * Process the array of lines of environment variables.
26
     *
27
     * This will produce an array of raw entries, one per variable.
28
     *
29
     * @param string[] $lines
30
     *
31
     * @return string[]
32
     */
33
    public static function process(array $lines)
34
    {
35
        $output = [];
36
        $multiline = false;
37
        $multilineBuffer = [];
38
 
39
        foreach ($lines as $line) {
40
            [$multiline, $line, $multilineBuffer] = self::multilineProcess($multiline, $line, $multilineBuffer);
41
 
42
            if (!$multiline && !self::isCommentOrWhitespace($line)) {
43
                $output[] = $line;
44
            }
45
        }
46
 
47
        return $output;
48
    }
49
 
50
    /**
51
     * Used to make all multiline variable process.
52
     *
53
     * @param bool     $multiline
54
     * @param string   $line
55
     * @param string[] $buffer
56
     *
57
     * @return array{bool,string,string[]}
58
     */
59
    private static function multilineProcess(bool $multiline, string $line, array $buffer)
60
    {
61
        $startsOnCurrentLine = $multiline ? false : self::looksLikeMultilineStart($line);
62
 
63
        // check if $line can be multiline variable
64
        if ($startsOnCurrentLine) {
65
            $multiline = true;
66
        }
67
 
68
        if ($multiline) {
69
            \array_push($buffer, $line);
70
 
71
            if (self::looksLikeMultilineStop($line, $startsOnCurrentLine)) {
72
                $multiline = false;
73
                $line = \implode("\n", $buffer);
74
                $buffer = [];
75
            }
76
        }
77
 
78
        return [$multiline, $line, $buffer];
79
    }
80
 
81
    /**
82
     * Determine if the given line can be the start of a multiline variable.
83
     *
84
     * @param string $line
85
     *
86
     * @return bool
87
     */
88
    private static function looksLikeMultilineStart(string $line)
89
    {
90
        return Str::pos($line, '="')->map(static function () use ($line) {
91
            return self::looksLikeMultilineStop($line, true) === false;
92
        })->getOrElse(false);
93
    }
94
 
95
    /**
96
     * Determine if the given line can be the start of a multiline variable.
97
     *
98
     * @param string $line
99
     * @param bool   $started
100
     *
101
     * @return bool
102
     */
103
    private static function looksLikeMultilineStop(string $line, bool $started)
104
    {
105
        if ($line === '"') {
106
            return true;
107
        }
108
 
109
        return Regex::occurrences('/(?=([^\\\\]"))/', \str_replace('\\\\', '', $line))->map(static function (int $count) use ($started) {
110
            return $started ? $count > 1 : $count >= 1;
111
        })->success()->getOrElse(false);
112
    }
113
 
114
    /**
115
     * Determine if the line in the file is a comment or whitespace.
116
     *
117
     * @param string $line
118
     *
119
     * @return bool
120
     */
121
    private static function isCommentOrWhitespace(string $line)
122
    {
123
        $line = \trim($line);
124
 
125
        return $line === '' || (isset($line[0]) && $line[0] === '#');
126
    }
127
}