| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 <fabien@symfony.com> PHP port
|
|
|
32 |
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
|
|
33 |
* @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
|
|
|
34 |
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
|
|
35 |
*/
|
|
|
36 |
class Glob
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* Returns a regexp which is the equivalent of the glob pattern.
|
|
|
40 |
*/
|
|
|
41 |
public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#'): string
|
|
|
42 |
{
|
|
|
43 |
$firstByte = true;
|
|
|
44 |
$escaping = false;
|
|
|
45 |
$inCurlies = 0;
|
|
|
46 |
$regex = '';
|
|
|
47 |
$sizeGlob = \strlen($glob);
|
|
|
48 |
for ($i = 0; $i < $sizeGlob; ++$i) {
|
|
|
49 |
$car = $glob[$i];
|
|
|
50 |
if ($firstByte && $strictLeadingDot && '.' !== $car) {
|
|
|
51 |
$regex .= '(?=[^\.])';
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$firstByte = '/' === $car;
|
|
|
55 |
|
|
|
56 |
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
|
|
|
57 |
$car = '[^/]++/';
|
|
|
58 |
if (!isset($glob[$i + 3])) {
|
|
|
59 |
$car .= '?';
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if ($strictLeadingDot) {
|
|
|
63 |
$car = '(?=[^\.])'.$car;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$car = '/(?:'.$car.')*';
|
|
|
67 |
$i += 2 + isset($glob[$i + 3]);
|
|
|
68 |
|
|
|
69 |
if ('/' === $delimiter) {
|
|
|
70 |
$car = str_replace('/', '\\/', $car);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
|
|
|
75 |
$regex .= "\\$car";
|
|
|
76 |
} elseif ('*' === $car) {
|
|
|
77 |
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
|
|
|
78 |
} elseif ('?' === $car) {
|
|
|
79 |
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
|
|
|
80 |
} elseif ('{' === $car) {
|
|
|
81 |
$regex .= $escaping ? '\\{' : '(';
|
|
|
82 |
if (!$escaping) {
|
|
|
83 |
++$inCurlies;
|
|
|
84 |
}
|
|
|
85 |
} elseif ('}' === $car && $inCurlies) {
|
|
|
86 |
$regex .= $escaping ? '}' : ')';
|
|
|
87 |
if (!$escaping) {
|
|
|
88 |
--$inCurlies;
|
|
|
89 |
}
|
|
|
90 |
} elseif (',' === $car && $inCurlies) {
|
|
|
91 |
$regex .= $escaping ? ',' : '|';
|
|
|
92 |
} elseif ('\\' === $car) {
|
|
|
93 |
if ($escaping) {
|
|
|
94 |
$regex .= '\\\\';
|
|
|
95 |
$escaping = false;
|
|
|
96 |
} else {
|
|
|
97 |
$escaping = true;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
continue;
|
|
|
101 |
} else {
|
|
|
102 |
$regex .= $car;
|
|
|
103 |
}
|
|
|
104 |
$escaping = false;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return $delimiter.'^'.$regex.'$'.$delimiter;
|
|
|
108 |
}
|
|
|
109 |
}
|