Debian Codegraph @

vendor/symfony/finder/Glob.php

raw
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 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 if ($unanchored = str_starts_with($glob, '**/')) {
48 $glob = '/'.$glob;
49 }
50 $sizeGlob = \strlen($glob);
51 for ($i = 0; $i < $sizeGlob; ++$i) {
52 $car = $glob[$i];
53 if ($firstByte && $strictLeadingDot && '.' !== $car) {
54 $regex .= '(?=[^\.])';
55 }
56
57 $firstByte = '/' === $car;
58
59 if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
60 $car = '[^/]++/';
61 if (!isset($glob[$i + 3])) {
62 $car .= '?';
63 }
64
65 if ($strictLeadingDot) {
66 $car = '(?=[^\.])'.$car;
67 }
68
69 $car = '/(?:'.$car.')*';
70 $i += 2 + isset($glob[$i + 3]);
71
72 if ('/' === $delimiter) {
73 $car = str_replace('/', '\\/', $car);
74 }
75 }
76
77 if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
78 $regex .= "\\$car";
79 } elseif ('*' === $car) {
80 $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
81 } elseif ('?' === $car) {
82 $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
83 } elseif ('{' === $car) {
84 $regex .= $escaping ? '\\{' : '(';
85 if (!$escaping) {
86 ++$inCurlies;
87 }
88 } elseif ('}' === $car && $inCurlies) {
89 $regex .= $escaping ? '}' : ')';
90 if (!$escaping) {
91 --$inCurlies;
92 }
93 } elseif (',' === $car && $inCurlies) {
94 $regex .= $escaping ? ',' : '|';
95 } elseif ('\\' === $car) {
96 if ($escaping) {
97 $regex .= '\\\\';
98 $escaping = false;
99 } else {
100 $escaping = true;
101 }
102
103 continue;
104 } else {
105 $regex .= $car;
106 }
107 $escaping = false;
108 }
109
110 if ($unanchored) {
111 $regex = substr_replace($regex, '?', 1 + ('/' === $delimiter) + ($strictLeadingDot ? \strlen('(?=[^\.])') : 0), 0);
112 }
113
114 return $delimiter.'^'.$regex.'$'.$delimiter;
115 }
116 }