1 | <?php |
||
10 | class Parser |
||
11 | { |
||
12 | /** |
||
13 | * @var string[] |
||
14 | */ |
||
15 | private $tokens; |
||
16 | |||
17 | /** |
||
18 | * @var string[] |
||
19 | */ |
||
20 | private $span; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $lineNumber = 0; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $strict; |
||
31 | |||
32 | 1 | public function __construct(string $changelog, bool $strict = true) |
|
33 | { |
||
34 | 1 | $this->tokens = array_map('trim', explode("\n", $changelog)); |
|
35 | 1 | $this->strict = $strict; |
|
36 | 1 | } |
|
37 | |||
38 | 1 | private function current(): string |
|
39 | { |
||
40 | 1 | if (count($this->tokens) === 0) { |
|
41 | 1 | return ''; |
|
42 | } |
||
43 | 1 | return $this->tokens[0]; |
|
44 | } |
||
45 | |||
46 | 1 | private function next(): string |
|
47 | { |
||
48 | 1 | if (count($this->tokens) === 0) { |
|
49 | throw $this->error('Unexpected end of file'); |
||
50 | } |
||
51 | |||
52 | 1 | $n = ++$this->lineNumber; |
|
53 | 1 | $line = array_shift($this->tokens); |
|
54 | |||
55 | 1 | $this->span[] = " {$n}: $line"; |
|
56 | |||
57 | 1 | if (count($this->span) > 4) { |
|
58 | 1 | array_shift($this->span); |
|
59 | } |
||
60 | |||
61 | 1 | return $line; |
|
62 | } |
||
63 | |||
64 | 1 | private function acceptEmptyLine() |
|
65 | { |
||
66 | 1 | if ($this->strict) { |
|
67 | if ('' !== $this->next()) { |
||
68 | throw $this->error('Expected an empty line'); |
||
69 | } |
||
70 | } else { |
||
71 | 1 | while (preg_match('/^\s*$/', $this->current()) && count($this->tokens) > 0) { |
|
72 | 1 | $this->next(); |
|
73 | } |
||
74 | } |
||
75 | 1 | } |
|
76 | |||
77 | 1 | private function acceptEof() |
|
78 | { |
||
79 | 1 | if (count($this->tokens) !== 0) { |
|
80 | $this->next(); |
||
81 | throw $this->error('Expected EOF'); |
||
82 | } |
||
83 | 1 | } |
|
84 | |||
85 | 1 | private function matchVersion($line, &$m = null) |
|
86 | { |
||
87 | 1 | return preg_match('/^\#\# \s ( v\d+\.\d+\.\d+(-[\w\.]+)? | master )$/x', $line, $m); |
|
88 | } |
||
89 | |||
90 | private function error($message): ParseException |
||
101 | |||
102 | 1 | public function parse(): Changelog |
|
103 | { |
||
104 | 1 | $changelog = $this->parseTitle(); |
|
105 | |||
106 | 1 | $this->acceptEmptyLine(); |
|
122 | |||
123 | 1 | private function parseTitle(): Changelog |
|
133 | |||
134 | 1 | private function parseVersion(): Version |
|
135 | { |
||
136 | 1 | if ($this->matchVersion($this->next(), $m)) { |
|
137 | 1 | $version = new Version(); |
|
138 | 1 | $version->setVersion($curr = $m[1]); |
|
139 | |||
140 | 1 | $compareLink = $this->next(); |
|
141 | 1 | if (!preg_match('/^\[/', $compareLink)) { |
|
142 | throw $this->error('Expected link to compare page with previous version'); |
||
143 | } |
||
144 | |||
145 | 1 | $prev = 'v\d+\.\d+\.\d+(-[\d\w\.]+)?'; |
|
146 | $regexp = "/ |
||
147 | 1 | ^ \[($prev)\.\.\.$curr\] |
|
148 | 1 | \(https\:\/\/github\.com\/deployphp\/deployer\/compare\/$prev\.\.\.$curr\) $ |
|
149 | /x"; |
||
150 | |||
151 | 1 | if (preg_match($regexp, $compareLink, $m)) { |
|
152 | 1 | $version->setPrevious($m[1]); |
|
153 | } else { |
||
154 | throw $this->error('Error in compare link syntax'); |
||
155 | } |
||
156 | |||
157 | 1 | $this->acceptEmptyLine(); |
|
158 | |||
159 | 1 | $sections = ['Added', 'Changed', 'Fixed', 'Removed']; |
|
160 | 1 | $sectionsCount = count($sections); |
|
161 | |||
162 | 1 | for ($i = 0; $i < $sectionsCount; $i++) { |
|
163 | 1 | foreach ($sections as $key => $section) { |
|
164 | 1 | if (preg_match('/^\#\#\# \s ' . $section . ' $/x', $this->current())) { |
|
165 | 1 | $this->next(); |
|
166 | |||
167 | 1 | $version->{"set$section"}($this->parseItems()); |
|
168 | 1 | unset($sections[$key]); |
|
169 | |||
170 | 1 | $this->acceptEmptyLine(); |
|
171 | |||
172 | 1 | break; |
|
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 1 | $this->acceptEmptyLine(); |
|
178 | |||
179 | 1 | return $version; |
|
180 | } |
||
181 | |||
182 | throw $this->error('Expected version'); |
||
183 | } |
||
184 | |||
185 | 1 | private function parseItems(): array |
|
206 | |||
207 | 1 | private function parseReferences(): array |
|
219 | } |
||
220 |