Skip to content

Commit a6ead57

Browse files
bug #59446 [Routing] Fix configuring a single route's hosts (MatTheCat)
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] Fix configuring a single route's hosts | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58086 | License | MIT All loaders were affected by this issue: when you configure a route it is immediately added to the main collection **and** in the one returned by the `LocalizedRouteTrait`. Problem is: only the latter is updated when configuring hosts, which means they will be ignored. This PR merges these two collections, with a twist: after its hosts are configured, a route may have to be removed from the main collection (like `static` becoming `static.nl` and `static.en` in the tests). Commits ------- ecbaff0 [Routing] Fix configuring a single route’ hosts
2 parents 72bd4ae + ecbaff0 commit a6ead57

File tree

10 files changed

+92
-2
lines changed

10 files changed

+92
-2
lines changed

src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ public function __construct(RouteCollection $collection, RouteCollection $route,
4242
*/
4343
final public function host(string|array $host): static
4444
{
45+
$previousRoutes = clone $this->route;
4546
$this->addHost($this->route, $host);
47+
foreach ($previousRoutes as $name => $route) {
48+
if (!$this->route->get($name)) {
49+
$this->collection->remove($name);
50+
}
51+
}
52+
$this->collection->addCollection($this->route);
4653

4754
return $this;
4855
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
135135
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $path));
136136
}
137137

138-
$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $node->getAttribute('path'));
138+
$routes = $this->createLocalizedRoute(new RouteCollection(), $id, $paths ?: $node->getAttribute('path'));
139139
$routes->addDefaults($defaults);
140140
$routes->addRequirements($requirements);
141141
$routes->addOptions($options);
@@ -146,6 +146,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
146146
if (null !== $hosts) {
147147
$this->addHost($routes, $hosts);
148148
}
149+
150+
$collection->addCollection($routes);
149151
}
150152

151153
/**

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
157157
$defaults['_stateless'] = $config['stateless'];
158158
}
159159

160-
$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
160+
$routes = $this->createLocalizedRoute(new RouteCollection(), $name, $config['path']);
161161
$routes->addDefaults($defaults);
162162
$routes->addRequirements($requirements);
163163
$routes->addOptions($options);
@@ -168,6 +168,8 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
168168
if (isset($config['host'])) {
169169
$this->addHost($routes, $config['host']);
170170
}
171+
172+
$collection->addCollection($routes);
171173
}
172174

173175
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Resource\FileResource;
4+
use Symfony\Component\Routing\Route;
5+
use Symfony\Component\Routing\RouteCollection;
6+
7+
return function (string $format) {
8+
$expectedRoutes = new RouteCollection();
9+
$expectedRoutes->add('static.en', $route = new Route('/example'));
10+
$route->setHost('www.example.com');
11+
$route->setRequirement('_locale', 'en');
12+
$route->setDefault('_locale', 'en');
13+
$route->setDefault('_canonical_route', 'static');
14+
$expectedRoutes->add('static.nl', $route = new Route('/example'));
15+
$route->setHost('www.example.nl');
16+
$route->setRequirement('_locale', 'nl');
17+
$route->setDefault('_locale', 'nl');
18+
$route->setDefault('_canonical_route', 'static');
19+
20+
$expectedRoutes->addResource(new FileResource(__DIR__."/route-with-hosts.$format"));
21+
22+
return $expectedRoutes;
23+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes->add('static', '/example')->host([
7+
'nl' => 'www.example.nl',
8+
'en' => 'www.example.com',
9+
]);
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
https://symfony.com/schema/routing/routing-1.0.xsd">
6+
<route id="static" path="/example">
7+
<host locale="nl">www.example.nl</host>
8+
<host locale="en">www.example.com</host>
9+
</route>
10+
</routes>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
static:
3+
path: /example
4+
host:
5+
nl: www.example.nl
6+
en: www.example.com

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ public function testImportingRoutesWithSingleHostInImporter()
317317
$this->assertEquals($expectedRoutes('php'), $routes);
318318
}
319319

320+
public function testAddingRouteWithHosts()
321+
{
322+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
323+
$routes = $loader->load('route-with-hosts.php');
324+
325+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';
326+
327+
$this->assertEquals($expectedRoutes('php'), $routes);
328+
}
329+
320330
public function testImportingAliases()
321331
{
322332
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/alias']));

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,16 @@ public function testImportingRoutesWithSingleHostsInImporter()
577577
$this->assertEquals($expectedRoutes('xml'), $routes);
578578
}
579579

580+
public function testAddingRouteWithHosts()
581+
{
582+
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
583+
$routes = $loader->load('route-with-hosts.xml');
584+
585+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';
586+
587+
$this->assertEquals($expectedRoutes('xml'), $routes);
588+
}
589+
580590
public function testWhenEnv()
581591
{
582592
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ public function testImportingRoutesWithSingleHostInImporter()
443443
$this->assertEquals($expectedRoutes('yml'), $routes);
444444
}
445445

446+
public function testAddingRouteWithHosts()
447+
{
448+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
449+
$routes = $loader->load('route-with-hosts.yml');
450+
451+
$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';
452+
453+
$this->assertEquals($expectedRoutes('yml'), $routes);
454+
}
455+
446456
public function testWhenEnv()
447457
{
448458
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');

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