Skip to content

Commit 896ca07

Browse files
[Routing] Fix host vars extraction
1 parent a5c759c commit 896ca07

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,10 @@ private function compileRoute(Route $route, string $name, bool $supportsRedirect
626626
// optimize parameters array
627627
if ($matches || $hostMatches) {
628628
$vars = array();
629-
if ($hostMatches) {
629+
if ($hostMatches && $checkHost) {
630630
$vars[] = '$hostMatches';
631631
}
632-
if ($matches) {
632+
if ($matches || ($hostMatches && !$checkHost)) {
633633
$vars[] = '$matches';
634634
}
635635
$vars[] = "array('_route' => '$name')";
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
4+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
5+
use Symfony\Component\Routing\RequestContext;
6+
7+
/**
8+
* This class has been auto-generated
9+
* by the Symfony Routing Component.
10+
*/
11+
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
12+
{
13+
public function __construct(RequestContext $context)
14+
{
15+
$this->context = $context;
16+
}
17+
18+
public function match($rawPathinfo)
19+
{
20+
$allow = array();
21+
$pathinfo = rawurldecode($rawPathinfo);
22+
$trimmedPathinfo = rtrim($pathinfo, '/');
23+
$context = $this->context;
24+
$requestMethod = $canonicalMethod = $context->getMethod();
25+
$host = strtolower($context->getHost());
26+
27+
if ('HEAD' === $requestMethod) {
28+
$canonicalMethod = 'GET';
29+
}
30+
31+
$matchedPathinfo = $host.$pathinfo;
32+
$regexList = array(
33+
0 => '{^(?'
34+
.'|(?i:([^\\.]++)\\.exampple\\.com)(?'
35+
.'|/abc([^/]++)(?'
36+
.'|(*:54)'
37+
.')'
38+
.')'
39+
.')$}sD',
40+
);
41+
42+
foreach ($regexList as $offset => $regex) {
43+
while (preg_match($regex, $matchedPathinfo, $matches)) {
44+
switch ($m = (int) $matches['MARK']) {
45+
case 54:
46+
$matches = array('foo' => $matches[1] ?? null, 'foo' => $matches[2] ?? null);
47+
48+
// r1
49+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'r1')), array());
50+
51+
// r2
52+
return $this->mergeDefaults(array_replace($matches, array('_route' => 'r2')), array());
53+
54+
break;
55+
}
56+
57+
if (54 === $m) {
58+
break;
59+
}
60+
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));
61+
$offset += strlen($m);
62+
}
63+
}
64+
65+
throw $allow ? new MethodNotAllowedException(array_keys($allow)) : new ResourceNotFoundException();
66+
}
67+
}

Tests/Matcher/Dumper/PhpMatcherDumperTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ public function getRouteCollections()
467467
$suffixCollection->add('r100', new Route('abc{foo}/100'));
468468
$suffixCollection->add('r200', new Route('abc{foo}/200'));
469469

470+
/* test case 13 */
471+
$hostCollection = new RouteCollection();
472+
$hostCollection->add('r1', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
473+
$hostCollection->add('r2', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
474+
470475
return array(
471476
array(new RouteCollection(), 'url_matcher0.php', array()),
472477
array($collection, 'url_matcher1.php', array()),
@@ -481,6 +486,7 @@ public function getRouteCollections()
481486
array($chunkedCollection, 'url_matcher10.php', array()),
482487
array($demoCollection, 'url_matcher11.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
483488
array($suffixCollection, 'url_matcher12.php', array()),
489+
array($hostCollection, 'url_matcher13.php', array()),
484490
);
485491
}
486492

0 commit comments

Comments
 (0)