Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\Response;
16
use Symfony\Component\HttpKernel\Controller\ControllerReference;
17
use Symfony\Component\HttpKernel\UriSigner;
18
use Twig\Environment;
19
 
20
/**
21
 * Implements the Hinclude rendering strategy.
22
 *
23
 * @author Fabien Potencier <fabien@symfony.com>
24
 */
25
class HIncludeFragmentRenderer extends RoutableFragmentRenderer
26
{
27
    private ?string $globalDefaultTemplate;
28
    private ?UriSigner $signer;
29
    private ?Environment $twig;
30
    private string $charset;
31
 
32
    /**
688 lars 33
     * @param string|null $globalDefaultTemplate The global default content (it can be a template name or the content)
148 lars 34
     */
35
    public function __construct(Environment $twig = null, UriSigner $signer = null, string $globalDefaultTemplate = null, string $charset = 'utf-8')
36
    {
37
        $this->twig = $twig;
38
        $this->globalDefaultTemplate = $globalDefaultTemplate;
39
        $this->signer = $signer;
40
        $this->charset = $charset;
41
    }
42
 
43
    /**
44
     * Checks if a templating engine has been set.
45
     */
46
    public function hasTemplating(): bool
47
    {
48
        return null !== $this->twig;
49
    }
50
 
51
    /**
52
     * Additional available options:
53
     *
54
     *  * default:    The default content (it can be a template name or the content)
55
     *  * id:         An optional hx:include tag id attribute
56
     *  * attributes: An optional array of hx:include tag attributes
57
     */
58
    public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
59
    {
60
        if ($uri instanceof ControllerReference) {
61
            $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
62
        }
63
 
64
        // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
65
        $uri = str_replace('&', '&amp;', $uri);
66
 
67
        $template = $options['default'] ?? $this->globalDefaultTemplate;
68
        if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
69
            $content = $this->twig->render($template);
70
        } else {
71
            $content = $template;
72
        }
73
 
74
        $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
75
        if (isset($options['id']) && $options['id']) {
76
            $attributes['id'] = $options['id'];
77
        }
78
        $renderedAttributes = '';
79
        if (\count($attributes) > 0) {
80
            $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
81
            foreach ($attributes as $attribute => $value) {
82
                $renderedAttributes .= sprintf(
83
                    ' %s="%s"',
84
                    htmlspecialchars($attribute, $flags, $this->charset, false),
85
                    htmlspecialchars($value, $flags, $this->charset, false)
86
                );
87
            }
88
        }
89
 
90
        return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
91
    }
92
 
93
    public function getName(): string
94
    {
95
        return 'hinclude';
96
    }
97
}