Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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, $hosts)
22
    {
23
        if (!$hosts || !\is_array($hosts)) {
24
            $routes->setHost($hosts ?: '');
25
 
26
            return;
27
        }
28
 
29
        foreach ($routes->all() as $name => $route) {
30
            if (null === $locale = $route->getDefault('_locale')) {
31
                $routes->remove($name);
32
                foreach ($hosts as $locale => $host) {
33
                    $localizedRoute = clone $route;
34
                    $localizedRoute->setDefault('_locale', $locale);
35
                    $localizedRoute->setRequirement('_locale', preg_quote($locale));
36
                    $localizedRoute->setDefault('_canonical_route', $name);
37
                    $localizedRoute->setHost($host);
38
                    $routes->add($name.'.'.$locale, $localizedRoute);
39
                }
40
            } elseif (!isset($hosts[$locale])) {
41
                throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding host in its parent collection.', $name, $locale));
42
            } else {
43
                $route->setHost($hosts[$locale]);
44
                $route->setRequirement('_locale', preg_quote($locale));
45
                $routes->add($name, $route);
46
            }
47
        }
48
    }
49
}