Skip to content

Commit 003b38b

Browse files
committed
Progressing
1 parent ffdaf58 commit 003b38b

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/Util/FileMatcher.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
private const T_SLASH = 'slash';
2929
private const T_BACKSLASH = 'backslash';
3030
private const T_CHAR = 'char';
31-
private const T_GLOBSTAR = 'globstar';
31+
private const T_GREEDY_GLOBSTAR = 'greedy_globstar';
3232
private const T_QUERY = 'query';
33+
private const T_GLOBSTAR = 'globstar';
3334

3435

3536
public static function match(string $path, FileMatcherPattern $pattern): bool
@@ -58,18 +59,22 @@ public static function toRegEx($glob, $flags = 0): string
5859
$type = $token[0];
5960
$regex .= match ($type) {
6061
// literal char
61-
self::T_CHAR => $token[1] ?? throw new Exception('Expected char token to have a value'),
62+
self::T_CHAR => preg_quote($token[1]),
6263

6364
// literal directory separator
6465
self::T_SLASH => '/',
6566
self::T_QUERY => '.',
6667

6768
// match any segment up until the next directory separator
6869
self::T_ASTERIX => '[^/]*',
69-
self::T_GLOBSTAR => '.*',
70+
self::T_GREEDY_GLOBSTAR => '.*',
71+
self::T_GLOBSTAR => '/([^/]+/)*',
7072
default => '',
7173
};
7274
}
75+
$regex .= '(/|$)';
76+
dump($tokens);
77+
dump($regex);
7378

7479
return '{^'.$regex.'}';
7580
}
@@ -133,9 +138,33 @@ private static function processTokens(array $tokens): array
133138
continue;
134139
}
135140

136-
if ($type === self::T_ASTERIX && ($tokens[$offset + 1] ?? null) === self::T_ASTERIX) {
137-
$offset++;
141+
// normal globstar
142+
if (
143+
$type === self::T_SLASH &&
144+
($tokens[$offset + 1][0] ?? null) === self::T_ASTERIX && ($tokens[$offset + 2][0] ?? null) === self::T_ASTERIX && ($tokens[$offset + 3][0] ?? null) === self::T_SLASH
145+
) {
138146
$resolved[] = [self::T_GLOBSTAR, '**'];
147+
148+
// we eat the two `*` in addition to the slash
149+
$offset += 3;
150+
continue;
151+
}
152+
153+
// greedy globstar (trailing?)
154+
if (
155+
$type === self::T_SLASH &&
156+
($tokens[$offset + 1][0] ?? null) === self::T_ASTERIX && ($tokens[$offset + 2][0] ?? null) === self::T_ASTERIX
157+
) {
158+
$resolved[] = [self::T_GREEDY_GLOBSTAR, '**'];
159+
160+
// we eat the two `*` in addition to the slash
161+
$offset += 2;
162+
continue;
163+
}
164+
165+
if ($type === self::T_ASTERIX && ($tokens[$offset + 1][0] ?? null) === self::T_ASTERIX) {
166+
$resolved[] = [self::T_CHAR, $char];
167+
$resolved[] = [self::T_CHAR, $char];
139168
continue;
140169
}
141170

0 commit comments

Comments
 (0)