|
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
|
|
|
* <p>This is a utility class used by selectors and DirectoryScanner. The |
|
22
|
|
|
* functionality more properly belongs just to selectors, but unfortunately |
|
23
|
|
|
* DirectoryScanner exposed these as protected methods. Thus we have to |
|
24
|
|
|
* support any subclasses of DirectoryScanner that may access these methods. |
|
25
|
|
|
* </p> |
|
26
|
|
|
* <p>This is a Singleton.</p> |
|
27
|
|
|
* |
|
28
|
|
|
* @author Hans Lellelid, [email protected] (Phing) |
|
29
|
|
|
* @author Arnout J. Kuiper, [email protected] (Ant) |
|
30
|
|
|
* @author Magesh Umasankar |
|
31
|
|
|
* @author Bruce Atherton, [email protected] (Ant) |
|
32
|
|
|
* @package phing.types.selectors |
|
33
|
|
|
*/ |
|
34
|
|
|
class SelectorUtils |
|
35
|
|
|
{ |
|
36
|
|
|
private static $instance; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Retrieves the instance of the Singleton. |
|
40
|
|
|
*/ |
|
41
|
7 |
|
public static function getInstance() |
|
42
|
|
|
{ |
|
43
|
7 |
|
if (!isset(self::$instance)) { |
|
44
|
1 |
|
self::$instance = new SelectorUtils(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
7 |
|
return self::$instance; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Tests whether or not a given path matches the start of a given |
|
52
|
|
|
* pattern up to the first "**". |
|
53
|
|
|
* <p> |
|
54
|
|
|
* This is not a general purpose test and should only be used if you |
|
55
|
|
|
* can live with false positives. For example, <code>pattern=**\a</code> |
|
56
|
|
|
* and <code>str=b</code> will yield <code>true</code>. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $pattern |
|
59
|
|
|
* @param string $str |
|
60
|
|
|
* @param bool $isCaseSensitive |
|
61
|
|
|
* |
|
62
|
|
|
* @internal param The $pattern pattern to match against. Must not be |
|
63
|
|
|
* <code>null</code>. |
|
64
|
|
|
* @internal param The $str path to match, as a String. Must not be |
|
65
|
|
|
* <code>null</code>. |
|
66
|
|
|
* @internal param Whether $isCaseSensitive or not matching should be performed |
|
67
|
|
|
* case sensitively. |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool whether or not a given path matches the start of a given |
|
70
|
|
|
* pattern up to the first "**". |
|
71
|
|
|
*/ |
|
72
|
44 |
|
public static function matchPatternStart($pattern, $str, $isCaseSensitive = true) |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
// When str starts with a DIRECTORY_SEPARATOR, pattern has to start with a |
|
76
|
|
|
// DIRECTORY_SEPARATOR. |
|
77
|
|
|
// When pattern starts with a DIRECTORY_SEPARATOR, str has to start with a |
|
78
|
|
|
// DIRECTORY_SEPARATOR. |
|
79
|
|
|
if ( |
|
80
|
44 |
|
StringHelper::startsWith(DIRECTORY_SEPARATOR, $str) !== StringHelper::startsWith( |
|
81
|
44 |
|
DIRECTORY_SEPARATOR, |
|
82
|
44 |
|
$pattern |
|
83
|
|
|
) |
|
84
|
|
|
) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
44 |
|
$patDirs = explode(DIRECTORY_SEPARATOR, $pattern); |
|
89
|
44 |
|
$strDirs = explode(DIRECTORY_SEPARATOR, $str); |
|
90
|
|
|
|
|
91
|
44 |
|
$patIdxStart = 0; |
|
92
|
44 |
|
$patIdxEnd = count($patDirs) - 1; |
|
93
|
44 |
|
$strIdxStart = 0; |
|
94
|
44 |
|
$strIdxEnd = count($strDirs) - 1; |
|
95
|
|
|
|
|
96
|
|
|
// up to first '**' |
|
97
|
44 |
|
while ($patIdxStart <= $patIdxEnd && $strIdxStart <= $strIdxEnd) { |
|
98
|
44 |
|
$patDir = $patDirs[$patIdxStart]; |
|
99
|
44 |
|
if ($patDir == "**") { |
|
100
|
17 |
|
break; |
|
101
|
|
|
} |
|
102
|
31 |
|
if (!self::match($patDir, $strDirs[$strIdxStart], $isCaseSensitive)) { |
|
103
|
20 |
|
return false; |
|
104
|
|
|
} |
|
105
|
15 |
|
$patIdxStart++; |
|
106
|
15 |
|
$strIdxStart++; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
28 |
|
if ($strIdxStart > $strIdxEnd) { |
|
110
|
|
|
// String is exhausted |
|
111
|
13 |
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
18 |
|
if ($patIdxStart > $patIdxEnd) { |
|
115
|
|
|
// String not exhausted, but pattern is. Failure. |
|
116
|
1 |
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// pattern now holds ** while string is not exhausted |
|
120
|
|
|
// this will generate false positives but we can live with that. |
|
121
|
17 |
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Tests whether or not a given path matches a given pattern. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $pattern The pattern to match against. Must not be <code>null</code>. |
|
128
|
|
|
* @param string $str The path to match, as a String. Must not be <code>null</code>. |
|
129
|
|
|
* @param bool $isCaseSensitive Whether or not matching should be performed case sensitively. |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool <code>true</code> if the pattern matches against the string, |
|
132
|
|
|
*/ |
|
133
|
141 |
|
public static function matchPath($pattern, $str, $isCaseSensitive = true) |
|
134
|
|
|
{ |
|
135
|
|
|
// explicitly exclude directory itself |
|
136
|
141 |
|
if ($str == '' && $pattern == '**/*') { |
|
137
|
1 |
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
140 |
|
$rePattern = preg_quote($pattern, '/'); |
|
141
|
140 |
|
$dirSep = preg_quote(DIRECTORY_SEPARATOR, '/'); |
|
142
|
140 |
|
$trailingDirSep = '((' . $dirSep . ')?|(' . $dirSep . ').+)'; |
|
143
|
|
|
$patternReplacements = [ |
|
144
|
140 |
|
$dirSep . '\*\*' . $dirSep => $dirSep . '.*' . $trailingDirSep, |
|
145
|
140 |
|
$dirSep . '\*\*' => $trailingDirSep, |
|
146
|
140 |
|
'\*\*' . $dirSep => '(.*' . $dirSep . ')?', |
|
147
|
140 |
|
'\*\*' => '.*', |
|
148
|
140 |
|
'\*' => '[^' . $dirSep . ']*', |
|
149
|
140 |
|
'\?' => '[^' . $dirSep . ']' |
|
150
|
|
|
]; |
|
151
|
140 |
|
$rePattern = str_replace(array_keys($patternReplacements), array_values($patternReplacements), $rePattern); |
|
152
|
140 |
|
$rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i'); |
|
153
|
|
|
|
|
154
|
140 |
|
return (bool) preg_match($rePattern, $str); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Tests whether or not a string matches against a pattern. |
|
159
|
|
|
* The pattern may contain two special characters:<br> |
|
160
|
|
|
* '*' means zero or more characters<br> |
|
161
|
|
|
* '?' means one and only one character |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $pattern The pattern to match against. |
|
164
|
|
|
* Must not be |
|
165
|
|
|
* <code>null</code>. |
|
166
|
|
|
* @param string $str The string which must be matched against the pattern. |
|
167
|
|
|
* Must not be <code>null</code>. |
|
168
|
|
|
* @param bool $isCaseSensitive Whether or not matching should be performed |
|
169
|
|
|
* case sensitively.case sensitively. |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool <code>true</code> if the string matches against the pattern, |
|
172
|
|
|
* or <code>false</code> otherwise. |
|
173
|
|
|
*/ |
|
174
|
31 |
|
public static function match($pattern, $str, $isCaseSensitive = true) |
|
175
|
|
|
{ |
|
176
|
31 |
|
$rePattern = preg_quote($pattern, '/'); |
|
177
|
31 |
|
$rePattern = str_replace(["\*", "\?"], ['.*', '.'], $rePattern); |
|
178
|
31 |
|
$rePattern = '/^' . $rePattern . '$/' . ($isCaseSensitive ? '' : 'i'); |
|
179
|
|
|
|
|
180
|
31 |
|
return (bool) preg_match($rePattern, $str); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns dependency information on these two files. If src has been |
|
185
|
|
|
* modified later than target, it returns true. If target doesn't exist, |
|
186
|
|
|
* it likewise returns true. Otherwise, target is newer than src and |
|
187
|
|
|
* is not out of date, thus the method returns false. It also returns |
|
188
|
|
|
* false if the src file doesn't even exist, since how could the |
|
189
|
|
|
* target then be out of date. |
|
190
|
|
|
* |
|
191
|
|
|
* @param PhingFile $src the original file |
|
192
|
|
|
* @param PhingFile $target the file being compared against |
|
193
|
|
|
* @param int $granularity the amount in seconds of slack we will give in |
|
194
|
|
|
* determining out of dateness |
|
195
|
|
|
* @return bool whether the target is out of date |
|
196
|
|
|
*/ |
|
197
|
3 |
|
public static function isOutOfDate(PhingFile $src, PhingFile $target, $granularity) |
|
198
|
|
|
{ |
|
199
|
3 |
|
if (!$src->exists()) { |
|
200
|
1 |
|
return false; |
|
201
|
|
|
} |
|
202
|
2 |
|
if (!$target->exists()) { |
|
203
|
1 |
|
return true; |
|
204
|
|
|
} |
|
205
|
1 |
|
if (($src->lastModified() - $granularity) > $target->lastModified()) { |
|
206
|
|
|
return true; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
return false; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string $string |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
1 |
|
public static function removeWhitespace($string) |
|
217
|
|
|
{ |
|
218
|
1 |
|
return preg_replace( |
|
219
|
1 |
|
"/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/", |
|
220
|
1 |
|
'', |
|
221
|
1 |
|
$string |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|