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 the user-specified |
22
|
|
|
* regular expression matching strings. |
23
|
|
|
* |
24
|
|
|
* Example: |
25
|
|
|
* <pre><linecontainsregexp> |
26
|
|
|
* <regexp pattern="foo*"> |
27
|
|
|
* </linecontainsregexp></pre> |
28
|
|
|
* |
29
|
|
|
* Or: |
30
|
|
|
* |
31
|
|
|
* <pre><filterreader classname="phing.filters.LineContainsRegExp"> |
32
|
|
|
* <param type="regexp" value="foo*"/> |
33
|
|
|
* </filterreader></pre> |
34
|
|
|
* |
35
|
|
|
* This will fetch all those lines that contain the pattern <code>foo</code> |
36
|
|
|
* |
37
|
|
|
* @author Yannick Lecaillez <[email protected]> |
38
|
|
|
* @author Hans Lellelid <[email protected]> |
39
|
|
|
* @see FilterReader |
40
|
|
|
* @package phing.filters |
41
|
|
|
*/ |
42
|
|
|
class LineContainsRegexp extends BaseParamFilterReader implements ChainableReader |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parameter name for regular expression. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const REGEXP_KEY = "regexp"; |
51
|
|
|
const NEGATE_KEY = 'negate'; |
52
|
|
|
const CS_KEY = 'casesensitive'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Regular expressions that are applied against lines. |
56
|
|
|
* |
57
|
|
|
* @var RegularExpression[] |
58
|
|
|
*/ |
59
|
|
|
private $regexps = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var bool $negate |
63
|
|
|
*/ |
64
|
|
|
private $negate = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool $casesensitive |
68
|
|
|
*/ |
69
|
|
|
private $casesensitive = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all lines in a buffer that contain specified strings. |
73
|
|
|
* |
74
|
|
|
* @param int $len |
75
|
|
|
* @return mixed buffer, -1 on EOF |
76
|
|
|
* @throws IOException |
77
|
|
|
* @throws RegexpException |
78
|
|
|
*/ |
79
|
3 |
|
public function read($len = null) |
80
|
|
|
{ |
81
|
3 |
|
if (!$this->getInitialized()) { |
82
|
3 |
|
$this->initialize(); |
83
|
3 |
|
$this->setInitialized(true); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
$buffer = $this->in->read($len); |
87
|
|
|
|
88
|
3 |
|
if ($buffer === -1) { |
89
|
3 |
|
return -1; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
$lines = explode("\n", $buffer); |
93
|
3 |
|
$matched = []; |
94
|
|
|
|
95
|
3 |
|
$regexpsSize = count($this->regexps); |
96
|
3 |
|
foreach ($lines as $line) { |
97
|
3 |
|
for ($i = 0; $i < $regexpsSize; $i++) { |
98
|
3 |
|
$regexp = $this->regexps[$i]; |
99
|
3 |
|
$re = $regexp->getRegexp($this->getProject()); |
100
|
3 |
|
$re->setIgnoreCase(!$this->casesensitive); |
101
|
3 |
|
$matches = $re->matches($line); |
102
|
3 |
|
if (!$matches) { |
103
|
3 |
|
$line = null; |
104
|
3 |
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
3 |
|
if ($line !== null) { |
108
|
3 |
|
$matched[] = $line; |
109
|
|
|
} |
110
|
|
|
} |
111
|
3 |
|
$filtered_buffer = implode("\n", $matched); |
112
|
|
|
|
113
|
3 |
|
if ($this->isNegated()) { |
114
|
1 |
|
$filtered_buffer = implode("\n", array_diff($lines, $matched)); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return $filtered_buffer; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Whether to match casesensitevly. |
122
|
|
|
* @param bool $b |
123
|
|
|
*/ |
124
|
1 |
|
public function setCaseSensitive(bool $b) |
125
|
|
|
{ |
126
|
1 |
|
$this->casesensitive = $b; |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Find out whether we match casesensitevly. |
131
|
|
|
* |
132
|
|
|
* @return boolean negation flag. |
133
|
|
|
*/ |
134
|
|
|
public function isCaseSensitive() |
135
|
|
|
{ |
136
|
|
|
return $this->casesensitive; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the negation mode. Default false (no negation). |
141
|
|
|
* |
142
|
|
|
* @param boolean $b the boolean negation mode to set. |
143
|
|
|
*/ |
144
|
1 |
|
public function setNegate(bool $b) |
145
|
|
|
{ |
146
|
1 |
|
$this->negate = $b; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Find out whether we have been negated. |
151
|
|
|
* |
152
|
|
|
* @return boolean negation flag. |
153
|
|
|
*/ |
154
|
3 |
|
public function isNegated() |
155
|
|
|
{ |
156
|
3 |
|
return $this->negate; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Adds a <code>regexp</code> element. |
161
|
|
|
* |
162
|
|
|
* @return object regExp The <code>regexp</code> element added. |
163
|
|
|
*/ |
164
|
|
|
public function createRegexp() |
165
|
|
|
{ |
166
|
|
|
$num = array_push($this->regexps, new RegularExpression()); |
167
|
|
|
|
168
|
|
|
return $this->regexps[$num - 1]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets the vector of regular expressions which must be contained within |
173
|
|
|
* a line read from the original stream in order for it to match this |
174
|
|
|
* filter. |
175
|
|
|
* |
176
|
|
|
* @param array $regexps |
177
|
|
|
* @throws Exception |
178
|
|
|
* @internal param An $regexps array of regular expressions which must be contained |
179
|
|
|
* within a line in order for it to match in this filter. Must not be |
180
|
|
|
* <code>null</code>. |
181
|
|
|
*/ |
182
|
|
|
public function setRegexps($regexps) |
183
|
|
|
{ |
184
|
|
|
// type check, error must never occur, bad code of it does |
185
|
|
|
if (!is_array($regexps)) { |
|
|
|
|
186
|
|
|
throw new Exception("Expected an 'array', got something else"); |
187
|
|
|
} |
188
|
|
|
$this->regexps = $regexps; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns the array of regular expressions which must be contained within |
193
|
|
|
* a line read from the original stream in order for it to match this |
194
|
|
|
* filter. |
195
|
|
|
* |
196
|
|
|
* @return array The array of regular expressions which must be contained within |
197
|
|
|
* a line read from the original stream in order for it to match this |
198
|
|
|
* filter. The returned object is "live" - in other words, changes made to |
199
|
|
|
* the returned object are mirrored in the filter. |
200
|
|
|
*/ |
201
|
|
|
public function getRegexps() |
202
|
|
|
{ |
203
|
|
|
return $this->regexps; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set the regular expression as an attribute. |
208
|
|
|
*/ |
209
|
|
|
public function setRegexp($pattern) |
210
|
|
|
{ |
211
|
|
|
$regexp = new RegularExpression(); |
212
|
|
|
$regexp->setPattern($pattern); |
213
|
|
|
$this->regexps[] = $regexp; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Creates a new LineContainsRegExp using the passed in |
218
|
|
|
* Reader for instantiation. |
219
|
|
|
* |
220
|
|
|
* @param Reader $reader |
221
|
|
|
* @return LineContainsRegexp A new filter based on this configuration, but filtering |
222
|
|
|
* the specified reader |
223
|
|
|
* @throws Exception |
224
|
|
|
* @internal param A $object Reader object providing the underlying stream. |
225
|
|
|
* Must not be <code>null</code>. |
226
|
|
|
*/ |
227
|
|
|
public function chain(Reader $reader): Reader |
228
|
|
|
{ |
229
|
|
|
$newFilter = new LineContainsRegexp($reader); |
230
|
|
|
$newFilter->setRegexps($this->getRegexps()); |
231
|
|
|
$newFilter->setNegate($this->isNegated()); |
232
|
|
|
$newFilter->setCaseSensitive($this->isCaseSensitive()); |
233
|
|
|
$newFilter->setInitialized(true); |
234
|
|
|
$newFilter->setProject($this->getProject()); |
235
|
|
|
|
236
|
|
|
return $newFilter; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Parses parameters to add user defined regular expressions. |
241
|
|
|
*/ |
242
|
3 |
|
private function initialize() |
243
|
|
|
{ |
244
|
3 |
|
$params = $this->getParameters(); |
245
|
3 |
|
if ($params !== null) { |
|
|
|
|
246
|
3 |
|
for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) { |
247
|
3 |
|
if (self::REGEXP_KEY === $params[$i]->getType()) { |
248
|
3 |
|
$pattern = $params[$i]->getValue(); |
249
|
3 |
|
$regexp = new RegularExpression(); |
250
|
3 |
|
$regexp->setPattern($pattern); |
251
|
3 |
|
$this->regexps[] = $regexp; |
252
|
2 |
|
} elseif (self::NEGATE_KEY === $params[$i]->getType()) { |
253
|
1 |
|
$this->setNegate(Project::toBoolean($params[$i]->getValue())); |
254
|
1 |
|
} elseif (self::CS_KEY === $params[$i]->getType()) { |
255
|
1 |
|
$this->setCaseSensitive(Project::toBoolean($params[$i]->getValue())); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
3 |
|
} |
260
|
|
|
} |
261
|
|
|
|