-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGlob.php
535 lines (477 loc) · 16.8 KB
/
Glob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/*
* This file is part of the webmozart/glob package.
*
* (c) Bernhard Schussek <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Glob;
use InvalidArgumentException;
use Webmozart\Glob\Iterator\GlobIterator;
/**
* Searches and matches file paths using Ant-like globs.
*
* This class implements an Ant-like version of PHP's `glob()` function. The
* wildcard "*" matches any number of characters except directory separators.
* The double wildcard "**" matches any number of characters, including
* directory separators.
*
* Use {@link glob()} to glob the filesystem for paths:
*
* ```php
* foreach (Glob::glob('/project/**.twig') as $path) {
* // do something...
* }
* ```
*
* Use {@link match()} to match a file path against a glob:
*
* ```php
* if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) {
* // path matches
* }
* ```
*
* You can also filter an array of paths for all paths that match your glob with
* {@link filter()}:
*
* ```php
* $filteredPaths = Glob::filter($paths, '/project/**.twig');
* ```
*
* Internally, the methods described above convert the glob into a regular
* expression that is then matched against the matched paths. If you need to
* match many paths against the same glob, you should convert the glob manually
* and use {@link preg_match()} to test the paths:
*
* ```php
* $staticPrefix = Glob::getStaticPrefix('/project/**.twig');
* $regEx = Glob::toRegEx('/project/**.twig');
*
* if (0 !== strpos($path, $staticPrefix)) {
* // no match
* }
*
* if (!preg_match($regEx, $path)) {
* // no match
* }
* ```
*
* The method {@link getStaticPrefix()} returns the part of the glob up to the
* first wildcard "*". You should always test whether a path has this prefix
* before calling the much more expensive {@link preg_match()}.
*
* @since 1.0
*
* @author Bernhard Schussek <[email protected]>
*/
final class Glob
{
/**
* Flag: Filter the values in {@link Glob::filter()}.
*/
const FILTER_VALUE = 1;
/**
* Flag: Filter the keys in {@link Glob::filter()}.
*/
const FILTER_KEY = 2;
/**
* Globs the file system paths matching the glob.
*
* The glob may contain the wildcard "*". This wildcard matches any number
* of characters, *including* directory separators.
*
* ```php
* foreach (Glob::glob('/project/**.twig') as $path) {
* // do something...
* }
* ```
*
* @param string $glob The canonical glob. The glob should contain forward
* slashes as directory separators only. It must not
* contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize globs
* prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in this
* class.
*
* @return string[] The matching paths. The keys of the array are
* incrementing integers.
*/
public static function glob($glob, $flags = 0)
{
$results = iterator_to_array(new GlobIterator($glob, $flags));
sort($results);
return $results;
}
/**
* Matches a path against a glob.
*
* ```php
* if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) {
* // path matches
* }
* ```
*
* @param string $path The path to match.
* @param string $glob The canonical glob. The glob should contain forward
* slashes as directory separators only. It must not
* contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize globs
* prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in
* this class.
*
* @return bool Returns `true` if the path is matched by the glob.
*/
public static function match($path, $glob, $flags = 0)
{
if (!self::isDynamic($glob)) {
return $glob === $path;
}
if (0 !== strpos($path, self::getStaticPrefix($glob, $flags))) {
return false;
}
if (!preg_match(self::toRegEx($glob, $flags), $path)) {
return false;
}
return true;
}
/**
* Filters an array for paths matching a glob.
*
* The filtered array is returned. This array preserves the keys of the
* passed array.
*
* ```php
* $filteredPaths = Glob::filter($paths, '/project/**.twig');
* ```
*
* @param string[] $paths A list of paths.
* @param string $glob The canonical glob. The glob should contain
* forward slashes as directory separators only. It
* must not contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize
* globs prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in
* this class.
*
* @return string[] The paths matching the glob indexed by their original
* keys.
*/
public static function filter(array $paths, $glob, $flags = self::FILTER_VALUE)
{
if (($flags & self::FILTER_VALUE) && ($flags & self::FILTER_KEY)) {
throw new InvalidArgumentException('The flags Glob::FILTER_VALUE and Glob::FILTER_KEY cannot be passed at the same time.');
}
if (!self::isDynamic($glob)) {
if ($flags & self::FILTER_KEY) {
return isset($paths[$glob]) ? array($glob => $paths[$glob]) : array();
}
$key = array_search($glob, $paths);
return false !== $key ? array($key => $glob) : array();
}
$staticPrefix = self::getStaticPrefix($glob, $flags);
$regExp = self::toRegEx($glob, $flags);
$filter = function ($path) use ($staticPrefix, $regExp) {
return 0 === strpos($path, $staticPrefix) && preg_match($regExp, $path);
};
if (PHP_VERSION_ID >= 50600) {
$filterFlags = ($flags & self::FILTER_KEY) ? ARRAY_FILTER_USE_KEY : 0;
return array_filter($paths, $filter, $filterFlags);
}
// No support yet for the third argument of array_filter()
if ($flags & self::FILTER_KEY) {
$result = array();
foreach ($paths as $path => $value) {
if ($filter($path)) {
$result[$path] = $value;
}
}
return $result;
}
return array_filter($paths, $filter);
}
/**
* Returns the base path of a glob.
*
* This method returns the most specific directory that contains all files
* matched by the glob. If this directory does not exist on the file system,
* it's not necessary to execute the glob algorithm.
*
* More specifically, the "base path" is the longest path trailed by a "/"
* on the left of the first wildcard "*". If the glob does not contain
* wildcards, the directory name of the glob is returned.
*
* ```php
* Glob::getBasePath('/css/*.css');
* // => /css
*
* Glob::getBasePath('/css/style.css');
* // => /css
*
* Glob::getBasePath('/css/st*.css');
* // => /css
*
* Glob::getBasePath('/*.css');
* // => /
* ```
*
* @param string $glob The canonical glob. The glob should contain forward
* slashes as directory separators only. It must not
* contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize globs
* prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in this
* class.
*
* @return string The base path of the glob.
*/
public static function getBasePath($glob, $flags = 0)
{
// Search the static prefix for the last "/"
$staticPrefix = self::getStaticPrefix($glob, $flags);
if (false !== ($pos = strrpos($staticPrefix, '/'))) {
// Special case: Return "/" if the only slash is at the beginning
// of the glob
if (0 === $pos) {
return '/';
}
// Special case: Include trailing slash of "scheme:///foo"
if ($pos - 3 === strpos($glob, '://')) {
return substr($staticPrefix, 0, $pos + 1);
}
return substr($staticPrefix, 0, $pos);
}
// Glob contains no slashes on the left of the wildcard
// Return an empty string
return '';
}
/**
* Converts a glob to a regular expression.
*
* Use this method if you need to match many paths against a glob:
*
* ```php
* $staticPrefix = Glob::getStaticPrefix('/project/**.twig');
* $regEx = Glob::toRegEx('/project/**.twig');
*
* if (0 !== strpos($path, $staticPrefix)) {
* // no match
* }
*
* if (!preg_match($regEx, $path)) {
* // no match
* }
* ```
*
* You should always test whether a path contains the static prefix of the
* glob returned by {@link getStaticPrefix()} to reduce the number of calls
* to the expensive {@link preg_match()}.
*
* @param string $glob The canonical glob. The glob should contain forward
* slashes as directory separators only. It must not
* contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize globs
* prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in this
* class.
*
* @return string The regular expression for matching the glob.
*/
public static function toRegEx($glob, $flags = 0, $delimiter = '~')
{
if (!Path::isAbsolute((string) $glob) && false === strpos($glob, '://')) {
throw new InvalidArgumentException(sprintf(
'The glob "%s" is not absolute and not a URI.',
$glob
));
}
$inSquare = false;
$curlyLevels = 0;
$regex = '';
$length = strlen($glob);
for ($i = 0; $i < $length; ++$i) {
$c = $glob[$i];
switch ($c) {
case '.':
case '(':
case ')':
case '|':
case '+':
case '^':
case '$':
case $delimiter:
$regex .= "\\$c";
break;
case '/':
if (isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
$regex .= '/([^/]+/)*';
$i += 3;
} else {
$regex .= '/';
}
break;
case '*':
$regex .= '[^/]*';
break;
case '?':
$regex .= '.';
break;
case '{':
$regex .= '(';
++$curlyLevels;
break;
case '}':
if ($curlyLevels > 0) {
$regex .= ')';
--$curlyLevels;
} else {
$regex .= '}';
}
break;
case ',':
$regex .= $curlyLevels > 0 ? '|' : ',';
break;
case '[':
$regex .= '[';
$inSquare = true;
if (isset($glob[$i + 1]) && '^' === $glob[$i + 1]) {
$regex .= '^';
++$i;
}
break;
case ']':
$regex .= $inSquare ? ']' : '\\]';
$inSquare = false;
break;
case '-':
$regex .= $inSquare ? '-' : '\\-';
break;
case '\\':
if (isset($glob[$i + 1])) {
switch ($glob[$i + 1]) {
case '*':
case '?':
case '{':
case '}':
case '[':
case ']':
case '-':
case '^':
case '\\':
$regex .= '\\'.$glob[$i + 1];
++$i;
break;
default:
$regex .= '\\\\';
}
} else {
$regex .= '\\\\';
}
break;
default:
$regex .= $c;
break;
}
}
if ($inSquare) {
throw new InvalidArgumentException(sprintf(
'Invalid glob: missing ] in %s',
$glob
));
}
if ($curlyLevels > 0) {
throw new InvalidArgumentException(sprintf(
'Invalid glob: missing } in %s',
$glob
));
}
return $delimiter.'^'.$regex.'$'.$delimiter;
}
/**
* Returns the static prefix of a glob.
*
* The "static prefix" is the part of the glob up to the first wildcard "*".
* If the glob does not contain wildcards, the full glob is returned.
*
* @param string $glob The canonical glob. The glob should contain forward
* slashes as directory separators only. It must not
* contain any "." or ".." segments. Use the
* "webmozart/path-util" utility to canonicalize globs
* prior to calling this method.
* @param int $flags A bitwise combination of the flag constants in this
* class.
*
* @return string The static prefix of the glob.
*/
public static function getStaticPrefix($glob, $flags = 0)
{
if (!Path::isAbsolute((string) $glob) && false === strpos($glob, '://')) {
throw new InvalidArgumentException(sprintf(
'The glob "%s" is not absolute and not a URI.',
$glob
));
}
$prefix = '';
$length = strlen($glob);
for ($i = 0; $i < $length; ++$i) {
$c = $glob[$i];
switch ($c) {
case '/':
$prefix .= '/';
if (isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
break 2;
}
break;
case '*':
case '?':
case '{':
case '[':
break 2;
case '\\':
if (isset($glob[$i + 1])) {
switch ($glob[$i + 1]) {
case '*':
case '?':
case '{':
case '[':
case '\\':
$prefix .= $glob[$i + 1];
++$i;
break;
default:
$prefix .= '\\';
}
} else {
$prefix .= '\\';
}
break;
default:
$prefix .= $c;
break;
}
}
return $prefix;
}
/**
* Returns whether the glob contains a dynamic part.
*
* The glob contains a dynamic part if it contains an unescaped "*" or
* "{" character.
*
* @param string $glob The glob to test.
*
* @return bool Returns `true` if the glob contains a dynamic part and
* `false` otherwise.
*/
public static function isDynamic($glob)
{
return false !== strpos($glob, '*') || false !== strpos($glob, '{') || false !== strpos($glob, '?') || false !== strpos($glob, '[');
}
private function __construct()
{
}
}