PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Front_End/vendor/symfony/http-kernel/Fragment/HIncludeFragmentRenderer.php

https://gitlab.com/Sigpot/AirSpot
PHP | 160 lines | 88 code | 20 blank | 52 comment | 15 complexity | b468ae8e569a366ce592271e86299e8b MD5 | raw file
  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. $flags = ENT_QUOTES | ENT_SUBSTITUTE;
  98. foreach ($attributes as $attribute => $value) {
  99. $renderedAttributes .= sprintf(
  100. ' %s="%s"',
  101. htmlspecialchars($attribute, $flags, $this->charset, false),
  102. htmlspecialchars($value, $flags, $this->charset, false)
  103. );
  104. }
  105. }
  106. return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  107. }
  108. /**
  109. * @param string $template
  110. *
  111. * @return bool
  112. */
  113. private function templateExists($template)
  114. {
  115. if ($this->templating instanceof EngineInterface) {
  116. try {
  117. return $this->templating->exists($template);
  118. } catch (\InvalidArgumentException $e) {
  119. return false;
  120. }
  121. }
  122. $loader = $this->templating->getLoader();
  123. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  124. return $loader->exists($template);
  125. }
  126. try {
  127. $loader->getSource($template);
  128. return true;
  129. } catch (\Twig_Error_Loader $e) {
  130. }
  131. return false;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getName()
  137. {
  138. return 'hinclude';
  139. }
  140. }