Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e48a5b8

Browse filesBrowse files
author
Olivier Dolbeau
committed
Deal with hosts per locale config
1 parent 8e47e9f commit e48a5b8
Copy full SHA for e48a5b8

File tree

Expand file treeCollapse file tree

5 files changed

+125
-3
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+125
-3
lines changed
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator\Traits;
13+
14+
use Symfony\Component\Routing\RouteCollection;
15+
16+
/**
17+
* @internal
18+
*/
19+
trait HostTrait
20+
{
21+
final protected function addHost(RouteCollection $routes, $host)
22+
{
23+
if (null === $host) {
24+
return;
25+
}
26+
27+
if (!\is_array($host)) {
28+
$routes->setHost($host);
29+
30+
return;
31+
}
32+
33+
foreach ($host as $locale => $localeHost) {
34+
$host[$locale] = trim(trim($localeHost), '/');
35+
}
36+
37+
foreach ($routes->all() as $name => $route) {
38+
if ('' !== $route->getHost()) {
39+
continue;
40+
}
41+
42+
if (null === $locale = $route->getDefault('_locale')) {
43+
$routes->remove($name);
44+
foreach ($host as $locale => $localeHost) {
45+
$localizedRoute = clone $route;
46+
$localizedRoute->setDefault('_locale', $locale);
47+
$localizedRoute->setDefault('_canonical_route', $name);
48+
$localizedRoute->setHost($localeHost);
49+
$routes->add($name.'.'.$locale, $localizedRoute);
50+
}
51+
} elseif (!isset($host[$locale])) {
52+
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
53+
} else {
54+
$route->setHost($host[$locale]);
55+
$routes->add($name, $route);
56+
}
57+
}
58+
}
59+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Loader\FileLoader;
1515
use Symfony\Component\Config\Resource\FileResource;
16+
use Symfony\Component\Routing\Loader\Configurator\Traits\HostTrait;
1617
use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
1718
use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
1819
use Symfony\Component\Routing\RouteCollection;
@@ -28,6 +29,7 @@
2829
*/
2930
class YamlFileLoader extends FileLoader
3031
{
32+
use HostTrait;
3133
use LocalizedRouteTrait;
3234
use PrefixTrait;
3335

@@ -196,10 +198,8 @@ protected function parseImport(RouteCollection $collection, array $config, strin
196198

197199
foreach ($imported as $subCollection) {
198200
$this->addPrefix($subCollection, $prefix, $trailingSlashOnRoot);
201+
$this->addHost($subCollection, $host);
199202

200-
if (null !== $host) {
201-
$subCollection->setHost($host);
202-
}
203203
if (null !== $condition) {
204204
$subCollection->setCondition($condition);
205205
}
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
i_need:
3+
resource: ./localized-route.yml
4+
host:
5+
nl: www.example.nl
6+
en: www.example.com
7+
8+
i_need_also:
9+
resource: ./localized-route-with-host.yml
10+
host:
11+
nl: www.example.nl
12+
en: www.example.com
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
home_with_host:
3+
path:
4+
nl: /nl
5+
en: /en
6+
host: www.my_host.com
7+
8+
not_localized_with_host:
9+
controller: HomeController::otherAction
10+
path: /here
11+
host: www.my_host.com

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,46 @@ public function testImportingWithControllerDefault()
375375
$this->assertEquals('DefaultController::defaultAction', $routes->get('not_localized')->getDefault('_controller'));
376376
}
377377

378+
public function testImportingRoutesWithHosts()
379+
{
380+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
381+
$routes = $loader->load('importer-with-host.yml');
382+
383+
$this->assertCount(7, $routes);
384+
385+
$route = $routes->get('home.nl');
386+
$this->assertEquals('/nl', $route->getPath());
387+
$this->assertEquals('www.example.nl', $route->getHost());
388+
$this->assertEquals('nl', $route->getRequirement('_locale'));
389+
390+
$route = $routes->get('home.en');
391+
$this->assertEquals('/en', $route->getPath());
392+
$this->assertEquals('www.example.com', $route->getHost());
393+
$this->assertEquals('en', $route->getRequirement('_locale'));
394+
395+
$route = $routes->get('not_localized.nl');
396+
$this->assertEquals('/here', $route->getPath());
397+
$this->assertEquals('www.example.nl', $route->getHost());
398+
399+
$route = $routes->get('not_localized.en');
400+
$this->assertEquals('/here', $route->getPath());
401+
$this->assertEquals('www.example.com', $route->getHost());
402+
403+
$route = $routes->get('home_with_host.nl');
404+
$this->assertEquals('/nl', $route->getPath());
405+
$this->assertEquals('www.my_host.com', $route->getHost());
406+
$this->assertEquals('nl', $route->getRequirement('_locale'));
407+
408+
$route = $routes->get('home_with_host.en');
409+
$this->assertEquals('/en', $route->getPath());
410+
$this->assertEquals('www.my_host.com', $route->getHost());
411+
$this->assertEquals('en', $route->getRequirement('_locale'));
412+
413+
$route = $routes->get('not_localized_with_host');
414+
$this->assertEquals('/here', $route->getPath());
415+
$this->assertEquals('www.my_host.com', $route->getHost());
416+
}
417+
378418
public function testImportRouteWithNoTrailingSlash()
379419
{
380420
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/import_with_no_trailing_slash']));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.