From 4751a732f272f5c4f9e9f48fdceb2352ccbdd9ac Mon Sep 17 00:00:00 2001 From: Olivier Dolbeau Date: Tue, 31 Mar 2020 23:05:50 +0200 Subject: [PATCH] =?UTF-8?q?[Routing]=C2=A0Deal=20with=20hosts=20per=20loca?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Symfony/Component/Routing/CHANGELOG.md | 1 + .../Configurator/CollectionConfigurator.php | 19 +++++++ .../Configurator/ImportConfigurator.php | 15 ++++++ .../Loader/Configurator/RouteConfigurator.php | 15 ++++++ .../Loader/Configurator/Traits/HostTrait.php | 49 ++++++++++++++++++ .../Traits/LocalizedRouteTrait.php | 13 +++-- .../Routing/Loader/XmlFileLoader.php | 41 +++++++++------ .../Routing/Loader/YamlFileLoader.php | 23 +++++---- .../Loader/schema/routing/routing-1.0.xsd | 2 + .../import-with-host-expected-collection.php | 50 +++++++++++++++++++ ...th-locale-and-host-expected-collection.php | 50 +++++++++++++++++++ ...t-with-single-host-expected-collection.php | 32 ++++++++++++ ...mport-without-host-expected-collection.php | 31 ++++++++++++ .../Fixtures/locale_and_host/imported.php | 19 +++++++ .../Fixtures/locale_and_host/imported.xml | 19 +++++++ .../Fixtures/locale_and_host/imported.yml | 18 +++++++ .../locale_and_host/importer-with-host.php | 10 ++++ .../locale_and_host/importer-with-host.xml | 10 ++++ .../locale_and_host/importer-with-host.yml | 6 +++ .../importer-with-locale-and-host.php | 13 +++++ .../importer-with-locale-and-host.xml | 12 +++++ .../importer-with-locale-and-host.yml | 9 ++++ .../importer-with-single-host.php | 7 +++ .../importer-with-single-host.xml | 8 +++ .../importer-with-single-host.yml | 4 ++ .../locale_and_host/importer-without-host.php | 7 +++ .../locale_and_host/importer-without-host.xml | 8 +++ .../locale_and_host/importer-without-host.yml | 3 ++ .../Tests/Loader/PhpFileLoaderTest.php | 40 +++++++++++++++ .../Tests/Loader/XmlFileLoaderTest.php | 40 +++++++++++++++ .../Tests/Loader/YamlFileLoaderTest.php | 40 +++++++++++++++ 31 files changed, 584 insertions(+), 30 deletions(-) create mode 100644 src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-host-expected-collection.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-single-host-expected-collection.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-without-host-expected-collection.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.yml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.yml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.yml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.yml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.yml diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index b0f2f0e8d2..267372eb82 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php index 79c1100a82..1d93ca5f6f 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php @@ -20,11 +20,13 @@ use Symfony\Component\Routing\RouteCollection; 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) { @@ -41,6 +43,9 @@ class CollectionConfigurator 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); } @@ -86,6 +91,20 @@ class CollectionConfigurator 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); diff --git a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php index 37996536ed..1841253694 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php @@ -18,6 +18,7 @@ use Symfony\Component\Routing\RouteCollection; */ class ImportConfigurator { + use Traits\HostTrait; use Traits\PrefixTrait; use Traits\RouteTrait; @@ -59,4 +60,18 @@ class ImportConfigurator 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; + } } diff --git a/src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php index d617403a51..fcb377182e 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php @@ -19,6 +19,7 @@ use Symfony\Component\Routing\RouteCollection; class RouteConfigurator { use Traits\AddTrait; + use Traits\HostTrait; use Traits\RouteTrait; protected $parentConfigurator; @@ -31,4 +32,18 @@ class RouteConfigurator $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; + } } diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php new file mode 100644 index 0000000000..54ae6566a9 --- /dev/null +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/HostTrait.php @@ -0,0 +1,49 @@ + + * + * 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); + } + } + } +} diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php index 8bd54095f6..4734a4eac0 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/LocalizedRouteTrait.php @@ -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; @@ -52,13 +52,12 @@ trait LocalizedRouteTrait $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); diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 5e31d69785..12f437341d 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Routing\Loader; 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; @@ -26,6 +27,7 @@ use Symfony\Component\Routing\RouteCollection; */ class XmlFileLoader extends FileLoader { + use HostTrait; use LocalizedRouteTrait; use PrefixTrait; @@ -116,7 +118,7 @@ class XmlFileLoader extends FileLoader $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'); @@ -128,14 +130,17 @@ class XmlFileLoader extends FileLoader throw new \InvalidArgumentException(sprintf('The element in file "%s" must not have both a "path" attribute and 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); + } } /** @@ -155,13 +160,12 @@ class XmlFileLoader extends FileLoader $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 element in file "%s" must not have both a "prefix" attribute and child nodes.', $path)); @@ -193,9 +197,10 @@ class XmlFileLoader extends FileLoader 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); } @@ -245,6 +250,7 @@ class XmlFileLoader extends FileLoader $condition = null; $prefixes = []; $paths = []; + $hosts = []; /** @var \DOMElement $n */ foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) { @@ -256,6 +262,9 @@ class XmlFileLoader extends FileLoader 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; @@ -309,7 +318,11 @@ class XmlFileLoader extends FileLoader $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]; } /** diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index b0718f6838..c62c0abc11 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -13,6 +13,7 @@ namespace Symfony\Component\Routing\Loader; 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; @@ -28,6 +29,7 @@ use Symfony\Component\Yaml\Yaml; */ class YamlFileLoader extends FileLoader { + use HostTrait; use LocalizedRouteTrait; use PrefixTrait; @@ -137,14 +139,17 @@ class YamlFileLoader extends FileLoader $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']); + } } /** @@ -198,7 +203,7 @@ class YamlFileLoader extends FileLoader $this->addPrefix($subCollection, $prefix, $trailingSlashOnRoot); if (null !== $host) { - $subCollection->setHost($host); + $this->addHost($subCollection, $host); } if (null !== $condition) { $subCollection->setCondition($condition); diff --git a/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd b/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd index 423aa7979e..846d126724 100644 --- a/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd +++ b/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd @@ -45,6 +45,7 @@ + @@ -63,6 +64,7 @@ + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-host-expected-collection.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-host-expected-collection.php new file mode 100644 index 0000000000..13f4a09b08 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-host-expected-collection.php @@ -0,0 +1,50 @@ +add('imported.en', $route = new Route('/example')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported.nl', $route = new Route('/voorbeeld')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized.en', $route = new Route('/here')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported_not_localized'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized.nl', $route = new Route('/here')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported_not_localized'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host.en', $route = new Route('/here_again')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported_single_host'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host.nl', $route = new Route('/here_again')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported_single_host'); + $route->setDefault('_controller', 'ImportedController::someAction'); + + $expectedRoutes->addResource(new FileResource(__DIR__."/imported.$format")); + $expectedRoutes->addResource(new FileResource(__DIR__."/importer-with-host.$format")); + + return $expectedRoutes; +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php new file mode 100644 index 0000000000..099fbdcf47 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php @@ -0,0 +1,50 @@ +add('imported.en', $route = new Route('/en/example')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported.nl', $route = new Route('/nl/voorbeeld')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized.en', $route = new Route('/en/here')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported_not_localized'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized.nl', $route = new Route('/nl/here')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported_not_localized'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host.en', $route = new Route('/en/here_again')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported_single_host'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host.nl', $route = new Route('/nl/here_again')); + $route->setHost('www.example.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported_single_host'); + $route->setDefault('_controller', 'ImportedController::someAction'); + + $expectedRoutes->addResource(new FileResource(__DIR__."/imported.$format")); + $expectedRoutes->addResource(new FileResource(__DIR__."/importer-with-locale-and-host.$format")); + + return $expectedRoutes; +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-single-host-expected-collection.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-single-host-expected-collection.php new file mode 100644 index 0000000000..fd66fd537c --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-with-single-host-expected-collection.php @@ -0,0 +1,32 @@ +add('imported.en', $route = new Route('/example')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported.nl', $route = new Route('/voorbeeld')); + $route->setHost('www.example.com'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized', $route = new Route('/here')); + $route->setHost('www.example.com'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host', $route = new Route('/here_again')); + $route->setHost('www.example.com'); + $route->setDefault('_controller', 'ImportedController::someAction'); + + $expectedRoutes->addResource(new FileResource(__DIR__."/imported.$format")); + $expectedRoutes->addResource(new FileResource(__DIR__."/importer-with-single-host.$format")); + + return $expectedRoutes; +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-without-host-expected-collection.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-without-host-expected-collection.php new file mode 100644 index 0000000000..bd2e41353f --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/import-without-host-expected-collection.php @@ -0,0 +1,31 @@ +add('imported.en', $route = new Route('/example')); + $route->setHost('www.custom.com'); + $route->setRequirement('_locale', 'en'); + $route->setDefault('_locale', 'en'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported.nl', $route = new Route('/voorbeeld')); + $route->setHost('www.custom.nl'); + $route->setRequirement('_locale', 'nl'); + $route->setDefault('_locale', 'nl'); + $route->setDefault('_canonical_route', 'imported'); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_not_localized', $route = new Route('/here')); + $route->setDefault('_controller', 'ImportedController::someAction'); + $expectedRoutes->add('imported_single_host', $route = new Route('/here_again')); + $route->setHost('www.custom.com'); + $route->setDefault('_controller', 'ImportedController::someAction'); + + $expectedRoutes->addResource(new FileResource(__DIR__."/imported.$format")); + $expectedRoutes->addResource(new FileResource(__DIR__."/importer-without-host.$format")); + + return $expectedRoutes; +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.php new file mode 100644 index 0000000000..4abe703b95 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.php @@ -0,0 +1,19 @@ +add('imported', ['nl' => '/voorbeeld', 'en' => '/example']) + ->controller('ImportedController::someAction') + ->host([ + 'nl' => 'www.custom.nl', + 'en' => 'www.custom.com', + ]) + ->add('imported_not_localized', '/here') + ->controller('ImportedController::someAction') + ->add('imported_single_host', '/here_again') + ->controller('ImportedController::someAction') + ->host('www.custom.com') + ; +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.xml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.xml new file mode 100644 index 0000000000..30ff6811a2 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.xml @@ -0,0 +1,19 @@ + + + + ImportedController::someAction + /voorbeeld + /example + www.custom.nl + www.custom.com + + + ImportedController::someAction + + + ImportedController::someAction + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.yml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.yml new file mode 100644 index 0000000000..22feea82e5 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/imported.yml @@ -0,0 +1,18 @@ +--- +imported: + controller: ImportedController::someAction + path: + nl: /voorbeeld + en: /example + host: + nl: www.custom.nl + en: www.custom.com + +imported_not_localized: + controller: ImportedController::someAction + path: /here + +imported_single_host: + controller: ImportedController::someAction + path: /here_again + host: www.custom.com diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.php new file mode 100644 index 0000000000..14c3e96312 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.php @@ -0,0 +1,10 @@ +import('imported.php')->host([ + 'nl' => 'www.example.nl', + 'en' => 'www.example.com', + ]); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.xml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.xml new file mode 100644 index 0000000000..e06136d8af --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.xml @@ -0,0 +1,10 @@ + + + + www.example.nl + www.example.com + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.yml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.yml new file mode 100644 index 0000000000..f93ece8b7f --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-host.yml @@ -0,0 +1,6 @@ +--- +i_need: + resource: ./imported.yml + host: + nl: www.example.nl + en: www.example.com diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.php new file mode 100644 index 0000000000..ae86b05f8a --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.php @@ -0,0 +1,13 @@ +import('imported.php')->host([ + 'nl' => 'www.example.nl', + 'en' => 'www.example.com', + ])->prefix([ + 'nl' => '/nl', + 'en' => '/en', + ]); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.xml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.xml new file mode 100644 index 0000000000..71904bd2a6 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.xml @@ -0,0 +1,12 @@ + + + + /nl + /en + www.example.nl + www.example.com + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.yml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.yml new file mode 100644 index 0000000000..bc10ec4c50 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-locale-and-host.yml @@ -0,0 +1,9 @@ +--- +i_need: + resource: ./imported.yml + prefix: + nl: /nl + en: /en + host: + nl: www.example.nl + en: www.example.com diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.php new file mode 100644 index 0000000000..834f2cbb26 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.php @@ -0,0 +1,7 @@ +import('imported.php')->host('www.example.com'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.xml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.xml new file mode 100644 index 0000000000..121a78b2bf --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.yml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.yml new file mode 100644 index 0000000000..5e4d45c29a --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-with-single-host.yml @@ -0,0 +1,4 @@ +--- +i_need: + resource: ./imported.yml + host: www.example.com diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.php b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.php new file mode 100644 index 0000000000..ab5565c7a9 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.php @@ -0,0 +1,7 @@ +import('imported.php'); +}; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.xml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.xml new file mode 100644 index 0000000000..a8fb3d8e66 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.yml b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.yml new file mode 100644 index 0000000000..ef7ecebb82 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/locale_and_host/importer-without-host.yml @@ -0,0 +1,3 @@ +--- +i_need: + resource: ./imported.yml diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index ffde004ade..0c46ea2b9c 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -243,4 +243,44 @@ class PhpFileLoaderTest extends TestCase $this->assertEquals($expectedCollection, $routeCollection); } + + public function testImportingRoutesWithHostsInImporter() + { + $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-host.php'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('php'), $routes); + } + + public function testImportingRoutesWithLocalesAndHostInImporter() + { + $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-locale-and-host.php'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('php'), $routes); + } + + public function testImportingRoutesWithoutHostInImporter() + { + $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-without-host.php'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('php'), $routes); + } + + public function testImportingRoutesWithSingleHostInImporter() + { + $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-single-host.php'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('php'), $routes); + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php index b448c68bbc..65ecc2d9b4 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -520,4 +520,44 @@ class XmlFileLoaderTest extends TestCase $this->assertEquals('/slash/', $routeCollection->get('a_app_homepage')->getPath()); $this->assertEquals('/no-slash', $routeCollection->get('b_app_homepage')->getPath()); } + + public function testImportingRoutesWithHostsInImporter() + { + $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-host.xml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('xml'), $routes); + } + + public function testImportingRoutesWithLocalesAndHostInImporter() + { + $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-locale-and-host.xml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('xml'), $routes); + } + + public function testImportingRoutesWithoutHostsInImporter() + { + $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-without-host.xml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('xml'), $routes); + } + + public function testImportingRoutesWithSingleHostsInImporter() + { + $loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-single-host.xml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('xml'), $routes); + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php index 24bb4eeb4e..39a1541663 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -392,4 +392,44 @@ class YamlFileLoaderTest extends TestCase $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures'])); $loader->load('requirements_without_placeholder_name.yml'); } + + public function testImportingRoutesWithHostsInImporter() + { + $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-host.yml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('yml'), $routes); + } + + public function testImportingRoutesWithLocalesAndHostInImporter() + { + $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-locale-and-host.yml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-locale-and-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('yml'), $routes); + } + + public function testImportingRoutesWithoutHostInImporter() + { + $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-without-host.yml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-without-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('yml'), $routes); + } + + public function testImportingRoutesWithSingleHostInImporter() + { + $loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/locale_and_host'])); + $routes = $loader->load('importer-with-single-host.yml'); + + $expectedRoutes = require __DIR__.'/../Fixtures/locale_and_host/import-with-single-host-expected-collection.php'; + + $this->assertEquals($expectedRoutes('yml'), $routes); + } }