Skip to content

Commit 5a1519e

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

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-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, 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

Lines changed: 5 additions & 0 deletions
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
}
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', priority: 2)]
10+
public function important()
11+
{
12+
13+
}
14+
15+
#[Route(path: '/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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,24 @@ 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+
$r = new \ReflectionClass($routes);
528+
$p = $r->getProperty('priorities');
529+
$p->setAccessible(true);
530+
$this->assertEquals([2, 2, 1], array_values($p->getValue($routes)));
531+
}
512532
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy