Skip to content

Commit fc75029

Browse files
committed
fix: add sample parameter to addPlaceholder() for valid route URIs
Fixes #9804 When using custom placeholders with 'php spark routes', filters were shown as '<unknown>' because SampleURIGenerator could not produce valid sample URIs for unknown regex patterns. Solution: - Extend addPlaceholder() with optional $sample parameter that lets users specify a representative value for the placeholder - Add getPlaceholderSamples() to RouteCollection - Update SampleURIGenerator to use user-provided samples first, then built-in samples, then the placeholder name as fallback - Remove generateSample() regex parser (unreliable for arbitrary patterns) Usage: $routes->addPlaceholder('code', '[A-Z]{3}[0-9]+', 'ABC123'); Ref: #9804
1 parent 2a1c348 commit fc75029

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

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

6464
foreach ($this->routes->getPlaceholders() as $placeholder => $regex) {
65-
$sample = $this->samples[$placeholder] ?? '::unknown::';
65+
// Priority: 1) user-provided sample, 2) built-in sample, 3) placeholder name
66+
$sample = $this->routes->getPlaceholderSamples()[$placeholder]
67+
?? $this->samples[$placeholder]
68+
?? $placeholder;
6669

6770
$sampleUri = str_replace('(' . $regex . ')', $sample, $sampleUri);
6871
}

‎system/Router/RouteCollection.php‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ class RouteCollection implements RouteCollectionInterface
114114
'hash' => '[^/]+',
115115
];
116116

117+
/**
118+
* Sample values for custom placeholders, used by SampleURIGenerator
119+
* to produce valid sample URIs for 'php spark routes'.
120+
*
121+
* @var array<string, string>
122+
*/
123+
protected $placeholderSamples = [];
124+
117125
/**
118126
* An array of all routes and their mappings.
119127
*
@@ -384,16 +392,29 @@ protected function discoverRoutes()
384392
* You can pass an associative array as $placeholder, and have
385393
* multiple placeholders added at once.
386394
*
395+
* The $sample parameter provides a representative value for the
396+
* placeholder, used by 'php spark routes' to generate valid sample URIs.
397+
* When omitted, the system attempts to infer a sample from the pattern.
398+
*
387399
* @param array|string $placeholder
388400
*/
389-
public function addPlaceholder($placeholder, ?string $pattern = null): RouteCollectionInterface
401+
public function addPlaceholder($placeholder, ?string $pattern = null, ?string $sample = null): RouteCollectionInterface
390402
{
391403
if (! is_array($placeholder)) {
392404
$placeholder = [$placeholder => $pattern];
393405
}
394406

395407
$this->placeholders = array_merge($this->placeholders, $placeholder);
396408

409+
if ($sample !== null) {
410+
$name = is_string($placeholder) ? [$placeholder => $sample] : [];
411+
412+
// When array format is used with a single key, extract the name
413+
foreach ($placeholder as $name => $pat) {
414+
$this->placeholderSamples[$name] = $sample;
415+
}
416+
}
417+
397418
return $this;
398419
}
399420

@@ -409,6 +430,18 @@ public function getPlaceholders(): array
409430
return $this->placeholders;
410431
}
411432

433+
/**
434+
* Returns sample values for placeholders, used by SampleURIGenerator.
435+
*
436+
* @return array<string, string>
437+
*
438+
* @internal
439+
*/
440+
public function getPlaceholderSamples(): array
441+
{
442+
return $this->placeholderSamples;
443+
}
444+
412445
/**
413446
* Sets the default namespace to use for Controllers when no other
414447
* namespace has been specified.

‎system/Router/RouteCollectionInterface.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ public function add(string $from, $to, ?array $options = null);
4444
* You can pass an associative array as $placeholder, and have
4545
* multiple placeholders added at once.
4646
*
47+
* The $sample parameter provides a representative value for the
48+
* placeholder, used by 'php spark routes' to generate valid sample URIs.
49+
*
4750
* @param array|string $placeholder
4851
* @param string|null $pattern The regex pattern
52+
* @param string|null $sample A sample value that matches the pattern
4953
*
5054
* @return RouteCollectionInterface
5155
*/
52-
public function addPlaceholder($placeholder, ?string $pattern = null);
56+
public function addPlaceholder($placeholder, ?string $pattern = null, ?string $sample = null);
5357

5458
/**
5559
* Sets the default namespace to use for Controllers when no other

0 commit comments

Comments
 (0)