Drupal investigation

HIncludeFragmentRenderer.php 5.7KB

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