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
|
|
|
* When using this as a custom selector, this method will be called. |
101
|
|
|
* It translates each parameter into the appropriate setXXX() call. |
102
|
|
|
* |
103
|
|
|
* @param array $parameters the complete set of parameters for this selector |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setParameters(array $parameters): void |
108
|
|
|
{ |
109
|
|
|
parent::setParameters($parameters); |
110
|
|
|
if ($parameters !== null) { |
|
|
|
|
111
|
|
|
for ($i = 0, $size = count($parameters); $i < $size; $i++) { |
112
|
|
|
$paramname = $parameters[$i]->getName(); |
113
|
|
|
switch (strtolower($paramname)) { |
114
|
|
|
case self::EXPRESSION_KEY: |
115
|
|
|
$this->setExpression($parameters[$i]->getValue()); |
116
|
|
|
break; |
117
|
|
|
case self::CASE_KEY: |
118
|
|
|
$this->setCasesensitive(Project::toBoolean($parameters[$i]->getValue())); |
119
|
|
|
break; |
120
|
|
|
case self::ML_KEY: |
121
|
|
|
$this->setMultiLine(Project::toBoolean($parameters[$i]->getValue())); |
|
|
|
|
122
|
|
|
break; |
123
|
|
|
default: |
124
|
|
|
$this->setError("Invalid parameter " . $paramname); |
125
|
|
|
} |
126
|
|
|
} // for each param |
127
|
|
|
} // if params |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Checks to make sure all settings are kosher. In this case, it |
132
|
|
|
* means that the pattern attribute has been set. |
133
|
|
|
*/ |
134
|
1 |
|
public function verifySettings() |
135
|
|
|
{ |
136
|
1 |
|
if ($this->userProvidedExpression === null) { |
137
|
|
|
$this->setError("The expression attribute is required"); |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* The heart of the matter. This is where the selector gets to decide |
143
|
|
|
* on the inclusion of a file in a particular fileset. |
144
|
|
|
* |
145
|
|
|
* @param PhingFile $basedir base directory the scan is being done from |
146
|
|
|
* @param string $filename the name of the file to check |
147
|
|
|
* @param PhingFile $file PhingFile object the selector can use |
148
|
|
|
* |
149
|
|
|
* @throws BuildException |
150
|
|
|
* |
151
|
|
|
* @return bool whether the file should be selected or not |
152
|
|
|
*/ |
153
|
1 |
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file) |
154
|
|
|
{ |
155
|
1 |
|
$this->validate(); |
156
|
|
|
|
157
|
|
|
try { |
158
|
1 |
|
if ($file->isDirectory() || $file->isLink()) { |
159
|
1 |
|
return true; |
160
|
|
|
} |
161
|
|
|
} catch (IOException $ioe) { |
162
|
|
|
if (OsCondition::isOS('windows')) { |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
throw new BuildException($ioe); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if ($this->myRegExp === null) { |
170
|
1 |
|
$this->myRegExp = new RegularExpression(); |
171
|
1 |
|
$this->myRegExp->setPattern($this->userProvidedExpression); |
172
|
1 |
|
$this->myExpression = $this->myRegExp->getRegexp($this->getProject()); |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
$in = null; |
|
|
|
|
176
|
|
|
try { |
177
|
1 |
|
$in = new BufferedReader(new FileReader($file)); |
178
|
1 |
|
$teststr = $in->readLine(); |
179
|
1 |
|
while ($teststr !== null) { |
180
|
1 |
|
$this->myExpression->setMultiline($this->multiline); |
181
|
1 |
|
$this->myExpression->setIgnoreCase(!$this->casesensitive); |
182
|
1 |
|
if ($this->myExpression->matches($teststr)) { |
183
|
1 |
|
return true; |
184
|
|
|
} |
185
|
1 |
|
$teststr = $in->readLine(); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
$in->close(); |
189
|
|
|
|
190
|
1 |
|
return false; |
191
|
|
|
} catch (IOException $ioe) { |
192
|
|
|
if ($in) { |
|
|
|
|
193
|
|
|
$in->close(); |
194
|
|
|
} |
195
|
|
|
throw new BuildException("Could not read file " . $filename); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|