8000 [Routing] Fixed priority getting lost when defining prefix array · symfony/symfony@347a471 · GitHub
[go: up one dir, main page]

Skip to content

Commit 347a471

Browse files
author
pritasil
committed
[Routing] Fixed priority getting lost when defining prefix array
1 parent a314b65 commit 347a471

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/Symfony/Component/Routing/Loader/Configurator/Traits/PrefixTrait.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ final protected function addPrefix(RouteCollection $routes, string|array $prefix
2929
}
3030
foreach ($routes->all() as $name => $route) {
3131
if (null === $locale = $route->getDefault('_locale')) {
32+
$priority = $routes->getPriority($name) ?? 0;
3233
$routes->remove($name);
3334
foreach ($prefix as $locale => $localePrefix) {
3435
$localizedRoute = clone $route;
3536
$localizedRoute->setDefault('_locale', $locale);
3637
$localizedRoute->setRequirement('_locale', preg_quote($locale));
3738
$localizedRoute->setDefault('_canonical_route', $name);
3839
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
39-
$routes->add($name.'.'.$locale, $localizedRoute);
40+
$routes->add($name.'.'.$locale, $localizedRoute, $priority);
4041
}
4142
} elseif (!isset($prefix[$locale])) {
4243
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
4344
} else {
4445
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
45-
$routes->add($name, $route);
46+
$routes->add($name, $route, $routes->getPriority($name) ?? 0);
4647
}
4748
}
4849

src/Symfony/Component/Routing/RouteCollection.php

+5
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,9 @@ public function getAlias(string $name): ?Alias
407407
{
408408
return $this->aliases[$name] ?? null;
409409
}
410+
411+
public function getPriority(string $name): ?int
412+
{
413+
return $this->priorities[$name] ?? null;
414+
}
410415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
4+
5+
use Symfony\Component\Routing\Attribute\Route;
6+
7+
class RouteWithPriorityController
8+
{
9+
#[Route(path: '/important', name: 'important', priority: 2)]
10+
public function important()
11+
{
12+
13+
}
14+
15+
#[Route(path: '/also-important', name: 'also_important', defaults: ['_locale' => 'cs'], priority: 1)]
16+
public function alsoImportant()
17+
{
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
important_controllers:
2+
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
3+
type: attribute
4+
prefix:
5+
cs: /cs
6+
en: /en

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,23 @@ protected function configureRoute(Route $route, \ReflectionClass $class, \Reflec
509509
$this->assertSame('/my-prefix/my/route', $route->getPath());
510510
$this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller'));
511511
}
512+
513+
public function testPriorityWithPrefix()
514+
{
515+
new LoaderResolver([
516+
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')),
517+
new class() extends AttributeClassLoader {
518+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
519+
{
520+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
521+
}
522+
},
523+
]);
524+
525+
$routes = $loader->load('localized-prefix.yml');
526+
527+
$this->assertEquals(2, $routes->getPriority('important.cs'));
528+
$this->assertEquals(2, $routes->getPriority('important.en'));
529+
$this->assertEquals(1, $routes->getPriority('also_important'));
530+
}
512531
}

0 commit comments

Comments
 (0)
0