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 the filename.
|
22
|
|
*
|
23
|
|
* @author Hans Lellelid, hans@xmpl.org (Phing)
|
24
|
|
* @author Bruce Atherton, bruce@callenish.com (Ant)
|
25
|
|
*
|
26
|
|
* @package phing.types.selectors
|
27
|
|
*/
|
28
|
|
class FilenameSelector extends BaseExtendSelector
|
29
|
|
{
|
30
|
|
private $pattern = null;
|
31
|
|
private $regex = null;
|
32
|
|
private $casesensitive = true;
|
33
|
|
private $negated = false;
|
34
|
|
public const NAME_KEY = "name";
|
35
|
|
public const CASE_KEY = "casesensitive";
|
36
|
|
public const NEGATE_KEY = "negate";
|
37
|
|
public const REGEX_KEY = "regex";
|
38
|
|
|
39
|
|
private $reg;
|
40
|
|
private $expression;
|
41
|
|
|
42
|
|
/**
|
43
|
|
* @return string
|
44
|
|
*/
|
45
|
1
|
public function __toString()
|
46
|
|
{
|
47
|
1
|
$buf = "{filenameselector name: ";
|
48
|
1
|
if ($this->pattern !== null) {
|
49
|
1
|
$buf .= $this->pattern;
|
50
|
|
}
|
51
|
1
|
if ($this->regex != null) {
|
52
|
0
|
$buf .= $this->regex . " [as regular expression]";
|
53
|
|
}
|
54
|
1
|
$buf .= " negate: ";
|
55
|
1
|
if ($this->negated) {
|
56
|
0
|
$buf .= "true";
|
57
|
|
} else {
|
58
|
1
|
$buf .= "false";
|
59
|
|
}
|
60
|
1
|
$buf .= " casesensitive: ";
|
61
|
1
|
if ($this->casesensitive) {
|
62
|
1
|
$buf .= "true";
|
63
|
|
} else {
|
64
|
0
|
$buf .= "false";
|
65
|
|
}
|
66
|
1
|
$buf .= "}";
|
67
|
|
|
68
|
1
|
return $buf;
|
69
|
|
}
|
70
|
|
|
71
|
|
/**
|
72
|
|
* The name of the file, or the pattern for the name, that
|
73
|
|
* should be used for selection.
|
74
|
|
*
|
75
|
|
* @param string $pattern the file pattern that any filename must match
|
76
|
|
* against in order to be selected.
|
77
|
|
*
|
78
|
|
* @return void
|
79
|
|
*/
|
80
|
1
|
public function setName($pattern)
|
81
|
|
{
|
82
|
1
|
$pattern = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $pattern);
|
83
|
|
|
84
|
1
|
if (StringHelper::endsWith(DIRECTORY_SEPARATOR, $pattern)) {
|
85
|
0
|
$pattern .= "**";
|
86
|
|
}
|
87
|
1
|
$this->pattern = $pattern;
|
88
|
|
}
|
89
|
|
|
90
|
|
/**
|
91
|
|
* The regular expression the file name will be matched against.
|
92
|
|
*
|
93
|
|
* @param string $pattern the regular expression that any filename must match
|
94
|
|
* against in order to be selected.
|
95
|
|
*/
|
96
|
0
|
public function setRegex($pattern)
|
97
|
|
{
|
98
|
0
|
$this->regex = $pattern;
|
99
|
0
|
$this->reg = null;
|
100
|
|
}
|
101
|
|
|
102
|
|
/**
|
103
|
|
* Whether to ignore case when checking filenames.
|
104
|
|
*
|
105
|
|
* @param bool $casesensitive whether to pay attention to case sensitivity
|
106
|
|
*
|
107
|
|
* @return void
|
108
|
|
*/
|
109
|
0
|
public function setCasesensitive($casesensitive)
|
110
|
|
{
|
111
|
0
|
$this->casesensitive = $casesensitive;
|
112
|
|
}
|
113
|
|
|
114
|
|
/**
|
115
|
|
* You can optionally reverse the selection of this selector,
|
116
|
|
* thereby emulating an <exclude> tag, by setting the attribute
|
117
|
|
* negate to true. This is identical to surrounding the selector
|
118
|
|
* with <not></not>.
|
119
|
|
*
|
120
|
|
* @param bool $negated whether to negate this selection
|
121
|
|
*
|
122
|
|
* @return void
|
123
|
|
*/
|
124
|
0
|
public function setNegate($negated)
|
125
|
|
{
|
126
|
0
|
$this->negated = $negated;
|
127
|
|
}
|
128
|
|
|
129
|
|
/**
|
130
|
|
* When using this as a custom selector, this method will be called.
|
131
|
|
* It translates each parameter into the appropriate setXXX() call.
|
132
|
|
*
|
133
|
|
* @param array $parameters the complete set of parameters for this selector
|
134
|
|
*
|
135
|
|
* @return void
|
136
|
|
*/
|
137
|
0
|
public function setParameters(array $parameters): void
|
138
|
|
{
|
139
|
0
|
parent::setParameters($parameters);
|
140
|
0
|
if ($parameters !== null) {
|
141
|
0
|
for ($i = 0, $len = count($parameters); $i < $len; $i++) {
|
142
|
0
|
$paramname = $parameters[$i]->getName();
|
143
|
0
|
switch (strtolower($paramname)) {
|
144
|
|
case self::NAME_KEY:
|
145
|
0
|
$this->setName($parameters[$i]->getValue());
|
146
|
0
|
break;
|
147
|
|
case self::CASE_KEY:
|
148
|
0
|
$this->setCasesensitive(Project::toBoolean($parameters[$i]->getValue()));
|
149
|
0
|
break;
|
150
|
|
case self::NEGATE_KEY:
|
151
|
0
|
$this->setNegate(Project::toBoolean($parameters[$i]->getValue()));
|
152
|
0
|
break;
|
153
|
|
case self::REGEX_KEY:
|
154
|
0
|
$this->setRegex($parameters[$i]->getValue());
|
155
|
0
|
break;
|
156
|
|
default:
|
157
|
0
|
$this->setError("Invalid parameter " . $paramname);
|
158
|
|
}
|
159
|
|
} // for each param
|
160
|
|
} // if params
|
161
|
|
}
|
162
|
|
|
163
|
|
/**
|
164
|
|
* Checks to make sure all settings are kosher. In this case, it
|
165
|
|
* means that the name attribute has been set.
|
166
|
|
*
|
167
|
|
* {@inheritdoc}
|
168
|
|
*
|
169
|
|
* @return void
|
170
|
|
*/
|
171
|
1
|
public function verifySettings()
|
172
|
|
{
|
173
|
1
|
if ($this->pattern === null && $this->regex === null) {
|
174
|
0
|
$this->setError("The name or regex attribute is required");
|
175
|
1
|
} elseif ($this->pattern !== null && $this->regex !== null) {
|
176
|
0
|
$this->setError("Only one of name and regex attribute is allowed");
|
177
|
|
}
|
178
|
|
}
|
179
|
|
|
180
|
|
/**
|
181
|
|
* The heart of the matter. This is where the selector gets to decide
|
182
|
|
* on the inclusion of a file in a particular fileset. Most of the work
|
183
|
|
* for this selector is offloaded into SelectorUtils, a static class
|
184
|
|
* that provides the same services for both FilenameSelector and
|
185
|
|
* DirectoryScanner.
|
186
|
|
*
|
187
|
|
* {@inheritdoc}
|
188
|
|
*
|
189
|
|
* @param PhingFile $basedir the base directory the scan is being done from
|
190
|
|
* @param string $filename is the name of the file to check
|
191
|
|
* @param PhingFile $file is a PhingFile object the selector can use
|
192
|
|
*
|
193
|
|
* @return bool whether the file should be selected or not
|
194
|
|
*/
|
195
|
1
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
|
196
|
|
{
|
197
|
1
|
$this->validate();
|
198
|
|
|
199
|
1
|
if ($this->pattern !== null) {
|
200
|
1
|
return (SelectorUtils::matchPath($this->pattern, $filename, $this->casesensitive)
|
201
|
1
|
=== !($this->negated));
|
202
|
|
}
|
203
|
0
|
if ($this->reg === null) {
|
204
|
0
|
$this->reg = new RegularExpression();
|
205
|
0
|
$this->reg->setPattern($this->regex);
|
206
|
0
|
$this->expression = $this->reg->getRegexp($this->getProject());
|
207
|
|
}
|
208
|
0
|
$this->reg->setIgnoreCase(!$this->casesensitive);
|
209
|
0
|
return $this->expression->matches($filename) === !$this->negated;
|
210
|
|
}
|
211
|
|
}
|