Skip to content

Commit 31f6444

Browse files
committed
fix: generate valid sample URIs for custom route placeholders
Fixes #9804 SampleURIGenerator only had predefined sample values for built-in placeholders (any, segment, num, etc.). Custom placeholders added via addPlaceholder() fell back to '::unknown::' which does not match the regex pattern. This caused FilterFinder to throw PageNotFoundException when trying to route the sample URI, resulting in '<unknown>' being displayed for filters in 'php spark routes'. Solution: - Add generateSample() method that creates valid sample strings from regex patterns by extracting first valid characters from character classes and applying quantifiers - Use generateSample() as fallback when no predefined sample exists Ref: #9804
1 parent 2a1c348 commit 31f6444

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

‎system/Commands/Utilities/Routes/SampleURIGenerator.php‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,87 @@ public function get(string $routeKey): string
6262
}
6363

6464
foreach ($this->routes->getPlaceholders() as $placeholder => $regex) {
65-
$sample = $this->samples[$placeholder] ?? '::unknown::';
65+
$sample = $this->samples[$placeholder] ?? $this->generateSample($regex);
6666

6767
$sampleUri = str_replace('(' . $regex . ')', $sample, $sampleUri);
6868
}
6969

7070
// auto route
7171
return str_replace('[/...]', '/1/2/3/4/5', $sampleUri);
7272
}
73+
74+
/**
75+
* Generates a valid sample string from a regex pattern for unknown placeholders.
76+
*
77+
* Handles common regex constructs: character classes [a-z], quantifiers {n}, +, *, ?,
78+
* dot ., and escaped characters. Uses the first valid character from each
79+
* character class and repeats according to quantifiers.
80+
*/
81+
private function generateSample(string $regex): string
82+
{
83+
$result = '';
84+
$len = strlen($regex);
85+
$i = 0;
86+
$lastChar = 'a';
87+
88+
while ($i < $len) {
89+
$c = $regex[$i];
90+
$repeat = 1;
91+
92+
if ($c === '[') {
93+
$end = strpos($regex, ']', $i);
94+
if ($end === false) {
95+
break;
96+
}
97+
$class = substr($regex, $i + 1, $end - $i - 1);
98+
if (isset($class[0]) && $class[0] === '^') {
99+
$lastChar = 'a';
100+
} else {
101+
$lastChar = ($class[0] ?? '') === '\\' ? ($class[1] ?? 'a') : ($class[0] ?? 'a');
102+
}
103+
$i = $end + 1;
104+
} elseif ($c === '.') {
105+
$lastChar = 'a';
106+
$i++;
107+
} elseif ($c === '\\' && $i + 1 < $len) {
108+
$lastChar = $regex[$i + 1];
109+
$i += 2;
110+
} else {
111+
$i++;
112+
113+
continue;
114+
}
115+
116+
// Handle quantifiers: +, *, ?, {n}, {n,m}
117+
if ($i < $len) {
118+
$next = $regex[$i];
119+
120+
if ($next === '+') {
121+
$repeat = 1;
122+
$i++;
123+
} elseif ($next === '*') {
124+
$repeat = 0;
125+
$i++;
126+
} elseif ($next === '?') {
127+
$repeat = 0;
128+
$i++;
129+
} elseif ($next === '{') {
130+
$end = strpos($regex, '}', $i);
131+
132+
if ($end !== false) {
133+
$q = substr($regex, $i + 1, $end - $i - 1);
134+
// Use the minimum value from {n} or {n,m}
135+
$commaPos = strpos($q, ',');
136+
$min = $commaPos !== false ? (int) substr($q, 0, $commaPos) : (int) $q;
137+
$repeat = max($min, 1);
138+
$i = $end + 1;
139+
}
140+
}
141+
}
142+
143+
$result .= str_repeat($lastChar, $repeat);
144+
}
145+
146+
return $result !== '' ? $result : 'a';
147+
}
73148
}

0 commit comments

Comments
 (0)