Skip to content

[Routing] Fix: lost priority when defining hosts in configuration #58842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ final protected function addHost(RouteCollection $routes, $hosts)

foreach ($routes->all() as $name => $route) {
if (null === $locale = $route->getDefault('_locale')) {
$priority = $routes->getPriority($name) ?? 0;
$routes->remove($name);
foreach ($hosts as $locale => $host) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setRequirement('_locale', preg_quote($locale));
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setHost($host);
$routes->add($name.'.'.$locale, $localizedRoute);
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
}
} elseif (!isset($hosts[$locale])) {
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
} else {
$route->setHost($hosts[$locale]);
$route->setRequirement('_locale', preg_quote($locale));
$routes->add($name, $route);
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
controllers:
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
type: annotation
host:
cs: www.domain.cs
en: www.domain.com
23 changes: 23 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,27 @@ protected function configureRoute(
$this->assertSame(2, $routes->getPriority('important.en'));
$this->assertSame(1, $routes->getPriority('also_important'));
}

public function testPriorityWithHost()
{
new LoaderResolver([
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/locale_and_host')),
new class(new AnnotationReader(), null) extends AnnotationClassLoader {
protected function configureRoute(
Route $route,
\ReflectionClass $class,
\ReflectionMethod $method,
object $annot
): void {
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
},
]);

$routes = $loader->load('priorized-host.yml');

$this->assertSame(2, $routes->getPriority('important.cs'));
$this->assertSame(2, $routes->getPriority('important.en'));
$this->assertSame(1, $routes->getPriority('also_important'));
}
}
Loading