From ba411755aaf19f0dfb7bf06311a46b332d308e19 Mon Sep 17 00:00:00 2001 From: pritasil Date: Tue, 5 Dec 2023 22:01:21 +0100 Subject: [PATCH] [Routing] Fixed priority getting lost when defining prefix array --- .../Configurator/Traits/PrefixTrait.php | 5 ++-- .../Component/Routing/RouteCollection.php | 5 ++++ .../RouteWithPriorityController.php | 22 ++++++++++++++++ .../Fixtures/localized/localized-prefix.yml | 6 +++++ .../Tests/Loader/YamlFileLoaderTest.php | 26 +++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/RouteWithPriorityController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/localized/localized-prefix.yml diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php index 27053bcaf546b..0b19573ec40b1 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php @@ -29,6 +29,7 @@ final protected function addPrefix(RouteCollection $routes, $prefix, bool $trail } foreach ($routes->all() as $name => $route) { if (null === $locale = $route->getDefault('_locale')) { + $priority = $routes->getPriority($name) ?? 0; $routes->remove($name); foreach ($prefix as $locale => $localePrefix) { $localizedRoute = clone $route; @@ -36,13 +37,13 @@ final protected function addPrefix(RouteCollection $routes, $prefix, bool $trail $localizedRoute->setRequirement('_locale', preg_quote($locale)); $localizedRoute->setDefault('_canonical_route', $name); $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath())); - $routes->add($name.'.'.$locale, $localizedRoute); + $routes->add($name.'.'.$locale, $localizedRoute, $priority); } } elseif (!isset($prefix[$locale])) { throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale)); } else { $route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath())); - $routes->add($name, $route); + $routes->add($name, $route, $routes->getPriority($name) ?? 0); } } diff --git a/src/Symfony/Component/Routing/RouteCollection.php b/src/Symfony/Component/Routing/RouteCollection.php index b1219f844b7c0..95faead6e8582 100644 --- a/src/Symfony/Component/Routing/RouteCollection.php +++ b/src/Symfony/Component/Routing/RouteCollection.php @@ -395,4 +395,9 @@ public function getAlias(string $name): ?Alias { return $this->aliases[$name] ?? null; } + + public function getPriority(string $name): ?int + { + return $this->priorities[$name] ?? null; + } } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/RouteWithPriorityController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/RouteWithPriorityController.php new file mode 100644 index 0000000000000..1625589cee49d --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/RouteWithPriorityController.php @@ -0,0 +1,22 @@ +assertEquals($expectedRoutes('yaml'), $routes); } + + public function testPriorityWithPrefix() + { + new LoaderResolver([ + $loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')), + 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('localized-prefix.yml'); + + $this->assertSame(2, $routes->getPriority('important.cs')); + $this->assertSame(2, $routes->getPriority('important.en')); + $this->assertSame(1, $routes->getPriority('also_important')); + } }