-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutomataMatcher.php
208 lines (186 loc) · 5.48 KB
/
AutomataMatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace Kellegous\CodeOwners;
use Kellegous\CodeOwners\AutomataMatcher\State;
use Kellegous\CodeOwners\AutomataMatcher\Token;
/**
* A RuleMatcher that combines all the patterns into a single automata.
*/
final class AutomataMatcher implements RuleMatcher
{
/**
* This is the start state for the automata.
*
* @var State
*/
private State $start;
/**
* The full collection of rules from the Owners instance. The NFA keeps the index of
* rules instead of a reference to the rules themselves since the index provides a straight-forward
* way to honor the last-match-wins rule (the largest of the matching indexes wins).
*
* @var Rule[]
*/
private array $rules;
/**
* @param Rule[] $rules
* @param State $start
*/
private function __construct(
array $rules,
State $start
) {
$this->rules = $rules;
$this->start = $start;
}
/**
* Builds a new AutomataMatcher from the given rules.
*
* @param iterable<Rule> $rules
* @return self
*/
public static function build(iterable $rules): self
{
$start = new State();
$as_array = [];
foreach ($rules as $index => $rule) {
$start->addTokens(
self::parsePattern($rule->getPattern()),
$index
);
$as_array[] = $rule;
}
return new self($as_array, $start);
}
/**
* @param Pattern $pattern
* @return Token[]
*/
private static function parsePattern(Pattern $pattern): array
{
$tokens = [];
$pattern = $pattern->toString();
if (!self::isAbsolute($pattern) && !str_starts_with($pattern, '**/')) {
$pattern = '**/' . $pattern;
}
if (str_ends_with($pattern, '/')) {
$pattern .= '**';
} elseif (!str_ends_with($pattern, '/**') && !str_ends_with($pattern, '/*')) {
$pattern .= '/***';
}
$segments = explode('/', trim($pattern, '/'));
foreach ($segments as $i => $segment) {
if ($segment === '*' || $segment === '**') {
$tokens[] = new Token($segment, '#\A.*\Z#');
continue;
}
$tokens[] = self::parseToken($segment);
}
return $tokens;
}
/**
* Strangely, a pattern is absolute not only if it starts with a slash,
* but also if it contains a wildcard.
*
* @param string $pattern
*
* @return bool
*/
private static function isAbsolute(string $pattern): bool
{
$ix = strpos($pattern, '/');
if ($ix === false) {
return false;
}
if ($ix === 0) {
return true;
}
for ($i = $ix, $n = strlen($pattern); $i < $n; $i++) {
if ($pattern[$i] === '*' || $pattern[$i] === '?') {
return true;
}
}
return false;
}
/**
* @param string $segment
* @return Token
*/
private static function parseToken(
string $segment
): Token {
$buffer = '\A';
$escape = false;
for ($i = 0, $n = strlen($segment); $i < $n; $i++) {
if ($escape) {
$escape = false;
$buffer .= preg_quote($segment[$i], '#');
continue;
}
switch ($segment[$i]) {
case '\\':
$escape = true;
break;
case '*':
$buffer .= '.*';
break;
case '?':
$buffer .= '.';
break;
default:
$buffer .= preg_quote($segment[$i], '#');
break;
}
}
$buffer .= '\Z';
return new Token($segment, "#$buffer#");
}
/**
* @inerhitDoc
* @Override
* @param string $path
* @return Rule|null
*/
public function match(string $path): ?Rule
{
if (str_starts_with($path, '/') || str_ends_with($path, '/')) {
throw new \InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
);
}
$path = explode('/', $path);
$index = $this->start->match($path);
return $index >= 0
? $this->rules[$index]
: null;
}
/**
* Used to return an internal representation of the automata for debugging purposes.
*
* @return array{nodes: array<string, int>, edges: array{from: string, to: string, label: string}[]}
*
* @internal
*/
public function getDebugInfo(): array
{
$patterns = [];
foreach ($this->rules as $rule) {
foreach (self::parsePattern($rule->getPattern()) as $token) {
$patterns[$token->getRegex()] = $token->getPattern();
}
}
$nodes = [];
$edges = [];
$this->start->getDebugInfo($nodes, $edges);
$edges = array_map(
function (array $edge) use ($patterns): array {
['from' => $from, 'to' => $to, 'label' => $label] = $edge;
if ($label !== '**') {
$label = $patterns[$label] ?? '??';
}
return ['from' => $from, 'to' => $to, 'label' => $label];
},
$edges
);
return ['nodes' => $nodes, 'edges' => $edges];
}
}