|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Symfony\Component\Finder; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Glob matches globbing patterns against text. |
|
16
|
|
|
* |
|
17
|
|
|
* if match_glob("foo.*", "foo.bar") echo "matched\n"; |
|
18
|
|
|
* |
|
19
|
|
|
* // prints foo.bar and foo.baz |
|
20
|
|
|
* $regex = glob_to_regex("foo.*"); |
|
21
|
|
|
* for (['foo.bar', 'foo.baz', 'foo', 'bar'] as $t) |
|
22
|
|
|
* { |
|
23
|
|
|
* if (/$regex/) echo "matched: $car\n"; |
|
24
|
|
|
* } |
|
25
|
|
|
* |
|
26
|
|
|
* Glob implements glob(3) style matching that can be used to match |
|
27
|
|
|
* against text, rather than fetching names from a filesystem. |
|
28
|
|
|
* |
|
29
|
|
|
* Based on the Perl Text::Glob module. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Fabien Potencier <[email protected]> PHP port |
|
32
|
|
|
* @author Richard Clamp <[email protected]> Perl version |
|
33
|
|
|
* @copyright 2004-2005 Fabien Potencier <[email protected]> |
|
34
|
|
|
* @copyright 2002 Richard Clamp <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class Glob |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Returns a regexp which is the equivalent of the glob pattern. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#') |
|
44
|
|
|
{ |
|
45
|
|
|
$firstByte = true; |
|
46
|
|
|
$escaping = false; |
|
47
|
|
|
$inCurlies = 0; |
|
48
|
|
|
$regex = ''; |
|
49
|
|
|
$sizeGlob = \strlen($glob); |
|
50
|
|
|
for ($i = 0; $i < $sizeGlob; ++$i) { |
|
51
|
|
|
$car = $glob[$i]; |
|
52
|
|
|
if ($firstByte && $strictLeadingDot && '.' !== $car) { |
|
53
|
|
|
$regex .= '(?=[^\.])'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$firstByte = '/' === $car; |
|
57
|
|
|
|
|
58
|
|
|
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) { |
|
59
|
|
|
$car = '[^/]++/'; |
|
60
|
|
|
if (!isset($glob[$i + 3])) { |
|
61
|
|
|
$car .= '?'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($strictLeadingDot) { |
|
65
|
|
|
$car = '(?=[^\.])'.$car; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$car = '/(?:'.$car.')*'; |
|
69
|
|
|
$i += 2 + isset($glob[$i + 3]); |
|
70
|
|
|
|
|
71
|
|
|
if ('/' === $delimiter) { |
|
72
|
|
|
$car = str_replace('/', '\\/', $car); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) { |
|
77
|
|
|
$regex .= "\\$car"; |
|
78
|
|
|
} elseif ('*' === $car) { |
|
79
|
|
|
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*'); |
|
80
|
|
|
} elseif ('?' === $car) { |
|
81
|
|
|
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.'); |
|
82
|
|
|
} elseif ('{' === $car) { |
|
83
|
|
|
$regex .= $escaping ? '\\{' : '('; |
|
84
|
|
|
if (!$escaping) { |
|
85
|
|
|
++$inCurlies; |
|
86
|
|
|
} |
|
87
|
|
|
} elseif ('}' === $car && $inCurlies) { |
|
88
|
|
|
$regex .= $escaping ? '}' : ')'; |
|
89
|
|
|
if (!$escaping) { |
|
90
|
|
|
--$inCurlies; |
|
91
|
|
|
} |
|
92
|
|
|
} elseif (',' === $car && $inCurlies) { |
|
93
|
|
|
$regex .= $escaping ? ',' : '|'; |
|
94
|
|
|
} elseif ('\\' === $car) { |
|
95
|
|
|
if ($escaping) { |
|
96
|
|
|
$regex .= '\\\\'; |
|
97
|
|
|
$escaping = false; |
|
98
|
|
|
} else { |
|
99
|
|
|
$escaping = true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
continue; |
|
103
|
|
|
} else { |
|
104
|
|
|
$regex .= $car; |
|
105
|
|
|
} |
|
106
|
|
|
$escaping = false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $delimiter.'^'.$regex.'$'.$delimiter; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|