Cache
2 years ago
DBPermissions.php
2 years ago
DataEncoder.php
2 years ago
Escape.php
2 years ago
Glob.php
2 years ago
Hooks.php
2 years ago
Math.php
2 years ago
PluginInfo.php
2 years ago
Sanitize.php
2 years ago
ServerVars.php
2 years ago
SlashMode.php
5 years ago
Strings.php
2 years ago
Times.php
2 years ago
Urls.php
2 years ago
Version.php
2 years ago
WpDefaultDirectories.php
2 years ago
Glob.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | /** |
| 6 | * This is a port of Symfony\Component\Finder\Glob. |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Glob matches globbing patterns against text. |
| 11 | * |
| 12 | * if match_glob("foo.*", "foo.bar") echo "matched\n"; |
| 13 | * |
| 14 | * // prints foo.bar and foo.baz |
| 15 | * $regex = glob_to_regex("foo.*"); |
| 16 | * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t) |
| 17 | * { |
| 18 | * if (/$regex/) echo "matched: $car\n"; |
| 19 | * } |
| 20 | * |
| 21 | * Glob implements glob(3) style matching that can be used to match |
| 22 | * against text, rather than fetching names from a filesystem. |
| 23 | * |
| 24 | * Based on the Perl Text::Glob module. |
| 25 | * |
| 26 | * @author Fabien Potencier <fabien@symfony.com> PHP port |
| 27 | * @author Richard Clamp <richardc@unixbeard.net> Perl version |
| 28 | * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com> |
| 29 | * @copyright 2002 Richard Clamp <richardc@unixbeard.net> |
| 30 | */ |
| 31 | class Glob |
| 32 | { |
| 33 | /** |
| 34 | * Returns a regexp which is the equivalent of the glob pattern. |
| 35 | * |
| 36 | * @param string $glob The glob pattern |
| 37 | * @param bool $strictLeadingDot |
| 38 | * @param bool $strictWildcardSlash |
| 39 | * @param string $delimiter Optional delimiter |
| 40 | * |
| 41 | * @return string regex The regexp |
| 42 | */ |
| 43 | public static function toRegex($glob, $strictLeadingDot = \true, $strictWildcardSlash = \true, $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) { |
| 53 | if ($strictLeadingDot && '.' !== $car) { |
| 54 | $regex .= '(?=[^\\.])'; |
| 55 | } |
| 56 | |
| 57 | $firstByte = \false; |
| 58 | } |
| 59 | |
| 60 | if ('/' === $car) { |
| 61 | $firstByte = \true; |
| 62 | } |
| 63 | |
| 64 | if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) { |
| 65 | $regex .= "\\{$car}"; |
| 66 | } elseif ('*' === $car) { |
| 67 | $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*'); |
| 68 | } elseif ('?' === $car) { |
| 69 | $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.'); |
| 70 | } elseif ('{' === $car) { |
| 71 | $regex .= $escaping ? '\\{' : '('; |
| 72 | if (!$escaping) { |
| 73 | ++$inCurlies; |
| 74 | } |
| 75 | } elseif ('}' === $car && $inCurlies) { |
| 76 | $regex .= $escaping ? '}' : ')'; |
| 77 | if (!$escaping) { |
| 78 | --$inCurlies; |
| 79 | } |
| 80 | } elseif (',' === $car && $inCurlies) { |
| 81 | $regex .= $escaping ? ',' : '|'; |
| 82 | } elseif ('\\' === $car) { |
| 83 | if ($escaping) { |
| 84 | $regex .= '\\\\'; |
| 85 | $escaping = \false; |
| 86 | } else { |
| 87 | $escaping = \true; |
| 88 | } |
| 89 | continue; |
| 90 | } else { |
| 91 | $regex .= $car; |
| 92 | } |
| 93 | $escaping = \false; |
| 94 | } |
| 95 | |
| 96 | return $delimiter . '^' . $regex . '$' . $delimiter; |
| 97 | } |
| 98 | } |
| 99 |