|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Consolidation\Comments; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Remember comments in one text file (usually a yaml file), and |
|
7
|
|
|
* re-inject them into an edited copy of the same file. |
|
8
|
|
|
* |
|
9
|
|
|
* This is a workaround for the fact that the Symfony Yaml parser |
|
10
|
|
|
* does not record comments. |
|
11
|
|
|
* |
|
12
|
|
|
* Comments at the beginning and end of the file are guarenteed to |
|
13
|
|
|
* be retained at the beginning and the end of the resulting file. |
|
14
|
|
|
* |
|
15
|
|
|
* Comments that appear before sections of yaml that is deleted will |
|
16
|
|
|
* be deliberately discarded as well. |
|
17
|
|
|
* |
|
18
|
|
|
* If the resulting yaml file contents are reordered, comments may |
|
19
|
|
|
* become mis-ordered (attached to the wrong element). |
|
20
|
|
|
* |
|
21
|
|
|
* Comments that appear before sections of yaml that are edited may |
|
22
|
|
|
* be inadvertantly lost. It is recommended to always place comments |
|
23
|
|
|
* immediately before identifier lines (i.e. "foo:"). |
|
24
|
|
|
*/ |
|
25
|
|
|
class Comments |
|
26
|
|
|
{ |
|
27
|
|
|
protected $hasStored; |
|
28
|
|
|
protected $headComments; |
|
29
|
|
|
protected $accumulated; |
|
30
|
|
|
protected $lineIds; |
|
31
|
|
|
protected $stored; |
|
32
|
|
|
protected $endComments; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->hasStored = false; |
|
37
|
|
|
$this->headComments = false; |
|
38
|
|
|
$this->accumulated = []; |
|
39
|
|
|
$this->lineIds = []; |
|
40
|
|
|
$this->stored = []; |
|
41
|
|
|
$this->endComments = []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Collect all of the comments from a text file |
|
46
|
|
|
* (usually a yaml file). |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $contentLines |
|
49
|
|
|
*/ |
|
50
|
|
|
public function collect(array $contentLines) |
|
51
|
|
|
{ |
|
52
|
|
|
$contentLines = $this->removeTrailingBlankLines($contentLines); |
|
53
|
|
|
|
|
54
|
|
|
// Put look through the rest of the lines and store the comments as needed |
|
55
|
|
|
foreach ($contentLines as $line) { |
|
56
|
|
|
if ($this->isBlank($line)) { |
|
57
|
|
|
$this->accumulateEmptyLine(); |
|
58
|
|
|
} elseif ($this->isComment($line)) { |
|
59
|
|
|
$this->accumulate($line); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->storeAccumulated($line); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
$this->endCollect(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Description |
|
69
|
|
|
* @param array $contentLines |
|
70
|
|
|
* @return array of lines with comments re-intersperced |
|
71
|
|
|
*/ |
|
72
|
|
|
public function inject(array $contentLines) |
|
73
|
|
|
{ |
|
74
|
|
|
$contentLines = $this->removeTrailingBlankLines($contentLines); |
|
75
|
|
|
|
|
76
|
|
|
// If there were any comments at the beginning of the |
|
77
|
|
|
// file, then put them back at the beginning. |
|
78
|
|
|
$result = $this->headComments === false ? [] : $this->headComments; |
|
79
|
|
|
foreach ($contentLines as $line) { |
|
80
|
|
|
$fetched = $this->find($line); |
|
81
|
|
|
$result = array_merge($result, $fetched); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$result[] = $line; |
|
84
|
|
|
} |
|
85
|
|
|
// Any comments found at the end of the file will stay at |
|
86
|
|
|
// the end of the file. |
|
87
|
|
|
$result = array_merge($result, $this->endComments); |
|
88
|
|
|
return $result; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $line |
|
93
|
|
|
* @return true if provided line is a comment |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function isComment($line) |
|
96
|
|
|
{ |
|
97
|
|
|
return preg_match('%^ *#%', $line); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Stop collecting. Any accumulated comments will be |
|
102
|
|
|
* remembered so that they may be re-injected at the |
|
103
|
|
|
* end of the new file. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function endCollect() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->endComments = $this->accumulated; |
|
108
|
|
|
$this->accumulated = []; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function accumulateEmptyLine() |
|
112
|
|
|
{ |
|
113
|
|
|
if ($this->hasStored) { |
|
114
|
|
|
return $this->accumulate(''); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($this->headComments === false) { |
|
118
|
|
|
$this->headComments = []; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->headComments = array_merge($this->headComments, $this->accumulated); |
|
|
|
|
|
|
122
|
|
|
$this->accumulated = ['']; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Accumulate comments and blank lines in our cache. |
|
127
|
|
|
* @param string $line |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function accumulate($line) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->accumulated[] = $line; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* When a non-comment line is found, remember all of |
|
136
|
|
|
* the comment lines that came before it in our cache. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $line |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function storeAccumulated($line) |
|
141
|
|
|
{ |
|
142
|
|
|
|
|
143
|
|
|
// Remember that we called storeAccumulated at least once |
|
144
|
|
|
$this->hasStored = true; |
|
145
|
|
|
|
|
146
|
|
|
// The very first time storeAccumulated is called, the |
|
147
|
|
|
// accumulated comments will be placed in $this->headComments |
|
148
|
|
|
// instead of stored, so they may be restored to the |
|
149
|
|
|
// beginning of the new file. |
|
150
|
|
|
if ($this->headComments === false) { |
|
151
|
|
|
$this->headComments = $this->accumulated; |
|
152
|
|
|
$this->accumulated = []; |
|
153
|
|
|
return; |
|
154
|
|
|
} |
|
155
|
|
|
if (!empty($this->accumulated)) { |
|
156
|
|
|
$lineId = $this->getLineId($line, true); |
|
157
|
|
|
$this->stored[$lineId][] = $this->accumulated; |
|
158
|
|
|
$this->accumulated = []; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Generates unique line id based on the key it contains. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $line |
|
166
|
|
|
* @param bool $isCollecting |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function getLineId($line, $isCollecting = true) |
|
169
|
|
|
{ |
|
170
|
|
|
list($id) = explode(':', $line, 2); |
|
171
|
|
|
|
|
172
|
|
|
if ($isCollecting) { |
|
173
|
|
|
if (isset($this->lineIds[$id])) { |
|
174
|
|
|
$this->lineIds[$id][] = end($this->lineIds[$id]) + 1; |
|
175
|
|
|
} else { |
|
176
|
|
|
$this->lineIds[$id] = [1]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return end($this->lineIds[$id]) . '|' . $id; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (isset($this->lineIds[$id])) { |
|
183
|
|
|
return array_shift($this->lineIds[$id]) . '|' . $id; |
|
184
|
|
|
} else { |
|
185
|
|
|
return '1|' . $id; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Check to see if the provided line has any associated comments. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $line |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function find($line) |
|
195
|
|
|
{ |
|
196
|
|
|
$lineId = $this->getLineId($line, false); |
|
197
|
|
|
if (!isset($this->stored[$lineId]) || empty($this->stored[$lineId])) { |
|
198
|
|
|
return []; |
|
199
|
|
|
} |
|
200
|
|
|
// The stored result is a stack of accumulated comments. Pop |
|
201
|
|
|
// one off; if more remain, they will be attached to the next |
|
202
|
|
|
// line with the same value. |
|
203
|
|
|
return array_shift($this->stored[$lineId]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Remove all of the blank lines from the end of an array of lines. |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function removeTrailingBlankLines($lines) |
|
210
|
|
|
{ |
|
211
|
|
|
// Remove all of the trailing blank lines. |
|
212
|
|
|
while (!empty($lines) && $this->isBlank(end($lines))) { |
|
213
|
|
|
array_pop($lines); |
|
214
|
|
|
} |
|
215
|
|
|
return $lines; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Return 'true' if the provided line is empty (save for whitespace) |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function isBlank($line) |
|
222
|
|
|
{ |
|
223
|
|
|
return preg_match('#^\s*$#', $line); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|