Skip to content

Commit c5d9640

Browse files
[Routing] Handle "_canonical_route"
1 parent 2378358 commit c5d9640

File tree

11 files changed

+44
-18
lines changed

11 files changed

+44
-18
lines changed

Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
113113
?? $this->context->getParameter('_locale')
114114
?: $this->defaultLocale;
115115
116-
if (null !== $locale && isset(self::$declaredRoutes[$name.'.'.$locale])) {
116+
if (null !== $locale && (self::$declaredRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
117117
unset($parameters['_locale']);
118-
$name = $name.'.'.$locale;
118+
$name .= '.'.$locale;
119119
} elseif (!isset(self::$declaredRoutes[$name])) {
120120
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
121121
}

Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
116116
?? $this->context->getParameter('_locale')
117117
?: $this->defaultLocale;
118118

119-
if (null !== $locale && null !== $route = $this->routes->get($name.'.'.$locale)) {
119+
if (null !== $locale && null !== ($route = $this->routes->get($name.'.'.$locale)) && $route->getDefault('_canonical_route') === $name) {
120120
unset($parameters['_locale']);
121121
} elseif (null === $route = $this->routes->get($name)) {
122122
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));

Loader/AnnotationClassLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
200200
$this->configureRoute($route, $class, $method, $annot);
201201
if (0 !== $locale) {
202202
$route->setDefault('_locale', $locale);
203+
$route->setDefault('_canonical_route', $name);
203204
$collection->add($name.'.'.$locale, $route);
204205
} else {
205206
$collection->add($name, $route);

Loader/Configurator/ImportConfigurator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ final public function prefix($prefix)
5454
foreach ($prefix as $locale => $localePrefix) {
5555
$localizedRoute = clone $route;
5656
$localizedRoute->setDefault('_locale', $locale);
57+
$localizedRoute->setDefault('_canonical_route', $name);
5758
$localizedRoute->setPath($localePrefix.$route->getPath());
5859
$this->route->add($name.'.'.$locale, $localizedRoute);
5960
}

Loader/Configurator/Traits/AddTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ final public function add(string $name, $path): RouteConfigurator
6767
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
6868
$this->collection->add($this->name.$name.'.'.$locale, $route);
6969
$route->setDefault('_locale', $locale);
70+
$route->setDefault('_canonical_route', $this->name.$name);
7071
}
7172

7273
return new RouteConfigurator($this->collection, $routes, $this->name, $parentConfigurator, $this->prefixes);

Loader/XmlFileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
130130
} else {
131131
foreach ($paths as $locale => $p) {
132132
$defaults['_locale'] = $locale;
133-
$routeName = $id.'.'.$locale;
133+
$defaults['_canonical_route'] = $id;
134134
$route = new Route($p, $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
135-
$collection->add($routeName, $route);
135+
$collection->add($id.'.'.$locale, $route);
136136
}
137137
}
138138
}
@@ -183,6 +183,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
183183
$localizedRoute = clone $route;
184184
$localizedRoute->setPath($localePrefix.$route->getPath());
185185
$localizedRoute->setDefault('_locale', $locale);
186+
$localizedRoute->setDefault('_canonical_route', $name);
186187
$subCollection->add($name.'.'.$locale, $localizedRoute);
187188
}
188189
} elseif (!isset($prefixes[$locale])) {

Loader/YamlFileLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
126126
foreach ($config['path'] as $locale => $path) {
127127
$localizedRoute = clone $route;
128128
$localizedRoute->setDefault('_locale', $locale);
129+
$localizedRoute->setDefault('_canonical_route', $name);
129130
$localizedRoute->setPath($path);
130131
$collection->add($name.'.'.$locale, $localizedRoute);
131132
}
@@ -176,6 +177,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
176177
foreach ($prefix as $locale => $localePrefix) {
177178
$localizedRoute = clone $route;
178179
$localizedRoute->setDefault('_locale', $locale);
180+
$localizedRoute->setDefault('_canonical_route', $name);
179181
$localizedRoute->setPath($localePrefix.$route->getPath());
180182
$subCollection->add($name.'.'.$locale, $localizedRoute);
181183
}

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ private function compileStaticRoutes(array $staticRoutes, bool $matchHost): stri
256256
}
257257

