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\Generator;
13
 
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\Routing\Exception\RouteNotFoundException;
16
use Symfony\Component\Routing\RequestContext;
17
 
18
/**
19
 * Generates URLs based on rules dumped by CompiledUrlGeneratorDumper.
20
 */
21
class CompiledUrlGenerator extends UrlGenerator
22
{
23
    private $compiledRoutes = [];
24
    private $defaultLocale;
25
 
26
    public function __construct(array $compiledRoutes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)
27
    {
28
        $this->compiledRoutes = $compiledRoutes;
29
        $this->context = $context;
30
        $this->logger = $logger;
31
        $this->defaultLocale = $defaultLocale;
32
    }
33
 
34
    public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
35
    {
36
        $locale = $parameters['_locale']
37
            ?? $this->context->getParameter('_locale')
38
            ?: $this->defaultLocale;
39
 
40
        if (null !== $locale) {
41
            do {
42
                if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
43
                    $name .= '.'.$locale;
44
                    break;
45
                }
46
            } while (false !== $locale = strstr($locale, '_', true));
47
        }
48
 
49
        if (!isset($this->compiledRoutes[$name])) {
50
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
51
        }
52
 
53
        list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->compiledRoutes[$name];
54
 
55
        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
56
            if (!\in_array('_locale', $variables, true)) {
57
                unset($parameters['_locale']);
58
            } elseif (!isset($parameters['_locale'])) {
59
                $parameters['_locale'] = $defaults['_locale'];
60
            }
61
        }
62
 
63
        return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
64
    }
65
}