Skip to content

Commit fa07842

Browse files
committed
check if overwritten builtin sample still matches its regex
1 parent a17baae commit fa07842

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ private function resolveSample(string $placeholder, string $regex): string
9797
return $this->resolvedCache[$placeholder];
9898
}
9999

100-
$sample = $this->configuredSample($placeholder, $regex)
101-
?? $this->samples[$placeholder]
100+
$sample = $this->matchingSample($this->config->placeholderSamples[$placeholder] ?? null, $regex)
101+
?? $this->matchingSample($this->samples[$placeholder] ?? null, $regex)
102102
?? $this->sampleGenerator->generate($regex)
103103
?? self::UNKNOWN_SAMPLE;
104104

@@ -108,14 +108,14 @@ private function resolveSample(string $placeholder, string $regex): string
108108
}
109109

110110
/**
111-
* Returns the configured sample for the placeholder when one is set and it
112-
* matches the placeholder regex, otherwise ``null`` so resolution falls
113-
* through to the built-in, auto-generated, or unknown sample.
111+
* Returns the given sample when it is set and matches the placeholder
112+
* regex, otherwise ``null`` so resolution falls through to the next source.
113+
* Guards both the configured and built-in samples, since a built-in
114+
* placeholder name may be redefined with a different regex via
115+
* ``RouteCollection::addPlaceholder()``.
114116
*/
115-
private function configuredSample(string $placeholder, string $regex): ?string
117+
private function matchingSample(?string $sample, string $regex): ?string
116118
{
117-
$sample = $this->config->placeholderSamples[$placeholder] ?? null;
118-
119119
if ($sample === null) {
120120
return null;
121121
}

‎tests/system/Commands/Utilities/Routes/SampleURIGeneratorTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,18 @@ public function testEmptyConfiguredSampleIsIgnored(): void
190190

191191
$this->assertSame('test/AAA0', $uri);
192192
}
193+
194+
public function testOverriddenBuiltInPlaceholderIgnoresStaleSample(): void
195+
{
196+
$routes = service('routes');
197+
// Redefining a built-in name with a new regex must not reuse the stale
198+
// built-in sample (123), which no longer matches.
199+
$routes->addPlaceholder('num', '[A-Z]+');
200+
201+
$generator = new SampleURIGenerator();
202+
203+
$uri = $generator->get('shop/product/([A-Z]+)');
204+
205+
$this->assertSame('shop/product/A', $uri);
206+
}
193207
}

0 commit comments

Comments
 (0)