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\Route;
15
use Symfony\Component\Routing\RouteCollection;
16
 
17
/**
18
 * @internal
19
 *
20
 * @author Nicolas Grekas <p@tchwork.com>
21
 */
22
trait PrefixTrait
23
{
24
    final protected function addPrefix(RouteCollection $routes, $prefix, bool $trailingSlashOnRoot)
25
    {
26
        if (\is_array($prefix)) {
27
            foreach ($prefix as $locale => $localePrefix) {
28
                $prefix[$locale] = trim(trim($localePrefix), '/');
29
            }
30
            foreach ($routes->all() as $name => $route) {
31
                if (null === $locale = $route->getDefault('_locale')) {
32
                    $routes->remove($name);
33
                    foreach ($prefix as $locale => $localePrefix) {
34
                        $localizedRoute = clone $route;
35
                        $localizedRoute->setDefault('_locale', $locale);
36
                        $localizedRoute->setRequirement('_locale', preg_quote($locale));
37
                        $localizedRoute->setDefault('_canonical_route', $name);
38
                        $localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
39
                        $routes->add($name.'.'.$locale, $localizedRoute);
40
                    }
41
                } elseif (!isset($prefix[$locale])) {
42
                    throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
43
                } else {
44
                    $route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
45
                    $routes->add($name, $route);
46
                }
47
            }
48
 
49
            return;
50
        }
51
 
52
        $routes->addPrefix($prefix);
53
        if (!$trailingSlashOnRoot) {
54
            $rootPath = (new Route(trim(trim($prefix), '/').'/'))->getPath();
55
            foreach ($routes->all() as $route) {
56
                if ($route->getPath() === $rootPath) {
57
                    $route->setPath(rtrim($rootPath, '/'));
58
                }
59
            }
60
        }
61
    }
62
}