HIncludeFragmentRenderer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 $globalDefaultTemplate;
  24. private $signer;
  25. private $twig;
  26. private $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. * @return bool true if the templating engine has been set, false otherwise
  41. */
  42. public function hasTemplating()
  43. {
  44. return null !== $this->twig;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. *
  49. * Additional available options:
  50. *
  51. * * default: The default content (it can be a template name or the content)
  52. * * id: An optional hx:include tag id attribute
  53. * * attributes: An optional array of hx:include tag attributes
  54. */
  55. public function render($uri, Request $request, array $options = [])
  56. {
  57. if ($uri instanceof ControllerReference) {
  58. if (null === $this->signer) {
  59. throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
  60. }
  61. // we need to sign the absolute URI, but want to return the path only.
  62. $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), \strlen($request->getSchemeAndHttpHost()));
  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. $template = $options['default'] ?? $this->globalDefaultTemplate;
  67. if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
  68. $content = $this->twig->render($template);
  69. } else {
  70. $content = $template;
  71. }
  72. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  73. if (isset($options['id']) && $options['id']) {
  74. $attributes['id'] = $options['id'];
  75. }
  76. $renderedAttributes = '';
  77. if (\count($attributes) > 0) {
  78. $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
  79. foreach ($attributes as $attribute => $value) {
  80. $renderedAttributes .= sprintf(
  81. ' %s="%s"',
  82. htmlspecialchars($attribute, $flags, $this->charset, false),
  83. htmlspecialchars($value, $flags, $this->charset, false)
  84. );
  85. }
  86. }
  87. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getName()
  93. {
  94. return 'hinclude';
  95. }
  96. }