You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB

3 years ago
  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. $uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
  59. }
  60. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  61. $uri = str_replace('&', '&amp;', $uri);
  62. $template = $options['default'] ?? $this->globalDefaultTemplate;
  63. if (null !== $this->twig && $template && $this->twig->getLoader()->exists($template)) {
  64. $content = $this->twig->render($template);
  65. } else {
  66. $content = $template;
  67. }
  68. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  69. if (isset($options['id']) && $options['id']) {
  70. $attributes['id'] = $options['id'];
  71. }
  72. $renderedAttributes = '';
  73. if (\count($attributes) > 0) {
  74. $flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
  75. foreach ($attributes as $attribute => $value) {
  76. $renderedAttributes .= sprintf(
  77. ' %s="%s"',
  78. htmlspecialchars($attribute, $flags, $this->charset, false),
  79. htmlspecialchars($value, $flags, $this->charset, false)
  80. );
  81. }
  82. }
  83. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getName()
  89. {
  90. return 'hinclude';
  91. }
  92. }