258258
if (!$route->getCondition()) {
259+
$defaults = $route->getDefaults();
260+
if (isset($defaults['_canonical_route'])) {
261+
$name = $defaults['_canonical_route'];
262+
unset($defaults['_canonical_route']);
263+
}
259264
$default .= sprintf(
260265
"%s => array(%s, %s, %s, %s),\n",
261266
self::export($url),
262-
self::export(array('_route' => $name) + $route->getDefaults()),
267+
self::export(array('_route' => $name) + $defaults),
263268
self::export(!$route->compile()->getHostVariables() ? $route->getHost() : $route->compile()->getHostRegex() ?: null),
264269
self::export(array_flip($route->getMethods()) ?: null),
265270
self::export(array_flip($route->getSchemes()) ?: null)
@@ -490,10 +495,15 @@ private function compileStaticPrefixCollection(StaticPrefixCollection $tree, \st
490495

491496
if (!$route->getCondition() && (!is_array($next = $routes[1 + $i] ?? null) || $regex !== $next[1])) {
492497
$prevRegex = null;
498+
$defaults = $route->getDefaults();
499+
if (isset($defaults['_canonical_route'])) {
500+
$name = $defaults['_canonical_route'];
501+
unset($defaults['_canonical_route']);
502+
}
493503
$state->default .= sprintf(
494504
"%s => array(%s, %s, %s, %s),\n",
495505
$state->mark,
496-
self::export(array('_route' => $name) + $route->getDefaults()),
506+
self::export(array('_route' => $name) + $defaults),
497507
self::export($vars),
498508
self::export(array_flip($route->getMethods()) ?: null),
499509
self::export(array_flip($route->getSchemes()) ?: null)
@@ -619,6 +629,11 @@ private function compileRoute(Route $route, string $name, bool $checkHost): stri
619629

620630
// the offset where the return value is appended below, with indendation
621631
$retOffset = 12 + strlen($code);
632+
$defaults = $route->getDefaults();
633+
if (isset($defaults['_canonical_route'])) {
634+
$name = $defaults['_canonical_route'];
635+
unset($defaults['_canonical_route']);
636+
}
622637

623638
// optimize parameters array
624639
if ($matches || $hostMatches) {
@@ -633,10 +648,10 @@ private function compileRoute(Route $route, string $name, bool $checkHost): stri
633648
$code .= sprintf(
634649
" \$ret = \$this->mergeDefaults(%s, %s);\n",
635650
implode(' + ', $vars),
636-
self::export($route->getDefaults())
651+
self::export($defaults)
637652
);
638-
} elseif ($route->getDefaults()) {
639-
$code .= sprintf(" \$ret = %s;\n", self::export(array_replace($route->getDefaults(), array('_route' => $name))));
653+
} elseif ($defaults) {
654+
$code .= sprintf(" \$ret = %s;\n", self::export(array('_route' => $name) + $defaults));
640655
} else {
641656
$code .= sprintf(" \$ret = array('_route' => '%s');\n", $name);
642657
}

Matcher/UrlMatcher.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
194194
*/
195195
protected function getAttributes(Route $route, $name, array $attributes)
196196
{
197+
$defaults = $route->getDefaults();
198+
if (isset($defaults['_canonical_route'])) {
199+
$name = $defaults['_canonical_route'];
200+
unset($defaults['_canonical_route']);
201+
}
197202
$attributes['_route'] = $name;
198203

199-
return $this->mergeDefaults($attributes, $route->getDefaults());
204+
return $this->mergeDefaults($attributes, $defaults);
200205
}
201206

202207
/**

Tests/Generator/Dumper/PhpGeneratorDumperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public function testDumpWithRoutes()
8686

8787
public function testDumpWithLocalizedRoutes()
8888
{
89-
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en'));
90-
$this->routeCollection->add('test.nl', (new Route('/testen/is/leuk'))->setDefault('_locale', 'nl'));
89+
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'test'));
90+
$this->routeCollection->add('test.nl', (new Route('/testen/is/leuk'))->setDefault('_locale', 'nl')->setDefault('_canonical_route', 'test'));
9191

9292
$code = $this->generatorDumper->dump([
9393
'class' => 'LocalizedProjectUrlGenerator',

0 commit comments

Comments
 (0)