HIncludeFragmentRenderer.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if (!defined('ENT_SUBSTITUTE')) {
  12. define('ENT_SUBSTITUTE', 8);
  13. }
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Templating\EngineInterface;
  17. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  18. use Symfony\Component\HttpKernel\UriSigner;
  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|\Twig_Environment $templating An EngineInterface or a \Twig_Environment 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|\Twig_Environment|null $templating An EngineInterface or a \Twig_Environment instance
  49. *
  50. * @throws \InvalidArgumentException
  51. */
  52. public function setTemplating($templating)
  53. {
  54. if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_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 Boolean 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. foreach ($attributes as $attribute => $value) {
  101. $renderedAttributes .= sprintf(
  102. ' %s="%s"',
  103. htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false),
  104. htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false)
  105. );
  106. }
  107. }
  108. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  109. }
  110. /**
  111. * @param string $template
  112. *
  113. * @return boolean
  114. */
  115. private function templateExists($template)
  116. {
  117. if ($this->templating instanceof EngineInterface) {
  118. try {
  119. return $this->templating->exists($template);
  120. } catch (\InvalidArgumentException $e) {
  121. return false;
  122. }
  123. }
  124. $loader = $this->templating->getLoader();
  125. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  126. return $loader->exists($template);
  127. }
  128. try {
  129. $loader->getSource($template);
  130. return true;
  131. } catch (\Twig_Error_Loader $e) {
  132. }
  133. return false;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getName()
  139. {
  140. return 'hinclude';
  141. }
  142. }