HIncludeFragmentRenderer.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $uri = $this->signer->sign($this->generateFragmentUri($uri, $request));
  84. }
  85. // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
  86. $uri = str_replace('&', '&amp;', $uri);
  87. $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
  88. if (null !== $this->templating && $template && $this->templateExists($template)) {
  89. $content = $this->templating->render($template);
  90. } else {
  91. $content = $template;
  92. }
  93. $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
  94. if (isset($options['id']) && $options['id']) {
  95. $attributes['id'] = $options['id'];
  96. }
  97. $renderedAttributes = '';
  98. if (count($attributes) > 0) {
  99. foreach ($attributes as $attribute => $value) {
  100. $renderedAttributes .= sprintf(
  101. ' %s="%s"',
  102. htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false),
  103. htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false)
  104. );
  105. }
  106. }
  107. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  108. }
  109. /**
  110. * @param string $template
  111. *
  112. * @return boolean
  113. */
  114. private function templateExists($template)
  115. {
  116. if ($this->templating instanceof EngineInterface) {
  117. try {
  118. return $this->templating->exists($template);
  119. } catch (\InvalidArgumentException $e) {
  120. return false;
  121. }
  122. }
  123. $loader = $this->templating->getLoader();
  124. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  125. return $loader->exists($template);
  126. }
  127. try {
  128. $loader->getSource($template);
  129. return true;
  130. } catch (\Twig_Error_Loader $e) {
  131. }
  132. return false;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getName()
  138. {
  139. return 'hinclude';
  140. }
  141. }