Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ 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;
$localizedRoute->setDefault('_locale', $locale);
$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);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;

use Symfony\Component\Routing\Annotation\Route;

class RouteWithPriorityController
{
/**
* @Route("/important", name="important", priority=2)
*/
public function important()
{
}

/**
* @Route("/also-important", name="also_important", priority=1, defaults={"_locale": "cs"})
*/
public function alsoImportant()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
important_controllers:
resource: Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\RouteWithPriorityController
type: annotation
prefix:
cs: /cs
en: /en
26 changes: 26 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Symfony\Component\Routing\Tests\Loader;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -458,4 +461,27 @@ public function testImportingAliases()

$this->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'));
}
}