| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Routing\Loader\Configurator\Traits;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Routing\Route;
|
|
|
15 |
use Symfony\Component\Routing\RouteCollection;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @internal
|
|
|
19 |
*
|
|
|
20 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
21 |
* @author Jules Pietri <jules@heahprod.com>
|
|
|
22 |
*/
|
|
|
23 |
trait LocalizedRouteTrait
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* Creates one or many routes.
|
|
|
27 |
*
|
|
|
28 |
* @param string|array $path the path, or the localized paths of the route
|
|
|
29 |
*/
|
|
|
30 |
final protected function createLocalizedRoute(RouteCollection $collection, string $name, string|array $path, string $namePrefix = '', array $prefixes = null): RouteCollection
|
|
|
31 |
{
|
|
|
32 |
$paths = [];
|
|
|
33 |
|
|
|
34 |
$routes = new RouteCollection();
|
|
|
35 |
|
|
|
36 |
if (\is_array($path)) {
|
|
|
37 |
if (null === $prefixes) {
|
|
|
38 |
$paths = $path;
|
|
|
39 |
} elseif ($missing = array_diff_key($prefixes, $path)) {
|
|
|
40 |
throw new \LogicException(sprintf('Route "%s" is missing routes for locale(s) "%s".', $name, implode('", "', array_keys($missing))));
|
|
|
41 |
} else {
|
|
|
42 |
foreach ($path as $locale => $localePath) {
|
|
|
43 |
if (!isset($prefixes[$locale])) {
|
|
|
44 |
throw new \LogicException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$paths[$locale] = $prefixes[$locale].$localePath;
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
} elseif (null !== $prefixes) {
|
|
|
51 |
foreach ($prefixes as $locale => $prefix) {
|
|
|
52 |
$paths[$locale] = $prefix.$path;
|
|
|
53 |
}
|
|
|
54 |
} else {
|
|
|
55 |
$routes->add($namePrefix.$name, $route = $this->createRoute($path));
|
|
|
56 |
$collection->add($namePrefix.$name, $route);
|
|
|
57 |
|
|
|
58 |
return $routes;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
foreach ($paths as $locale => $path) {
|
|
|
62 |
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
|
|
|
63 |
$collection->add($namePrefix.$name.'.'.$locale, $route);
|
|
|
64 |
$route->setDefault('_locale', $locale);
|
|
|
65 |
$route->setRequirement('_locale', preg_quote($locale));
|
|
|
66 |
$route->setDefault('_canonical_route', $namePrefix.$name);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return $routes;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function createRoute(string $path): Route
|
|
|
73 |
{
|
|
|
74 |
return new Route($path);
|
|
|
75 |
}
|
|
|
76 |
}
|