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
|
|
|
* Selector that filters files based on whether they contain a |
22
|
|
|
* particular string using regexp. |
23
|
|
|
* |
24
|
|
|
* @author Hans Lellelid <[email protected]> (Phing) |
25
|
|
|
* @author Bruce Atherton <[email protected]> (Ant) |
26
|
|
|
* @package phing.types.selectors |
27
|
|
|
*/ |
28
|
|
|
class ContainsRegexpSelector extends BaseExtendSelector |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The expression set from XML. |
32
|
|
|
* |
33
|
|
|
* @var string $userProvidedExpression |
34
|
|
|
*/ |
35
|
|
|
private $userProvidedExpression; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Regexp $myExpression |
39
|
|
|
*/ |
40
|
|
|
private $myExpression; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool $casesensitive |
44
|
|
|
*/ |
45
|
|
|
private $casesensitive = true; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool $casesensitive |
49
|
|
|
*/ |
50
|
|
|
private $multiline = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var RegularExpression $myRegExp |
54
|
|
|
*/ |
55
|
|
|
private $myRegExp; |
56
|
|
|
|
57
|
|
|
const EXPRESSION_KEY = "expression"; |
58
|
|
|
const CASE_KEY = "casesensitive"; |
59
|
|
|
const ML_KEY = 'multiline'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
1 |
|
public function __toString() |
65
|
|
|
{ |
66
|
1 |
|
$buf = "{containsregexpselector expression: "; |
67
|
1 |
|
$buf .= $this->userProvidedExpression; |
68
|
1 |
|
$buf .= " casesensitive: "; |
69
|
1 |
|
if ($this->casesensitive) { |
70
|
1 |
|
$buf .= "true"; |
71
|
|
|
} else { |
72
|
|
|
$buf .= "false"; |
73
|
|
|
} |
74
|
1 |
|
$buf .= "}"; |
75
|
|
|
|
76
|
1 |
|
return $buf; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The expression to match on within a file. |
81
|
|
|
* |
82
|
|
|
* @param string $exp the string that a file must contain to be selected. |
83
|
|
|
*/ |
84
|
1 |
|
public function setExpression($exp) |
85
|
|
|
{ |
86
|
1 |
|
$this->userProvidedExpression = $exp; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Whether to ignore case in the regex match. |
91
|
|
|
* |
92
|
|
|
* @param boolean $casesensitive whether to pay attention to case sensitivity |
93
|
|
|
*/ |
94
|
|
|
public function setCasesensitive($casesensitive) |
95
|
|
|
{ |
96
|
|
|
$this->casesensitive = $casesensitive; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param bool $multiline |
101
|
|
|
*/ |
102
|
|
|
public function setMultiline(bool $multiline): void |
103
|
|
|
{ |
104
|
|
|
$this->multiline = $multiline; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* When using this as a custom selector, this method will be called. |
109
|
|
|
* It translates each parameter into the appropriate setXXX() call. |
110
|
|
|
* |
111
|
|
|
* @param array $parameters the complete set of parameters for this selector |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function setParameters(array $parameters): void |
116
|
|
|
{ |
117
|
|
|
parent::setParameters($parameters); |
118
|
|
|
if ($parameters !== null) { |
|
|
|
|
119
|
|
|
for ($i = 0, $size = count($parameters); $i < $size; $i++) { |
120
|
|
|
$paramname = $parameters[$i]->getName(); |
121
|
|
|
switch (strtolower($paramname)) { |
122
|
|
|
case self::EXPRESSION_KEY: |
123
|
|
|
$this->setExpression($parameters[$i]->getValue()); |
124
|
|
|
break; |
125
|
|
|
case self::CASE_KEY: |
126
|
|
|
$this->setCasesensitive(Project::toBoolean($parameters[$i]->getValue())); |
127
|
|
|
break; |
128
|
|
|
case self::ML_KEY: |
129
|
|
|
$this->setMultiLine(Project::toBoolean($parameters[$i]->getValue())); |
130
|
|
|
break; |
131
|
|
|
default: |
132
|
|
|
$this->setError("Invalid parameter " . $paramname); |
133
|
|
|
} |
134
|
|
|
} // for each param |
135
|
|
|
} // if params |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks to make sure all settings are kosher. In this case, it |
140
|
|
|
* means that the pattern attribute has been set. |
141
|
|
|
*/ |
142
|
1 |
|
public function verifySettings() |
143
|
|
|
{ |
144
|
1 |
|
if ($this->userProvidedExpression === null) { |
145
|
|
|
$this->setError("The expression attribute is required"); |
146
|
|
|
} |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* The heart of the matter. This is where the selector gets to decide |
151
|
|
|
* on the inclusion of a file in a particular fileset. |
152
|
|
|
* |
153
|
|
|
* @param PhingFile $basedir base directory the scan is being done from |
154
|
|
|
* @param string $filename the name of the file to check |
155
|
|
|
* @param PhingFile $file PhingFile object the selector can use |
156
|
|
|
* |
157
|
|
|
* @return bool whether the file should be selected or not |
158
|
|
|
* @throws IOException |
159
|
|
|
* @throws RegexpException |
160
|
|
|
*/ |
161
|
1 |
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file) |
162
|
|
|
{ |
163
|
1 |
|
$this->validate(); |
164
|
|
|
|
165
|
|
|
try { |
166
|
1 |
|
if ($file->isDirectory() || $file->isLink()) { |
167
|
1 |
|
return true; |
168
|
|
|
} |
169
|
|
|
} catch (IOException $ioe) { |
170
|
|
|
if (OsCondition::isOS('windows')) { |
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
throw new BuildException($ioe); |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
if ($this->myRegExp === null) { |
178
|
1 |
|
$this->myRegExp = new RegularExpression(); |
179
|
1 |
|
$this->myRegExp->setPattern($this->userProvidedExpression); |
180
|
1 |
|
$this->myExpression = $this->myRegExp->getRegexp($this->getProject()); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
$in = null; |
|
|
|
|
184
|
|
|
try { |
185
|
1 |
|
$in = new BufferedReader(new FileReader($file)); |
186
|
1 |
|
$teststr = $in->readLine(); |
187
|
1 |
|
while ($teststr !== null) { |
188
|
1 |
|
$this->myExpression->setMultiline($this->multiline); |
189
|
1 |
|
$this->myExpression->setIgnoreCase(!$this->casesensitive); |
190
|
1 |
|
if ($this->myExpression->matches($teststr)) { |
191
|
1 |
|
return true; |
192
|
|
|
} |
193
|
1 |
|
$teststr = $in->readLine(); |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
$in->close(); |
197
|
|
|
|
198
|
1 |
|
return false; |
199
|
|
|
} catch (IOException $ioe) { |
200
|
|
|
if ($in) { |
|
|
|
|
201
|
|
|
$in->close(); |
202
|
|
|
} |
203
|
|
|
throw new BuildException("Could not read file " . $filename); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|