Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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\HttpKernel\Fragment;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\HttpKernel\Controller\ControllerReference;
17
use Symfony\Component\HttpKernel\UriSigner;
18
 
19
/**
20
 * Generates a fragment URI.
21
 *
22
 * @author Kévin Dunglas <kevin@dunglas.fr>
23
 * @author Fabien Potencier <fabien@symfony.com>
24
 */
25
final class FragmentUriGenerator implements FragmentUriGeneratorInterface
26
{
27
    private string $fragmentPath;
28
    private ?UriSigner $signer;
29
    private ?RequestStack $requestStack;
30
 
31
    public function __construct(string $fragmentPath, UriSigner $signer = null, RequestStack $requestStack = null)
32
    {
33
        $this->fragmentPath = $fragmentPath;
34
        $this->signer = $signer;
35
        $this->requestStack = $requestStack;
36
    }
37
 
38
    public function generate(ControllerReference $controller, Request $request = null, bool $absolute = false, bool $strict = true, bool $sign = true): string
39
    {
40
        if (null === $request && (null === $this->requestStack || null === $request = $this->requestStack->getCurrentRequest())) {
41
            throw new \LogicException('Generating a fragment URL can only be done when handling a Request.');
42
        }
43
 
44
        if ($sign && null === $this->signer) {
45
            throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
46
        }
47
 
48
        if ($strict) {
49
            $this->checkNonScalar($controller->attributes);
50
        }
51
 
52
        // We need to forward the current _format and _locale values as we don't have
53
        // a proper routing pattern to do the job for us.
54
        // This makes things inconsistent if you switch from rendering a controller
55
        // to rendering a route if the route pattern does not contain the special
56
        // _format and _locale placeholders.
57
        if (!isset($controller->attributes['_format'])) {
58
            $controller->attributes['_format'] = $request->getRequestFormat();
59
        }
60
        if (!isset($controller->attributes['_locale'])) {
61
            $controller->attributes['_locale'] = $request->getLocale();
62
        }
63
 
64
        $controller->attributes['_controller'] = $controller->controller;
65
        $controller->query['_path'] = http_build_query($controller->attributes, '', '&');
66
        $path = $this->fragmentPath.'?'.http_build_query($controller->query, '', '&');
67
 
68
        // we need to sign the absolute URI, but want to return the path only.
69
        $fragmentUri = $sign || $absolute ? $request->getUriForPath($path) : $request->getBaseUrl().$path;
70
 
71
        if (!$sign) {
72
            return $fragmentUri;
73
        }
74
 
75
        $fragmentUri = $this->signer->sign($fragmentUri);
76
 
77
        return $absolute ? $fragmentUri : substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
78
    }
79
 
80
    private function checkNonScalar(array $values): void
81
    {
82
        foreach ($values as $key => $value) {
83
            if (\is_array($value)) {
84
                $this->checkNonScalar($value);
85
            } elseif (!\is_scalar($value) && null !== $value) {
86
                throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
87
            }
88
        }
89
    }
90
}