diff --git a/Loader/Configurator/Traits/HostTrait.php b/Loader/Configurator/Traits/HostTrait.php index 0e269cb1..e584f356 100644 --- a/Loader/Configurator/Traits/HostTrait.php +++ b/Loader/Configurator/Traits/HostTrait.php @@ -28,6 +28,7 @@ final protected function addHost(RouteCollection $routes, string|array $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; @@ -35,14 +36,14 @@ final protected function addHost(RouteCollection $routes, string|array $hosts): $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); } } } diff --git a/Tests/Fixtures/locale_and_host/priorized-host.yml b/Tests/Fixtures/locale_and_host/priorized-host.yml new file mode 100644 index 00000000..902b19e2 --- /dev/null +++ b/Tests/Fixtures/locale_and_host/priorized-host.yml @@ -0,0 +1,6 @@ +controllers: + resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController + type: attribute + host: + cs: www.domain.cs + en: www.domain.com diff --git a/Tests/Loader/YamlFileLoaderTest.php b/Tests/Loader/YamlFileLoaderTest.php index 3408a4b3..5c82e9b5 100644 --- a/Tests/Loader/YamlFileLoaderTest.php +++ b/Tests/Loader/YamlFileLoaderTest.php @@ -489,6 +489,29 @@ protected function configureRoute(Route $route, \ReflectionClass $class, \Reflec $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() extends AttributeClassLoader { + 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')); + } + /** * @dataProvider providePsr4ConfigFiles */