Skip to content

Commit

Permalink
[Routing] Fix: lost priority when defining hosts in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
BeBlood committed Nov 12, 2024
1 parent 986597b commit dd08c19
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Loader/Configurator/Traits/HostTrait.php
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
6 changes: 6 additions & 0 deletions Tests/Fixtures/locale_and_host/priorized-host.yml
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 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'));
}
}

0 comments on commit dd08c19

Please sign in to comment.