123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace Symfony\Component\HttpKernel\Fragment;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Controller\ControllerReference;
- use Symfony\Component\HttpKernel\UriSigner;
- use Twig\Environment;
- class HIncludeFragmentRenderer extends RoutableFragmentRenderer
- {
- private $globalDefaultTemplate;
- private $signer;
- private $twig;
- private $charset;
-
- public function __construct(Environment $twig = null, UriSigner $signer = null, string $globalDefaultTemplate = null, string $charset = 'utf-8')
- {
- $this->twig = $twig;
- $this->globalDefaultTemplate = $globalDefaultTemplate;
- $this->signer = $signer;
- $this->charset = $charset;
- }
-
- public function hasTemplating()
- {
- return null !== $this->twig;
- }
-
- public function render($uri, Request $request, array $options = [])
- {
- if ($uri instanceof ControllerReference) {
- $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
- }
-
- $uri = str_replace('&', '&', $uri);
- $template = $options['default'] ?? $this->globalDefaultTemplate;
- if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
- $content = $this->twig->render($template);
- } else {
- $content = $template;
- }
- $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
- if (isset($options['id']) && $options['id']) {
- $attributes['id'] = $options['id'];
- }
- $renderedAttributes = '';
- if (\count($attributes) > 0) {
- $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
- foreach ($attributes as $attribute => $value) {
- $renderedAttributes .= sprintf(
- ' %s="%s"',
- htmlspecialchars($attribute, $flags, $this->charset, false),
- htmlspecialchars($value, $flags, $this->charset, false)
- );
- }
- }
- return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
- }
-
- public function getName()
- {
- return 'hinclude';
- }
- }
|