HIncludeFragmentRenderer.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\HttpFoundation\UriSigner;
  14. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  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 ?UriSigner $signer;
  25. private ?Environment $twig;
  26. private string $charset;
  27. /**
  28. * @param string|null $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. * Additional available options:
  46. *
  47. * * default: The default content (it can be a template name or the content)
  48. * * id: An optional hx:include tag id attribute
  49. * * attributes: An optional array of hx:include tag attributes
  50. */
  51. public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
  52. {
  53. if ($uri instanceof ControllerReference) {
  54. $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
  55. }
  56. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  57. $uri = str_replace('&', '&amp;', $uri);
  58. $template = $options['default'] ?? $this->globalDefaultTemplate;
  59. if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
  60. $content = $this->twig->render($template);
  61. } else {
  62. $content = $template;
  63. }
  64. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  65. if (isset($options['id']) && $options['id']) {
  66. $attributes['id'] = $options['id'];
  67. }
  68. $renderedAttributes = '';
  69. if (\count($attributes) > 0) {
  70. $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
  71. foreach ($attributes as $attribute => $value) {
  72. $renderedAttributes .= \sprintf(
  73. ' %s="%s"',
  74. htmlspecialchars($attribute, $flags, $this->charset, false),
  75. htmlspecialchars($value, $flags, $this->charset, false)
  76. );
  77. }
  78. }
  79. return new Response(\sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  80. }
  81. public function getName(): string
  82. {
  83. return 'hinclude';
  84. }
  85. }