8000 bug #58842 [Routing] Fix: lost priority when defining hosts in config… · symfony/symfony@16087db · GitHub
[go: up one dir, main page]

Skip to content

Commit 16087db

Browse files
bug #58842 [Routing] Fix: lost priority when defining hosts in configuration (BeBlood)
This PR was merged into the 5.4 branch. Discussion ---------- [Routing] Fix: lost priority when defining hosts in configuration | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | #58843 | License | MIT When host are configured in routing, the original route is removed from the collection and then re-added. During this process, the route's priority is lost as it's not preserved during the removal and re-addition. Bug has similarities with #52912 (resolved for prefix) Commits ------- 437e6ad [Routing] Fix: lost priority when defining hosts in configuration
2 parents 90e3135 + 437e6ad commit 16087db

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/S 8000 ymfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ final protected function addHost(RouteCollection $routes, $hosts)
2828

2929
foreach ($routes->all() as $name => $route) {
3030
if (null === $locale = $route->getDefault('_locale')) {
31+
$priority = $routes->getPriority($name) ?? 0;
3132
$routes->remove($name);
3233
foreach ($hosts as $locale => $host) {
3334
$localizedRoute = clone $route;
3435
$localizedRoute->setDefault('_locale', $locale);
3536
$localizedRoute->setRequirement('_locale', preg_quote($locale));
3637
$localizedRoute->setDefault('_canonical_route', $name);
3738
$localizedRoute->setHost($host);
38-
$routes->add($name.'.'.$locale, $localizedRoute);
39+
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
3940
}
4041
} elseif (!isset($hosts[$locale])) {
4142
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
4243
} else {
4344
$route->setHost($hosts[$locale]);
4445
$route->setRequirement('_locale', preg_quote($locale));
45-
$routes->add($name, $route);
46+
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
4647
}
4748
}
4849
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
controllers:
2+
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
3+
type: annotation
4+
host:
5+
cs: www.domain.cs
6+
en: www.domain.com

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,27 @@ protected function configureRoute(
484484
$this->assertSame(2, $routes->getPriority('important.en'));
485485
$this->assertSame(1, $routes->getPriority('also_important'));
486486
}
487+
488+
public function testPriorityWithHost()
489+
{
490+
new LoaderResolver([
491+
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/locale_and_host')),
492+
new class(new AnnotationReader(), null) extends AnnotationClassLoader {
493+
protected function configureRoute(
494+
Route $route,
495+
\ReflectionClass $class,
496+
\ReflectionMethod $method,
497+
object $annot
498+
): void {
499+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
500+
}
501+
},
502+
]);
503+
504+
$routes = $loader->load('priorized-host.yml');
505+
506+
$this->assertSame(2, $routes->getPriority('important.cs'));
507+
$this->assertSame(2, $routes->getPriority('important.en'));
508+
$this->assertSame(1, $routes->getPriority('also_important'));
509+
}
487510
}

0 commit comments

Comments
 (0)
0