HIncludeFragmentRenderer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Fragment;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\UriSigner;
  15. use Twig\Environment;
  16. /**
  17. * Implements the Hinclude rendering strategy.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class HIncludeFragmentRenderer extends RoutableFragmentRenderer
  22. {
  23. private ?string $globalDefaultTemplate;
  24. private $signer;
  25. private $twig;
  26. private string $charset;
  27. /**
  28. * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
  29. */
  30. public function __construct(Environment $twig = null, UriSigner $signer = null, string $globalDefaultTemplate = null, string $charset = 'utf-8')
  31. {
  32. $this->twig = $twig;
  33. $this->globalDefaultTemplate = $globalDefaultTemplate;
  34. $this->signer = $signer;
  35. $this->charset = $charset;
  36. }
  37. /**
  38. * Checks if a templating engine has been set.
  39. */
  40. public function hasTemplating(): bool
  41. {
  42. return null !== $this->twig;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * Additional available options:
  48. *
  49. * * default: The default content (it can be a template name or the content)
  50. * * id: An optional hx:include tag id attribute
  51. * * attributes: An optional array of hx:include tag attributes
  52. */
  53. public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
  54. {
  55. if ($uri instanceof ControllerReference) {
  56. $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
  57. }
  58. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  59. $uri = str_replace('&', '&amp;', $uri);
  60. $template = $options['default'] ?? $this->globalDefaultTemplate;
  61. if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
  62. $content = $this->twig->render($template);
  63. } else {
  64. $content = $template;
  65. }
  66. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  67. if (isset($options['id']) && $options['id']) {
  68. $attributes['id'] = $options['id'];
  69. }
  70. $renderedAttributes = '';
  71. if (\count($attributes) > 0) {
  72. $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
  73. foreach ($attributes as $attribute => $value) {
  74. $renderedAttributes .= sprintf(
  75. ' %s="%s"',
  76. htmlspecialchars($attribute, $flags, $this->charset, false),
  77. htmlspecialchars($value, $flags, $this->charset, false)
  78. );
  79. }
  80. }
  81. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getName(): string
  87. {
  88. return 'hinclude';
  89. }
  90. }