From dd08c19879a9b37ff14fd30dcbdf99a4cf045db1 Mon Sep 17 00:00:00 2001 From: Benjamin BOUDIER Date: Tue, 12 Nov 2024 19:20:21 +0100 Subject: [PATCH 1/3] [Routing] Fix: lost priority when defining hosts in configuration --- Loader/Configurator/Traits/HostTrait.php | 5 ++-- .../locale_and_host/priorized-host.yml | 6 +++++ Tests/Loader/YamlFileLoaderTest.php | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Tests/Fixtures/locale_and_host/priorized-host.yml diff --git a/Loader/Configurator/Traits/HostTrait.php b/Loader/Configurator/Traits/HostTrait.php index 54ae6566..168bbb4f 100644 --- a/Loader/Configurator/Traits/HostTrait.php +++ b/Loader/Configurator/Traits/HostTrait.php @@ -28,6 +28,7 @@ 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; @@ -35,14 +36,14 @@ final protected function addHost(RouteCollection $routes, $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..570cd021 --- /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: annotation + host: + cs: www.domain.cs + en: www.domain.com diff --git a/Tests/Loader/YamlFileLoaderTest.php b/Tests/Loader/YamlFileLoaderTest.php index 25a2b473..8e58ce9a 100644 --- a/Tests/Loader/YamlFileLoaderTest.php +++ b/Tests/Loader/YamlFileLoaderTest.php @@ -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')); + } } From 91e02e606b4b705c2f4fb42f7e7708b7923a3220 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 13 Nov 2024 16:31:34 +0100 Subject: [PATCH 2/3] fix merge --- Tests/Fixtures/locale_and_host/priorized-host.yml | 2 +- Tests/Loader/YamlFileLoaderTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Fixtures/locale_and_host/priorized-host.yml b/Tests/Fixtures/locale_and_host/priorized-host.yml index 570cd021..902b19e2 100644 --- a/Tests/Fixtures/locale_and_host/priorized-host.yml +++ b/Tests/Fixtures/locale_and_host/priorized-host.yml @@ -1,6 +1,6 @@ controllers: resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController - type: annotation + type: attribute host: cs: www.domain.cs en: www.domain.com diff --git a/Tests/Loader/YamlFileLoaderTest.php b/Tests/Loader/YamlFileLoaderTest.php index 9eff1cee..6573fd01 100644 --- a/Tests/Loader/YamlFileLoaderTest.php +++ b/Tests/Loader/YamlFileLoaderTest.php @@ -486,7 +486,7 @@ public function testPriorityWithHost() { new LoaderResolver([ $loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/locale_and_host')), - new class(new AnnotationReader(), null) extends AnnotationClassLoader { + new class() extends AttributeClassLoader { protected function configureRoute( Route $route, \ReflectionClass $class, From e10a2450fa957af6c448b9b93c9010a4e4c0725e Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Mon, 25 Nov 2024 01:26:19 +0100 Subject: [PATCH 3/3] CS: re-apply trailing_comma_in_multiline --- Tests/Loader/YamlFileLoaderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Loader/YamlFileLoaderTest.php b/Tests/Loader/YamlFileLoaderTest.php index 253583e9..5c82e9b5 100644 --- a/Tests/Loader/YamlFileLoaderTest.php +++ b/Tests/Loader/YamlFileLoaderTest.php @@ -498,7 +498,7 @@ protected function configureRoute( Route $route, \ReflectionClass $class, \ReflectionMethod $method, - object $annot + object $annot, ): void { $route->setDefault('_controller', $class->getName().'::'.$method->getName()); }