HIncludeFragmentRenderer.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  24. * @param string|null $globalDefaultTemplate The global default content (it can be a template name or the content)
  25. */
  26. public function __construct(
  27. private ?Environment $twig = null,
  28. private ?UriSigner $signer = null,
  29. private ?string $globalDefaultTemplate = null,
  30. private string $charset = 'utf-8',
  31. ) {
  32. }
  33. /**
  34. * Checks if a templating engine has been set.
  35. */
  36. public function hasTemplating(): bool
  37. {
  38. return null !== $this->twig;
  39. }
  40. /**
  41. * Additional available options:
  42. *
  43. * * default: The default content (it can be a template name or the content)
  44. * * id: An optional hx:include tag id attribute
  45. * * attributes: An optional array of hx:include tag attributes
  46. */
  47. public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
  48. {
  49. if ($uri instanceof ControllerReference) {
  50. $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
  51. }
  52. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  53. $uri = str_replace('&', '&amp;', $uri);
  54. $template = $options['default'] ?? $this->globalDefaultTemplate;
  55. if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
  56. $content = $this->twig->render($template);
  57. } else {
  58. $content = $template;
  59. }
  60. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  61. if (isset($options['id']) && $options['id']) {
  62. $attributes['id'] = $options['id'];
  63. }
  64. $renderedAttributes = '';
  65. if (\count($attributes) > 0) {
  66. $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
  67. foreach ($attributes as $attribute => $value) {
  68. $renderedAttributes .= \sprintf(
  69. ' %s="%s"',
  70. htmlspecialchars($attribute, $flags, $this->charset, false),
  71. htmlspecialchars($value, $flags, $this->charset, false)
  72. );
  73. }
  74. }
  75. return new Response(\sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  76. }
  77. public function getName(): string
  78. {
  79. return 'hinclude';
  80. }
  81. }