1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Filter which includes only those lines that contain all the user-specified |
22
|
|
|
* strings. |
23
|
|
|
* |
24
|
|
|
* Example: |
25
|
|
|
* |
26
|
|
|
* <pre><linecontains> |
27
|
|
|
* <contains value="foo"> |
28
|
|
|
* <contains value="bar"> |
29
|
|
|
* </linecontains></pre> |
30
|
|
|
* |
31
|
|
|
* Or: |
32
|
|
|
* |
33
|
|
|
* <pre><filterreader classname="phing.filters.LineContains"> |
34
|
|
|
* <param type="contains" value="foo"/> |
35
|
|
|
* <param type="contains" value="bar"/> |
36
|
|
|
* </filterreader></pre> |
37
|
|
|
* |
38
|
|
|
* This will include only those lines that contain <code>foo</code> and |
39
|
|
|
* <code>bar</code>. |
40
|
|
|
* |
41
|
|
|
* @author Yannick Lecaillez <[email protected]> |
42
|
|
|
* @author Hans Lellelid <[email protected]> |
43
|
|
|
* @see PhingFilterReader |
44
|
|
|
* @package phing.filters |
45
|
|
|
*/ |
46
|
|
|
class LineContains extends BaseParamFilterReader implements ChainableReader |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* The parameter name for the string to match on. |
50
|
|
|
*/ |
51
|
|
|
private const CONTAINS_KEY = 'contains'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The parameter name for the string to match on. |
55
|
|
|
*/ |
56
|
|
|
private const NEGATE_KEY = 'negate'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Array of Contains objects. |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private $contains = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var bool $negate |
67
|
|
|
*/ |
68
|
|
|
private $negate = false; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Remaining line to be read from this filter, or <code>null</code> if |
72
|
|
|
* the next call to <code>read()</code> should read the original stream |
73
|
|
|
* to find the next matching line. |
74
|
|
|
*/ |
75
|
|
|
private $line; |
76
|
|
|
|
77
|
|
|
private $matchAny = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Remaining line to be read from this filter, or <code>null</code> if |
81
|
|
|
* the next call to <code>read()</code> should read the original stream |
82
|
|
|
* to find the next matching line. |
83
|
|
|
* |
84
|
|
|
* @param int|int $len |
85
|
|
|
* |
86
|
|
|
* @return int|string the next character in the resulting stream, or -1 |
87
|
|
|
* if the end of the resulting stream has been reached |
88
|
|
|
* |
89
|
|
|
* @throws IOException if the underlying stream throws an IOException during reading |
90
|
|
|
*/ |
91
|
3 |
|
public function read($len = null) |
92
|
|
|
{ |
93
|
3 |
|
if (!$this->getInitialized()) { |
94
|
2 |
|
$this->initialize(); |
95
|
2 |
|
$this->setInitialized(true); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$ch = -1; |
99
|
|
|
|
100
|
3 |
|
if ($this->line !== null) { |
101
|
3 |
|
$ch = $this->line[0] ?? -1; |
102
|
3 |
|
if (strlen($this->line) === 1) { |
103
|
3 |
|
$this->line = null; |
104
|
|
|
} else { |
105
|
3 |
|
$part = substr($this->line, 1); |
106
|
|
|
|
107
|
3 |
|
$this->line = $part !== false ? $part : null; |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
3 |
|
for ($this->line = $this->readLine(); $this->line !== null; $this->line = $this->readLine()) { |
111
|
3 |
|
$matches = true; |
112
|
3 |
|
foreach ($this->contains as $iValue) { |
113
|
3 |
|
$containsStr = $iValue->getValue(); |
114
|
3 |
|
if ($containsStr === '') { |
115
|
|
|
$this->log( |
116
|
|
|
'The value of <contents> is evaluated to an empty string.', |
117
|
|
|
Project::MSG_DEBUG |
118
|
|
|
); |
119
|
|
|
$matches = false; |
120
|
|
|
} else { |
121
|
3 |
|
$matches = false !== strpos($this->line, $containsStr); |
122
|
|
|
} |
123
|
3 |
|
if (!$matches) { |
124
|
3 |
|
if ($this->matchAny) { |
125
|
1 |
|
continue; |
126
|
|
|
} |
127
|
2 |
|
break; |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
if ($this->matchAny) { |
131
|
1 |
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
3 |
|
if ($matches ^ $this->isNegated()) { |
135
|
3 |
|
break; |
136
|
|
|
} |
137
|
|
|
} |
138
|
3 |
|
if ($this->line !== null) { |
139
|
3 |
|
return $this->read(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
return $ch; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the negation mode. Default false (no negation). |
148
|
|
|
* |
149
|
|
|
* @param boolean $b the boolean negation mode to set. |
150
|
|
|
*/ |
151
|
2 |
|
public function setNegate(bool $b) |
152
|
|
|
{ |
153
|
2 |
|
$this->negate = $b; |
154
|
2 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Find out whether we have been negated. |
158
|
|
|
* |
159
|
|
|
* @return boolean negation flag. |
160
|
|
|
*/ |
161
|
3 |
|
public function isNegated(): bool |
162
|
|
|
{ |
163
|
3 |
|
return $this->negate; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Adds a <code>contains</code> nested element. |
168
|
|
|
* |
169
|
|
|
* @return Contains The <code>contains</code> element added. |
170
|
|
|
* Must not be <code>null</code>. |
171
|
|
|
*/ |
172
|
1 |
|
public function createContains(): \Contains |
173
|
|
|
{ |
174
|
1 |
|
$num = array_push($this->contains, new Contains()); |
175
|
|
|
|
176
|
1 |
|
return $this->contains[$num - 1]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets the array of words which must be contained within a line read |
181
|
|
|
* from the original stream in order for it to match this filter. |
182
|
|
|
* |
183
|
|
|
* @param array $contains An array of words which must be contained |
184
|
|
|
* within a line in order for it to match in this filter. |
185
|
|
|
* Must not be <code>null</code>. |
186
|
|
|
* @throws Exception |
187
|
|
|
*/ |
188
|
1 |
|
private function setContains(array $contains) |
189
|
|
|
{ |
190
|
1 |
|
$this->contains = $contains; |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the vector of words which must be contained within a line read |
195
|
|
|
* from the original stream in order for it to match this filter. |
196
|
|
|
* |
197
|
|
|
* @return array The array of words which must be contained within a line read |
198
|
|
|
* from the original stream in order for it to match this filter. The |
199
|
|
|
* returned object is "live" - in other words, changes made to the |
200
|
|
|
* returned object are mirrored in the filter. |
201
|
|
|
*/ |
202
|
1 |
|
public function getContains(): array |
203
|
|
|
{ |
204
|
1 |
|
return $this->contains; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Creates a new LineContains using the passed in |
209
|
|
|
* Reader for instantiation. |
210
|
|
|
* |
211
|
|
|
* @param Reader $reader A Reader object providing the underlying stream. |
212
|
|
|
* Must not be <code>null</code>. |
213
|
|
|
* |
214
|
|
|
* @return LineContains A new filter based on this configuration, but filtering |
215
|
|
|
* the specified reader |
216
|
|
|
* @throws Exception |
217
|
|
|
*/ |
218
|
1 |
|
public function chain(Reader $reader): Reader |
219
|
|
|
{ |
220
|
1 |
|
$newFilter = new self($reader); |
221
|
1 |
|
$newFilter->setContains($this->getContains()); |
222
|
1 |
|
$newFilter->setNegate($this->isNegated()); |
223
|
1 |
|
$newFilter->setMatchAny($this->isMatchAny()); |
224
|
1 |
|
$newFilter->setInitialized(true); |
225
|
1 |
|
$newFilter->setProject($this->getProject()); |
226
|
|
|
|
227
|
1 |
|
return $newFilter; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Parses the parameters to add user-defined contains strings. |
232
|
|
|
*/ |
233
|
2 |
|
private function initialize() |
234
|
|
|
{ |
235
|
2 |
|
$params = $this->getParameters(); |
236
|
2 |
|
if ($params !== null) { |
|
|
|
|
237
|
2 |
|
foreach ($params as $param) { |
238
|
2 |
|
if (self::CONTAINS_KEY === $param->getType()) { |
239
|
2 |
|
$cont = new Contains(); |
240
|
2 |
|
$cont->setValue($param->getValue()); |
241
|
2 |
|
$this->contains[] = $cont; |
242
|
1 |
|
} elseif (self::NEGATE_KEY === $param->getType()) { |
243
|
1 |
|
$this->setNegate(Project::toBoolean($param->getValue())); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
2 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param bool $matchAny |
251
|
|
|
*/ |
252
|
1 |
|
public function setMatchAny(bool $matchAny): void |
253
|
|
|
{ |
254
|
1 |
|
$this->matchAny = $matchAny; |
255
|
1 |
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
1 |
|
public function isMatchAny(): bool |
261
|
|
|
{ |
262
|
1 |
|
return $this->matchAny; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|