Skip to content

Commit

Permalink
[Routing] Fix configuring a single route’ hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat committed Jan 9, 2025
1 parent 91e02e6 commit e9bfc94
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Loader/Configurator/RouteConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public function __construct(RouteCollection $collection, RouteCollection $route,
*/
final public function host(string|array $host): static
{
$previousRoutes = clone $this->route;
$this->addHost($this->route, $host);
foreach ($previousRoutes as $name => $route) {
if (!$this->route->get($name)) {
$this->collection->remove($name);
}
}
$this->collection->addCollection($this->route);

return $this;
}
Expand Down
4 changes: 3 additions & 1 deletion Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $path));
}

$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $node->getAttribute('path'));
$routes = $this->createLocalizedRoute(new RouteCollection(), $id, $paths ?: $node->getAttribute('path'));
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
Expand All @@ -146,6 +146,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
if (null !== $hosts) {
$this->addHost($routes, $hosts);
}

$collection->addCollection($routes);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
$defaults['_stateless'] = $config['stateless'];
}

$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
$routes = $this->createLocalizedRoute(new RouteCollection(), $name, $config['path']);
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
Expand All @@ -168,6 +168,8 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
}

$collection->addCollection($routes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

return function (string $format) {
$expectedRoutes = new RouteCollection();
$expectedRoutes->add('static.en', $route = new Route('/example'));
$route->setHost('www.example.com');
$route->setRequirement('_locale', 'en');
$route->setDefault('_locale', 'en');
$route->setDefault('_canonical_route', 'static');
$expectedRoutes->add('static.nl', $route = new Route('/example'));
$route->setHost('www.example.nl');
$route->setRequirement('_locale', 'nl');
$route->setDefault('_locale', 'nl');
$route->setDefault('_canonical_route', 'static');

$expectedRoutes->addResource(new FileResource(__DIR__."/route-with-hosts.$format"));

return $expectedRoutes;
};
10 changes: 10 additions & 0 deletions Tests/Fixtures/locale_and_host/route-with-hosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->add('static', '/example')->host([
'nl' => 'www.example.nl',
'en' => 'www.example.com',
]);
};
10 changes: 10 additions & 0 deletions Tests/Fixtures/locale_and_host/route-with-hosts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">
<route id="static" path="/example">
<host locale="nl">www.example.nl</host>
<host locale="en">www.example.com</host>
</route>
</routes>
6 changes: 6 additions & 0 deletions Tests/Fixtures/locale_and_host/route-with-hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
static:
path: /example
host:
nl: www.example.nl
en: www.example.com
10 changes: 10 additions & 0 deletions Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ public function testImportingRoutesWithSingleHostInImporter()
$this->assertEquals($expectedRoutes('php'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.php');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('php'), $routes);
}

public function testImportingAliases()
{
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/alias']));
Expand Down
10 changes: 10 additions & 0 deletions Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ public function testImportingRoutesWithSingleHostsInImporter()
$this->assertEquals($expectedRoutes('xml'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.xml');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('xml'), $routes);
}

public function testWhenEnv()
{
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
Expand Down
10 changes: 10 additions & 0 deletions Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ public function testImportingRoutesWithSingleHostInImporter()
$this->assertEquals($expectedRoutes('yml'), $routes);
}

public function testAddingRouteWithHosts()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host']));
$routes = $loader->load('route-with-hosts.yml');

$expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/route-with-hosts-expected-collection.php';

$this->assertEquals($expectedRoutes('yml'), $routes);
}

public function testWhenEnv()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
Expand Down

0 comments on commit e9bfc94

Please sign in to comment.