Skip to content

[Routing] Deal with hosts per locale #36187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2020
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* deprecated the `RouteCompiler::REGEX_DELIMITER` constant
* added `ExpressionLanguageProvider` to expose extra functions to route conditions
* added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations.
* added the "hosts" option to be able to configure the host per locale.

5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
class CollectionConfigurator
{
use Traits\AddTrait;
use Traits\HostTrait;
use Traits\RouteTrait;

private $parent;
private $parentConfigurator;
private $parentPrefixes;
private $host;

public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null, array $parentPrefixes = null)
{
Expand All @@ -41,6 +43,9 @@ public function __destruct()
if (null === $this->prefixes) {
$this->collection->addPrefix($this->route->getPath());
}
if (null !== $this->host) {
$this->addHost($this->collection, $this->host);
}

$this->parent->addCollection($this->collection);
}
Expand Down Expand Up @@ -86,6 +91,20 @@ final public function prefix($prefix): self
return $this;
}

/**
* Sets the host to use for all child routes.
*
* @param string|array $host the host, or the localized hosts
*
* @return $this
*/
final public function host($host): self
{
$this->host = $host;

return $this;
}

private function createRoute(string $path): Route
{
return (clone $this->route)->setPath($path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
class ImportConfigurator
{
use Traits\HostTrait;
use Traits\PrefixTrait;
use Traits\RouteTrait;

Expand Down Expand Up @@ -59,4 +60,18 @@ final public function namePrefix(string $namePrefix): self

return $this;
}

/**
* Sets the host to use for all child routes.
*
* @param string|array $host the host, or the localized hosts
*
* @return $this
*/
final public function host($host): self
{
$this->addHost($this->route, $host);

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class RouteConfigurator
{
use Traits\AddTrait;
use Traits\HostTrait;
use Traits\RouteTrait;

protected $parentConfigurator;
Expand All @@ -31,4 +32,18 @@ public function __construct(RouteCollection $collection, $route, string $name =
$this->parentConfigurator = $parentConfigurator; // for GC control
$this->prefixes = $prefixes;
}

/**
* Sets the host to use for all child routes.
*
* @param string|array $host the host, or the localized hosts
*
* @return $this
*/
final public function host($host): self
{
$this->addHost($this->route, $host);

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Loader\Configurator\Traits;

use Symfony\Component\Routing\RouteCollection;

/**
* @internal
*/
trait HostTrait
{
final protected function addHost(RouteCollection $routes, $hosts)
{
if (!$hosts || !\is_array($hosts)) {
$routes->setHost($hosts ?: '');

return;
}

foreach ($routes->all() as $name => $route) {
if (null === $locale = $route->getDefault('_locale')) {
$routes->remove($name);
foreach ($hosts as $locale => $host) {
$localizedRoute = clone $route;
$localizedRoute->setDefault('_locale', $locale);
$localizedRoute->setRequirement('_locale', preg_quote($locale));
$localizedRoute->setDefault('_canonical_route', $name);
$localizedRoute->setHost($host);
$routes->add($name.'.'.$locale, $localizedRoute);
}
} elseif (!isset($hosts[$locale])) {
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
} else {
$route->setHost($hosts[$locale]);
$route->setRequirement('_locale', preg_quote($locale));
$routes->add($name, $route);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ trait LocalizedRouteTrait
* Creates one or many routes.
*
* @param string|array $path the path, or the localized paths of the route
*
* @return Route|RouteCollection
*/
final protected function createLocalizedRoute(RouteCollection $collection, string $name, $path, string $namePrefix = '', array $prefixes = null)
final protected function createLocalizedRoute(RouteCollection $collection, string $name, $path, string $namePrefix = '', array $prefixes = null): RouteCollection
{
$paths = [];

$routes = new RouteCollection();

if (\is_array($path)) {
if (null === $prefixes) {
$paths = $path;
Expand All @@ -52,13 +52,12 @@ final protected function createLocalizedRoute(RouteCollection $collection, strin
$paths[$locale] = $prefix.$path;
}
} else {
$collection->add($namePrefix.$name, $route = $this->createRoute($path));
$routes->add($namePrefix.$name, $route = $this->createRoute($path));
$collection->add($namePrefix.$name, $route);

return $route;
return $routes;
}

$routes = new RouteCollection();

foreach ($paths as $locale => $path) {
$routes->add($name.'.'.$locale, $route = $this->createRoute($path));
$collection->add($namePrefix.$name.'.'.$locale, $route);
Expand Down
41 changes: 27 additions & 14 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\Routing\Loader\Configurator\Traits\HostTrait;
use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
use Symfony\Component\Routing\RouteCollection;
Expand All @@ -26,6 +27,7 @@
*/
class XmlFileLoader extends FileLoader
{
use HostTrait;
use LocalizedRouteTrait;
use PrefixTrait;

Expand Down Expand Up @@ -116,7 +118,7 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, st
$schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
$methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);

list($defaults, $requirements, $options, $condition, $paths) = $this->parseConfigs($node, $filepath);
list($defaults, $requirements, $options, $condition, $paths, /* $prefixes */, $hosts) = $this->parseConfigs($node, $filepath);

$path = $node->getAttribute('path');

Expand All @@ -128,14 +130,17 @@ 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.', $filepath));
}

$route = $this->createLocalizedRoute($collection, $id, $paths ?: $path);
$route->addDefaults($defaults);
$route->addRequirements($requirements);
$route->addOptions($options);
$route->setHost($node->getAttribute('host'));
$route->setSchemes($schemes);
$route->setMethods($methods);
$route->setCondition($condition);
$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $path);
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
$routes->setSchemes($schemes);
$routes->setMethods($methods);
$routes->setCondition($condition);

if (null !== $hosts) {
$this->addHost($routes, $hosts);
}
}

/**
Expand All @@ -155,13 +160,12 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, s

$type = $node->getAttribute('type');
$prefix = $node->getAttribute('prefix');
$host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
$schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
$methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
$trailingSlashOnRoot = $node->hasAttribute('trailing-slash-on-root') ? XmlUtils::phpize($node->getAttribute('trailing-slash-on-root')) : true;
$namePrefix = $node->getAttribute('name-prefix') ?: null;

list($defaults, $requirements, $options, $condition, /* $paths */, $prefixes) = $this->parseConfigs($node, $path);
list($defaults, $requirements, $options, $condition, /* $paths */, $prefixes, $hosts) = $this->parseConfigs($node, $path);

if ('' !== $prefix && $prefixes) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "prefix" attribute and <prefix> child nodes.', $path));
Expand Down Expand Up @@ -193,9 +197,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, s
foreach ($imported as $subCollection) {
$this->addPrefix($subCollection, $prefixes ?: $prefix, $trailingSlashOnRoot);

if (null !== $host) {
$subCollection->setHost($host);
if (null !== $hosts) {
$this->addHost($subCollection, $hosts);
}

if (null !== $condition) {
$subCollection->setCondition($condition);
}
Expand Down Expand Up @@ -245,6 +250,7 @@ private function parseConfigs(\DOMElement $node, string $path): array
$condition = null;
$prefixes = [];
$paths = [];
$hosts = [];

/** @var \DOMElement $n */
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
Expand All @@ -256,6 +262,9 @@ private function parseConfigs(\DOMElement $node, string $path): array
case 'path':
$paths[$n->getAttribute('locale')] = trim($n->textContent);
break;
case 'host':
$hosts[$n->getAttribute('locale')] = trim($n->textContent);
break;
case 'prefix':
$prefixes[$n->getAttribute('locale')] = trim($n->textContent);
break;
Expand Down Expand Up @@ -309,7 +318,11 @@ private function parseConfigs(\DOMElement $node, string $path): array
$defaults['_stateless'] = XmlUtils::phpize($stateless);
}

return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
if (!$hosts) {
$hosts = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
}

return [$defaults, $requirements, $options, $condition, $paths, $prefixes, $hosts];
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Loader\Configurator\Traits\HostTrait;
use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
use Symfony\Component\Routing\RouteCollection;
Expand All @@ -28,6 +29,7 @@
*/
class YamlFileLoader extends FileLoader
{
use HostTrait;
use LocalizedRouteTrait;
use PrefixTrait;

Expand Down Expand Up @@ -137,14 +139,17 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
$defaults['_stateless'] = $config['stateless'];
}

$route = $this->createLocalizedRoute($collection, $name, $config['path']);
$route->addDefaults($defaults);
$route->addRequirements($requirements);
$route->addOptions($options);
$route->setHost($config['host'] ?? '');
$route->setSchemes($config['schemes'] ?? []);
$route->setMethods($config['methods'] ?? []);
$route->setCondition($config['condition'] ?? null);
$routes = $this->createLocalizedRoute($collection, $name, $config['path']);
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);
$routes->setSchemes($config['schemes'] ?? []);
$routes->setMethods($config['methods'] ?? []);
$routes->setCondition($config['condition'] ?? null);

if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
}
}

/**
Expand Down Expand Up @@ -198,7 +203,7 @@ protected function parseImport(RouteCollection $collection, array $config, strin
$this->addPrefix($subCollection, $prefix, $trailingSlashOnRoot);

if (null !== $host) {
$subCollection->setHost($host);
$this->addHost($subCollection, $host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<xsd:sequence>
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="host" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="path" type="xsd:string" />
Expand All @@ -63,6 +64,7 @@
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="prefix" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="exclude" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="host" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="resource" type="xsd:string" use="required" />
<xsd:attribute name="type" type="xsd:string" />
Expand Down
Loading