Dashboard sipadu mbip
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HIncludeFragmentRenderer.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Symfony\Component\Templating\EngineInterface;
  16. use Twig\Environment;
  17. use Twig\Error\LoaderError;
  18. use Twig\Loader\ExistsLoaderInterface;
  19. /**
  20. * Implements the Hinclude rendering strategy.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class HIncludeFragmentRenderer extends RoutableFragmentRenderer
  25. {
  26. private $globalDefaultTemplate;
  27. private $signer;
  28. private $templating;
  29. private $charset;
  30. /**
  31. * @param EngineInterface|Environment $templating An EngineInterface or a Twig instance
  32. * @param UriSigner $signer A UriSigner instance
  33. * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
  34. * @param string $charset
  35. */
  36. public function __construct($templating = null, UriSigner $signer = null, string $globalDefaultTemplate = null, string $charset = 'utf-8')
  37. {
  38. $this->setTemplating($templating);
  39. $this->globalDefaultTemplate = $globalDefaultTemplate;
  40. $this->signer = $signer;
  41. $this->charset = $charset;
  42. }
  43. /**
  44. * Sets the templating engine to use to render the default content.
  45. *
  46. * @param EngineInterface|Environment|null $templating An EngineInterface or an Environment instance
  47. *
  48. * @throws \InvalidArgumentException
  49. */
  50. public function setTemplating($templating)
  51. {
  52. if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof Environment) {
  53. throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of Twig\Environment or Symfony\Component\Templating\EngineInterface');
  54. }
  55. if ($templating instanceof EngineInterface) {
  56. @trigger_error(sprintf('Using a "%s" instance for "%s" is deprecated since version 4.3; use a \Twig\Environment instance instead.', EngineInterface::class, __CLASS__), E_USER_DEPRECATED);
  57. }
  58. $this->templating = $templating;
  59. }
  60. /**
  61. * Checks if a templating engine has been set.
  62. *
  63. * @return bool true if the templating engine has been set, false otherwise
  64. */
  65. public function hasTemplating()
  66. {
  67. return null !== $this->templating;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * Additional available options:
  73. *
  74. * * default: The default content (it can be a template name or the content)
  75. * * id: An optional hx:include tag id attribute
  76. * * attributes: An optional array of hx:include tag attributes
  77. */
  78. public function render($uri, Request $request, array $options = [])
  79. {
  80. if ($uri instanceof ControllerReference) {
  81. if (null === $this->signer) {
  82. throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
  83. }
  84. // we need to sign the absolute URI, but want to return the path only.
  85. $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), \strlen($request->getSchemeAndHttpHost()));
  86. }
  87. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  88. $uri = str_replace('&', '&amp;', $uri);
  89. $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
  90. if (null !== $this->templating && $template && $this->templateExists($template)) {
  91. $content = $this->templating->render($template);
  92. } else {
  93. $content = $template;
  94. }
  95. $attributes = isset($options['attributes']) && \is_array($options['attributes']) ? $options['attributes'] : [];
  96. if (isset($options['id']) && $options['id']) {
  97. $attributes['id'] = $options['id'];
  98. }
  99. $renderedAttributes = '';
  100. if (\count($attributes) > 0) {
  101. $flags = ENT_QUOTES | ENT_SUBSTITUTE;
  102. foreach ($attributes as $attribute => $value) {
  103. $renderedAttributes .= sprintf(
  104. ' %s="%s"',
  105. htmlspecialchars($attribute, $flags, $this->charset, false),
  106. htmlspecialchars($value, $flags, $this->charset, false)
  107. );
  108. }
  109. }
  110. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  111. }
  112. private function templateExists(string $template): bool
  113. {
  114. if ($this->templating instanceof EngineInterface) {
  115. try {
  116. return $this->templating->exists($template);
  117. } catch (\Exception $e) {
  118. return false;
  119. }
  120. }
  121. $loader = $this->templating->getLoader();
  122. if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
  123. return $loader->exists($template);
  124. }
  125. try {
  126. if (method_exists($loader, 'getSourceContext')) {
  127. $loader->getSourceContext($template);
  128. } else {
  129. $loader->getSource($template);
  130. }
  131. return true;
  132. } catch (LoaderError $e) {
  133. }
  134. return false;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getName()
  140. {
  141. return 'hinclude';
  142. }
  143. }