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

Skip to content

Commit d9d6695

Browse files
author
pritasil
committed
[Routing] Fixed priority getting lost when defining prefix array
1 parent a63f545 commit d9d6695

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ final protected function addPrefix(RouteCollection $routes, $prefix, bool $trail
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,9 @@ public function getAlias(string $name): ?Alias
395395
{
396396
return $this->aliases[$name] ?? null;
397397
}
398+
399+
public function getPriority(string $name): ?int
400+
{
401+
return $this->priorities[$name] ?? null;
402+
}
398403
}
Lines changed: 20 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\Loader\LoaderResolver;
1617
use Symfony\Component\Config\Resource\FileResource;
18+
use Symfony\Component\Routing\Loader\AttributeClassLoader;
1719
use Symfony\Component\Routing\Loader\YamlFileLoader;
1820
use Symfony\Component\Routing\Route;
1921
use Symfony\Component\Routing\RouteCollection;
@@ -458,4 +460,23 @@ public function testImportingAliases()
458460

459461
$this->assertEquals($expectedRoutes('yaml'), $routes);
460462
}
463+
464+
public function testPriorityWithPrefix()
465+
{
466+
new LoaderResolver([
467+
$loader = new YamlFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures/localized')),
468+
new class() extends AttributeClassLoader {
469+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot): void
470+
{
471+
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
472+
}
473+
},
474+
]);
475+
476+
$routes = $loader->load('localized-prefix.yml');
477+
478+
$this->assertEquals(2, $routes->getPriority('important.cs'));
479+
$this->assertEquals(2, $routes->getPriority('important.en'));
480+
$this->assertEquals(1, $routes->getPriority('also_important'));
481+
}
461482
}

0 commit comments

Comments
 (0)
0