HIncludeFragmentRenderer.php 5.6 KB

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